我正在尝试用 Rust 制作一个小游戏。我想使用类似于实体组件系统模式的东西来处理所有游戏对象。
我的总体想法是有一个GameLoop
结构,它包含更新和绘制游戏所需的所有数据(屏幕、时间戳……)。
该World
结构应该包含所有游戏实体并在dispatch
函数中更新它们。它还调用存储在World
结构中的所有注册回调(那些是“系统”)。但是,它们在示例代码中是多余的。
我试图尽可能地分解代码,只包含相关部分。
use std::marker::PhantomData;
struct Something;
///The "somethings" are things like the display, a timestamp, ...
struct GameLoop {
sth: Something,
sth2: Something,
}
///C = Context
///The type that is passed to every entity to give it access to things like the delta time
struct World<C> {
phantom: PhantomData<C>, //This is here so Rust doesn't complain about the type parameter not being used
}
///The data that is passed to the system functions every frame
struct TickData<'a> {
delta: u64,
sth: &'a Something,
sth2: &'a mut Something,
}
impl GameLoop {
fn new() -> GameLoop {
GameLoop {
sth: Something {},
sth2: Something {},
}
}
///One game "tick" - Supposed to do things like calculating delta time, swapping buffers, ...
///Those are then passed to the callback
fn update<F: FnMut(u64, &Something, &mut Something)>(&mut self, f: &mut F) {
f(0, &self.sth, &mut self.sth2);
}
}
impl<C> World<C> {
fn new() -> World<C> {
World { phantom: PhantomData }
}
///Supposed to update all the game entities
fn dispatch(&mut self, context: &mut C) {
//...
}
}
impl<'a> TickData<'a> {
fn new<'b>(delta: u64, sth: &'b Something, sth2: &'b mut Something) -> TickData<'b> {
TickData {
delta: delta,
sth: sth,
sth2: sth2,
}
}
}
fn main() {
let mut game_loop = GameLoop::new();
let mut world = World::<TickData>::new();
//The game update function, called once per frame
let mut update_fnc = |delta: u64, sth: &Something, sth2: &mut Something| {
let mut tick_data = TickData::new(delta, sth, sth2);
&world.dispatch(&mut tick_data); //If this line is commented out, it compiles fine
//...
};
loop {
game_loop.update(&mut update_fnc); //Calculate the delta time, call the specified function and swap buffers
}
}
借贷/寿命似乎有问题。编译器除了冗长之外什么都没有。
问题似乎是&world.dispatch(&mut tick_data)
游戏更新函数中的调用,它应该更新所有游戏实体。如果我将其注释掉,则程序编译不会出现任何错误。
这是编译器告诉我的:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter 'b in function call due to conflicting requirements
--> src/main.rs:66:29
|
66 | let mut tick_data = TickData::new(delta, sth, sth2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 65:77...
--> src/main.rs:65:78
|
65 | let mut update_fnc = |delta: u64, sth: &Something, sth2: &mut Something| {
| ______________________________________________________________________________^ starting here...
66 | | let mut tick_data = TickData::new(delta, sth, sth2);
67 | |
68 | | &world.dispatch(&mut tick_data); //If this line is commented out, it compiles fine
69 | |
70 | | //...
71 | | };
| |_____^ ...ending here
note: ...so that reference does not outlive borrowed content
--> src/main.rs:66:55
|
66 | let mut tick_data = TickData::new(delta, sth, sth2);
| ^^^^
note: but, the lifetime must be valid for the expression at 74:25...
--> src/main.rs:74:26
|
74 | game_loop.update(&mut update_fnc); //Calculate the delta time, call the specified function and swap buffers
| ^^^^^^^^^^^^^^^
note: ...so that reference is valid at the time of borrow
--> src/main.rs:74:26
|
74 | game_loop.update(&mut update_fnc); //Calculate the delta time, call the specified function and swap buffers
| ^^^^^^^^^^^^^^^
我根本无法发现错误的原因。这些函数以一种程序方式调用,因为我只是借用了大部分数据,所以生命周期应该没有问题。
当我从结构中删除引用时,TickData
它只包含为Copy
特征实现的值,它也可以工作。
我通常不是那种张贴代码墙并要求人们修复它的人,但我现在真的一无所知。