如何延长结构的寿命,以便可以使用它来调用tokio :: run?

时间:2018-10-30 11:06:21

标签: rust

我有一个有效的功能:

extern crate tokio;

use std::time::{Duration, Instant};
use tokio::prelude::*;
use tokio::timer::Interval;

fn run(label: String) -> impl Future<Item = (), Error = ()> {
    Interval::new(Instant::now(), Duration::from_millis(1000))
        .for_each(move |instant| {
            println!("fire; instant={:?}, label={:?}", instant, label);
            Ok(())
        })
        .map_err(|e| panic!("interval errored; err={:?}", e))
}

fn main() {
    tokio::run(run("Hello".to_string()));
}

playground

在这种情况下,我想使用方法label创建一个可容纳某些参数(run)的结构,该方法将利用这些参数:

extern crate tokio;

use std::time::{Duration, Instant};
use tokio::prelude::*;
use tokio::timer::Interval;

struct Ir {
    label: String,
}

impl Ir {
    fn new(label: String) -> Ir {
        Ir { label }
    }

    fn run(&self) -> impl Future<Item = (), Error = ()> + '_ {
        Interval::new(Instant::now(), Duration::from_millis(1000))
            .for_each(move |instant| {
                println!("fire; instant={:?}, label={:?}", instant, self.label);
                Ok(())
            })
            .map_err(|e| panic!("interval errored; err={:?}", e))
    }
}

fn main() {
    let ir = Ir::new("Hello".to_string());
    tokio::run(ir.run());
}

playground

我得到的是:

error[E0597]: `ir` does not live long enough
  --> src/main.rs:28:16
   |
28 |     tokio::run(ir.run());
   |                ^^ borrowed value does not live long enough
29 | }
   | - borrowed value only lives until here
   |
   = note: borrowed value must be valid for the static lifetime...

我已经阅读了“ Rust by Example”中的“ Advanced Lifetimes”和“ Validating References with Lifetimes”,但我仍然不知道如何解决它。
ir为什么寿命不够长?
我已经在尝试调用ir.run()的范围内创建了它,因此我认为它会一直存在。

1 个答案:

答案 0 :(得分:4)

如果您重写:

tokio::run(ir.run());

为:

tokio::run(Ir::run(&ir))

错误变得更加清楚了:

error[E0597]: `ir` does not live long enough
  --> src/main.rs:28:24
   |
28 |    tokio::run(Ir::run(&ir));
   |                        ^^ borrowed value does not live long enough
29 | }
   | - borrowed value only lives until here
   |
   = note: borrowed value must be valid for the static lifetime...

由于tokio::run的未来需要'static的生命期:

pub fn run<F>(future: F) 
where
    F: Future<Item = (), Error = ()> + Send + 'static, 

为避免生命周期问题,请考虑使用Ir值:

fn run(self) -> impl Future<Item = (), Error = ()>  {
    Interval::new(Instant::now(), Duration::from_millis(1000))
        .for_each(move |instant| {
            println!("fire; instant={:?}, label={:?}", instant, self.label);
            Ok(())
        })
        .map_err(|e| panic!("interval errored; err={:?}", e))
}