Rust 具有易变性的关联类型中的生存期参数
我试图在Rust中编写一个实体组件系统(版本Rust 具有易变性的关联类型中的生存期参数,rust,lifetime,mutability,Rust,Lifetime,Mutability,我试图在Rust中编写一个实体组件系统(版本rustc 1.19.0-nightly(557967766 2017-05-26)),而不使用向下转换,最好也不使用宏。通用架构是specs和calx ecs的松散合并 我正在努力定义一个工作run方法,该方法根据所选组件执行系统操作。要求是系统可以相互借用每个相关实体的组件。然而,我真的在和借书人打交道,我有点迷路了 run方法由Schedulerstruct实现,它拥有一个Worldstruct和一个装箱系统特征对象向量 pub type Com
rustc 1.19.0-nightly(557967766 2017-05-26)
),而不使用向下转换,最好也不使用宏。通用架构是specs
和calx ecs
的松散合并
我正在努力定义一个工作run
方法,该方法根据所选组件执行系统操作。要求是系统可以相互借用每个相关实体的组件。然而,我真的在和借书人打交道,我有点迷路了
run
方法由Scheduler
struct实现,它拥有一个World
struct和一个装箱系统特征对象向量
pub type ComponentMask = u8;
pub trait AssemblyTrait {
fn new() -> Self;
fn match_mask(&self, entity: &Entity, mask: ComponentMask) -> bool;
fn remove(&mut self, entity: &Entity);
}
pub trait ViewTrait<'a> {
type View;
fn get_view(&'a mut self, entity: &Entity) -> Self::View;
}
pub struct World<'a, A: 'a + AssemblyTrait + ViewTrait<'a>> {
next_idx: usize,
next_uuid: UUID,
free_indices: Vec<usize>,
entities: ComponentContainer<bool>,
pub components: A,
phantom: marker::PhantomData<&'a A>,
}
pub struct Scheduler<'a, A> where A: 'a + AssemblyTrait + ViewTrait<'a> {
world: World<'a, A>,
systems: Vec<Box<'static + SystemTrait<'a, A>>>,
}
impl<'a, A> Scheduler<'a, A> where A: 'a + AssemblyTrait + ViewTrait<'a> {
// Some methods ommitted.
pub fn run(&'a mut self) {
for system in self.systems.iter() {
let mask = system.get_mask();
let mut components: Vec<A::View> = self.world.iter()
.filter(|e| self.world.match_mask(e, mask))
.map(|e| self.world.components.get_view(e))
.collect();
if components.len() > 0 {
system.run(&components);
}
}
}
}
您能否在问题中给出其余相关类型的定义?特别是,
Scheduler
、AssemblyTrait
和ViewTrait
应该在这里。这个问题看起来非常类似(虽然不是迭代器,而是您自己的特点):&mut self
的生存期与'a
@E_net4的生存期不兼容:我看到了与链接问题的相似之处。虽然我不太清楚我将如何运用它来解决我的问题。同时,我添加了您要求的定义。我注意到您使用了一个视图traiti,它实际上引入了绑定到泛型类型a
的附加生存期,因为structWorld
包含一个std::marker::PhantomData
字段,该字段反过来会使用一个错误使用的生存期参数。我将更新问题以包含世界结构的定义。@Shepmaster,谢谢您正在进行的回答。在您的基础上,我自己重新创建了一个,并意识到我的代码同时存在多个问题:不仅我的viewtract::View
(即可变引用作为返回类型)存在一个终生问题,而且我还多次可变地借用World
。我想,我要问的是;我怎样才能把这一切都解决?
pub trait SystemTrait<'a, A> where A: AssemblyTrait + ViewTrait<'a> {
fn get_name(&self) -> &'static str;
fn get_mask(&self) -> ComponentMask;
fn run(&mut self, components: &mut [A::View]);
}