Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extend the lifetime of a struct so that I can call tokio::run with it?

Tags:

rust

I have a function which works:

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

I'd like to create a struct which would hold some parameters (label) in this case with a method run which would utilize the parameters:

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

What I get is:

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...

I've read "Advanced Lifetimes" and "Validating References with Lifetimes" from "Rust by Example", but I still don't understand how to fix it.
Why doesn't ir live long enough?
I've created it in the same scope where I'm trying to call ir.run(), so I would assume it would stick around.

like image 924
Leonti Avatar asked Oct 30 '18 11:10

Leonti


1 Answers

If you rewrite:

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

as:

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

The error becomes a little clearer:

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...

Since tokio::run requires a 'static lifetime for the Future:

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

To avoid lifetime issues consider consuming the Ir value:

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))
}
like image 176
attdona Avatar answered Oct 28 '22 07:10

attdona