use std::marker::PhantomData;
trait SceneType {}
struct Scene<SceneType: ?Sized> {
_phantom: PhantomData<SceneType>
}
pub enum SceneOne {}
impl SceneType for SceneOne {}
impl<SceneOne> Scene<SceneOne> {
pub fn new() -> Self {
Self {
_phantom: Default::default()
}
}
}
fn main() {
let scene = Scene::<SceneOne>::new();
//works fine
//1 let boxed_scene: Box<Scene<dyn SceneType>> = Box::new(Scene::<SceneOne>::new());
// expected `Scene<dyn SceneType>`, found `Scene<SceneOne>` ... you could box the found value and coerce it to the trait object `Box<dyn SceneType>
//2 let boxed_scene: Box<Scene<dyn SceneType>> = Box::new(scene) as Box<Scene<dyn SceneType>>;
// as` expression can only be used to convert between primitive types or to coerce to a specific trait object
}
所以我最终来到这里的原因是我想要一个通用的新功能SceneType
,并且具有可以在任何场景上运行的通用功能
impl<T: SceneType> Scene<T> {
...
我的问题是,由于列出的错误消息,我无法将这些类型装箱以将它们存储在向量中。2 的错误是有道理的,但据我所知,我试图将其强制转换为特定的特征类型。