我的项目中有一个结构,该结构在逻辑上与来自不同板条箱的A结构相关。B两者内部都有一个可选的子结构 ( C/ D)。
假设对于这个例子,他们有这个结构定义:
struct D {
name: Option<String>
}
struct B {
spec: Option<D>
}
struct C {
name: Option<String>
}
struct A {
spec: Option<C>
}
Run Code Online (Sandbox Code Playgroud)
现在我想将Into-trait实现A为B:
impl Into<D> for C {
fn into(self) -> D {
D {
name: self.name
}
}
}
impl Into<B> for A {
fn into(self) -> B {
B {
spec: self.spec.into()
}
}
}
Run Code Online (Sandbox Code Playgroud)
但 Rust 不允许这样做:
error[E0277]: …Run Code Online (Sandbox Code Playgroud)