我在Rust中创建一个实体组件系统,我希望能够Vec为每种不同的Component类型存储一个组件:
pub trait Component {}
struct ComponentList<T: Component> {
components: Vec<T>,
}
Run Code Online (Sandbox Code Playgroud)
是否有可能创建这些ComponentLists 的集合?
struct ComponentManager {
component_lists: Vec<ComponentList<_>>, // This does not work
}
Run Code Online (Sandbox Code Playgroud)
这旨在使检索某种Component类型的列表更快,因为某种类型的组件的所有实例都是相同的ComponentList.
我曾尝试使用原始指针强制转换my_struct as *const usize,但这会导致non-scalar cast错误.找到基元的地址时,原始指针似乎工作正常,但不是自定义structs.
我正在尝试memmove在Rust中实现标准函数,我想知道哪种方法对于向下迭代更快(其中src< dest):
for i in (0..n).rev() {
//Do copying
}
Run Code Online (Sandbox Code Playgroud)
要么
let mut i = n;
while i != 0 {
i -= 1;
// Do copying
}
Run Code Online (Sandbox Code Playgroud)
将rev()在for环版本显著慢下来?
我想知道在对象上过度使用get()或在变量中存储get()的返回是否更有效并使用它.例如,执行此操作会更有效:
someObject.setColor(otherObject.getColor().r, otherObject.getColor().g,
otherObject.getColor().b);
Run Code Online (Sandbox Code Playgroud)
或者将它存储在这样的变量中
Color color = otherObject.getColor();
someObject.setColor(color.r, color.g, color.b);
Run Code Online (Sandbox Code Playgroud)