具有返回自我的特质的集合

时间:2019-04-08 16:47:55

标签: rust traits

我正在尝试收集实现特定特征的对象的集合。

如果我使用返回值的特征,则可以使用

use std::collections::BTreeMap;

struct World {
    entities: Vec<usize>,
    database: BTreeMap<usize, Box<ReadValue>>,
    //database : BTreeMap<usize,Box<ReadEcs>>, // Doesn't work
}

struct SourceInputGateway {
    entity_id: usize,
}

trait ReadValue {
    fn read(&self) -> f32;
}

impl ReadValue for SourceInputGateway {
    fn read(&self) -> f32 {
        0.0
    }
}

但是,如果我想返回Self作为值,那么无论是作为方法模板参数还是相关类型,这都行不通

trait ReadEcs {
    type T;
    fn read(&self) -> &Self::T;
}

impl ReadEcs for SourceInputGateway {
    type T = SourceInputGateway;
    fn read(&self) -> &Self::T {
        self
    }
}

我想做的是创建一个实现ReadEcs的类型映射,具体类型并不重要。

进一步的说明修改

如果我通过添加示例来扩展

// Different sized type
struct ComputeCalculator {
    entity_id : usize,
    name : String,
}

impl ReadValue for ComputeCalculator {
    fn read(&self) -> f32 {
        1230.0
    }
}

那我就可以做到

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn read_write() {
        let mut world = World::new();
        world.database.insert(0,Box::new(SourceInputGateway{ entity_id : 1}));
        world.database.insert(2,Box::new(ComputeCalculator{ entity_id : 2 , name : "foo".into() }));

        for (k,ref v) in world.database {
            let item : &Box<ReadValue> = v;
            item.read();
        }

    }
}

但是,如果我更改或添加了返回Self的trait方法,则无法执行此操作。 我想了解一种避免不安全指针的方法。

1 个答案:

答案 0 :(得分:0)

我对此进行了更多思考,我认为有可能在保留类型安全和连续存储所有优点的情况下解决此问题。

使用存储指针定义实体管理器

struct Entities {
    entities: Vec<usize>,
    containers: Vec<Box<Storage>>,
}

存储与行为有关的数据的组件本身

struct Position {
    entity_id: usize,
    position: f32,
}

struct Velocity {
    entity_id: usize,
    velocity: f32,
}

我们将有很多组件实例,因此我们需要一个连续的内存存储,可以通过索引进行访问。

struct PositionStore {
    storage: Vec<Position>,
}

struct VelocityStore {
    storage: Vec<Velocity>,
}



trait Storage {
    // Create and delete instances of the component
    fn allocate(&mut self, entity_id: usize) -> usize;

    // Interface methods that would correspond to a base class in C++
    fn do_this(&self);
    // fn do_that(&self);
}

商店的特征实现了arena样式的存储以及将传递给组件的方法。这可能在ECS的“系统”部分中,但留作以后的练习。

我想提示一下如何将构造自定义对象的Fn()传递给allocate()方法。我还没有想到。

impl Storage for PositionStore {
    fn allocate(&mut self, entity_id: usize) -> usize {
        self.storage.push(Position {
            entity_id,
            position: 0.0,
        });
        self.storage.len() - 1
    }

    fn run(&self) {
        self.storage.iter().for_each(|item| { println!("{}",item.position); });
    }
}

impl Storage for VelocityStore {
    fn allocate(&mut self, entity_id: usize) -> usize {
        self.storage.push(Velocity {
            entity_id,
            velocity: 0.0,
        });
        self.storage.len() - 1
    }

    fn do_this(&self) {
        self.storage.iter().for_each(|item| { println!("{}",item.velocity); });
    }
}

一些锅炉板。

impl Default for PositionStore {
    fn default() -> PositionStore {
        PositionStore {
            storage: Vec::new(),
        }
    }
}

impl Default for VelocityStore {
    fn default() -> VelocityStore {
        VelocityStore {
            storage: Vec::new(),
        }
    }
}

我认为这可以考虑更多。它存储了组件的存储与其位置之间的关系

您可能希望传递一个具有针对每个组件的特定初始化的lambda函数,而不是T::default()

impl Entities {
    fn register<T>(&mut self) -> usize
    where
        T: Storage + Default + 'static,
    {
        self.containers.push(Box::new(T::default()));
        self.containers.len() - 1
    }

    fn create<T>(&mut self, entity_id: usize, id: usize) -> usize {
        self.containers[id].allocate(entity_id)
    }

    fn run_loop(&self) {
        self.containers.iter().for_each(|x| x.do_this());
    }
}

一个测试案例,看是否可行

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn arbitary() {
        let mut entities = Entities {
            entities: Vec::new(),
            containers: Vec::new(),
        };
        let velocity_store_id = entities.register::<VelocityStore>();
        let position_store_id = entities.register::<PositionStore>();

        let _ = entities.create::<Velocity>(123, velocity_store_id);
        let _ = entities.create::<Velocity>(234, velocity_store_id);
        let _ = entities.create::<Position>(234, position_store_id);
        let _ = entities.create::<Position>(567, position_store_id);

        entities.run_loop();
    }
}