简体   繁体   English

rust 生命周期参数必须比静态生命周期更长

[英]rust lifetime parameter must outlive the static lifetime

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.因此,我正在按照本教程构建一个 rogue-like,并且我决定开始使用 specs 调度程序来使注册和执行系统更容易一些。

To do that I've added aDispatcher to my State struct:为此,我在我的State结构中添加了一个Dispatcher


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>,
}

But it's when I try to implement the GameSate trait for it I run into issues:但是当我尝试为其实现GameSate特征时,我遇到了问题:

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();
    }
}

I get these errors:我收到这些错误:

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

It wants the lifetimes 'a, 'b to outlive 'static , which sounds impossible as I'm sure that 'static is the whole program's lifetime.它希望'a, 'b的生命周期超过'static ,这听起来不可能,因为我确信'static是整个程序的生命周期。

How do I resolve this?我该如何解决这个问题?

GameState requires that implementors must be 'static : GameState要求实现者必须是'static

pub trait GameState: 'static {...}

In order to satisfy the 'static lifetime, your type must not contain any references shorter than 'static .为了满足'static生命周期,您的类型不得包含任何短于'static引用。 So, if you can't make 'a and 'b both be 'static , the only option is not to put the Dispatcher inside State .因此,如果您不能将'a'b都设为'static ,那么唯一的选择就是不要将Dispatcher放入State中。

For lifetime 'a to "outlive 'static " means that 'a must be equal or longer than 'static (and yes, 'static is the longest lifetime possible).对于生命周期'a “比'static寿命更长”意味着'a必须等于或长于'static (是的, 'static是可能的最长生命周期)。 Rust issue for a similar error .类似错误的 Rust 问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM