我正在寻找您对一段代码的反馈/建议。
基本上,我有一个这样的 SOA:
struct Entities {
pub meshes: FakeArena<Mesh>,
pub lights: FakeArena<Light>,
}
我可以通过他的“句柄”访问一个特定的值(每个句柄都绑定到特定的类型),所以我可以通过entities.meshes.get(&handle)
.
到目前为止,一切都很好,但我试图通过相应的竞技场动态检索值来实现这一点。通过做entities.get(&handle)
如果句柄类型是Mesh
,我返回entities.meshes.get(&handle)
。我的Entities
结构有一个名为的方法get
:
fn get<T: Any>(&self, handle: &Handle<T>) -> &T {
let mut entity: Option<&dyn Any> = None;
let any = handle as &dyn Any;
any.downcast_ref::<Handle<Mesh>>()
.map(|handle| entity = Some(self.meshes.get(handle) as &dyn Any));
any.downcast_ref::<Handle<Light>>()
.map(|handle| entity = Some(self.lights.get(handle) as &dyn Any));
if entity.is_none() {
panic!("Type not found in stored entites.");
}
entity
.unwrap()
.downcast_ref::<T>()
.expect("Error while downcasting the entity type")
}
这完美地工作。我将泛型类型转换为具体类型,然后再转换为泛型类型,但这似乎很奇怪和棘手。
也许我遗漏了一些东西,或者您对此有更好的想法;你会怎么做?:)