Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 具有易变性的关联类型中的生存期参数_Rust_Lifetime_Mutability - Fatal编程技术网

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

我试图在Rust中编写一个实体组件系统(版本
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
的附加生存期,因为struct
World
包含一个
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]);
}