rust 生命周期参数必须比静态生命周期长 [英] rust lifetime parameter must outlive the static lifetime
问题描述
所以我正在关注 本教程 来构建一个类似 rogue 的游戏,我已经决定开始使用规范调度器来简化系统的注册和执行.
So I'm following this tutorial to build a rogue-like and I've decided to start using the specs dispatcher to make registering and executing systems a bit easier.
为此,我添加了一个 Dispatcher
到我的 State
结构:
To do that I've added a Dispatcher
to my State
struct:
use rltk::{GameState, Rltk};
use specs::world::World;
use specs::Dispatcher;
pub struct State<'a, 'b> { // <-- Added new lifetime params here for dispacher
pub ecs: World,
pub dsp: Dispatcher<'a, 'b>,
}
但是当我尝试实施 GameSate
它的特征我遇到了问题:
But it's when I try to implement the GameSate
trait for it I run into issues:
impl<'a, 'b> GameState for State<'a, 'b> {
fn tick(&mut self, ctx: &mut Rltk) {
ctx.cls();
self.dsp.dispatch(&mut self.ecs);
self.ecs.maintain();
}
}
我收到这些错误:
error[E0478]: lifetime bound not satisfied
--> src/sys/state.rs:96:14
|
96 | impl<'a, 'b> GameState for State<'a, 'b> {
| ^^^^^^^^^
|
note: lifetime parameter instantiated with the lifetime `'a` as defined on the impl at 96:6
--> src/sys/state.rs:96:6
|
96 | impl<'a, 'b> GameState for State<'a, 'b> {
| ^^
= note: but lifetime parameter must outlive the static lifetime
error[E0478]: lifetime bound not satisfied
--> src/sys/state.rs:96:14
|
96 | impl<'a, 'b> GameState for State<'a, 'b> {
| ^^^^^^^^^
|
note: lifetime parameter instantiated with the lifetime `'b` as defined on the impl at 96:10
--> src/sys/state.rs:96:10
|
96 | impl<'a, 'b> GameState for State<'a, 'b> {
| ^^
= note: but lifetime parameter must outlive the static lifetime
它希望 'a, 'b
的生命周期长于 'static
,这听起来不可能,因为我确信 'static
是整个程序的生命周期.
It wants the lifetimes 'a, 'b
to outlive 'static
, which sounds impossible as I'm sure that 'static
is the whole program's lifetime.
我该如何解决这个问题?
How do I resolve this?
推荐答案
GameState
要求实现者必须是 'static
:
pub trait GameState: 'static {...}
为了满足 'static
生存期,您的类型不得包含任何短于 'static
的引用.所以,如果你不能让 'a
和 'b
都成为 'static
,唯一的选择就是不要把 State
中的 Dispatcher.
In order to satisfy the 'static
lifetime, your type must not contain any references shorter than 'static
. So, if you can't make 'a
and 'b
both be 'static
, the only option is not to put the Dispatcher
inside State
.
这篇关于rust 生命周期参数必须比静态生命周期长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!