这个问题在这里已经有了答案:
Why doesn't Rust support trait object upcasting?
(4 个回答)
Can I cast between two traits?
(5 个回答)
How do I convert a Vec<T> to a Vec<U> without copying the vector?
(2 个回答)
1年前关闭。
我试过this post使用 AsBase
特质,但不能完全举出一个最小的例子。而且由于那个帖子有点旧,缺少dyn
有时会有点困惑。
这是我认为我可以做的:
trait Entity {}
trait Part: Entity {}
trait System {
fn parts(&self) -> &Vec<Box<dyn Entity>>; // This is the specification
}
struct System32 {
parts: Vec<Box<dyn Part>>, // But this is what I have
}
impl System for System32 {
fn parts(&self) -> &Vec<Box<dyn Entity>> {
&self.parts // error: expected trait Entity, found trait Part
// I've also tried:
// &self.parts as &Vec<Box<dyn Entity>>
// error: an `as` expression can only be used to convert between
// primitive types or to coerce to a specific trait object
}
}
这甚至可能吗?如果是,那么我该如何进行类型转换?
最佳答案
Is this even possible?
不。
它有两个问题:第一,Rust 不是基于继承的语言,
trait B: A
表示“B 需要 A”多于“B 扩展 A”。现在,虽然这可能不是 Rust 的 trait 应该被考虑的方式,但无论如何都有执行这种“向上转换”的方法,并且该语言最终可能包含这个特性(部分用于多 trait 对象)。
然而这里有一个更大的问题:你返回一个
&Vec
.这意味着向量必须归某物所有,因为您只返回对它的引用。但是一个
Box<dyn Entity>
和 Box<dyn Part>
是完全不同的值(value)观。回到 Rust 不是基于继承的,
B
的 vtable不嵌入 A
的 vtable ,您不能只说“这现在是指向 A 的指针”,因为它绝对不是 [0]。这意味着从
Box<dyn Part>
开始至Box<dyn Entity>
是一个完整的值(value)转换,而不是对值(value)的就地重新解释为不同的类型。这意味着一个新的
Box
,以及一个新的 Vec
,这意味着您不能只返回对现有 Vec
的引用声称它是您想要的类型,Vec
的内容本身需要改变。[0] 与 C++ 不同,我相信,至少对于 SI 案例,对于 MI,您可以让其中一个 parent 成为简单的重新解释,但其他人必须偏移子指针才能获得正确的 vtable,即使它们是嵌入的,所以我想你会有同样的问题
关于rust - 如何将 &Vec<Box<dyn Child>> 转换为 &Vec<Box<dyn Base>> where trait Child : Base {}?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63239346/