-1
use futures::{future::BoxFuture, FutureExt}; // 0.3.6

struct Inner {}

impl Inner {
    async fn foo3(&self) -> Result<u32, ()> {
        Ok(8)
    }
}

struct Outer {
    i: Inner,
}

impl Outer {
    fn foo4(&mut self) -> BoxFuture<'static, Result<u32, ()>> {
        self.i.foo3().boxed()
    }
}

playground:

error[E0759]: `self` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
  --> src/lib.rs:17:16
   |
16 |     fn foo4(&mut self) -> BoxFuture<'static, Result<u32, ()>> {
   |             --------- this data with an anonymous lifetime `'_`...
17 |         self.i.foo3().boxed()
   |         -------^^^^---------- ...is captured and required to live as long as `'static` here

I notice that I can specify a lifetime for BoxFuture; instead of passing 'static, what else can I pass to make this code compile?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
user855
  • 19,048
  • 38
  • 98
  • 162
  • [Please don't edit your question to change it after you've received answers](https://meta.stackexchange.com/q/64459/281829), especially if those edits invalidate answers. If your question didn't ask what you needed, then ask a new question, link to the previous one, and describe what the exact difference(s) are. – Shepmaster Nov 15 '20 at 03:13

1 Answers1

-1

As the error message states:

self has an anonymous lifetime '_

Match the lifetime of the return type with the lifetime of the argument:

impl Outer {
    fn foo4(&mut self) -> BoxFuture<'_, Result<u32, ()>> {
        self.i.foo3().boxed()
    }
}

This is equivalent to

impl Outer {
    fn foo4<'a>(&'a mut self) -> BoxFuture<'a, Result<u32, ()>> {
        self.i.foo3().boxed()
    }
}

This specific case doesn't require boxing, so I'd write

impl Outer {
    async fn foo4(&mut self) -> Result<u32, ()> {
        self.i.foo3().await
    }
}

See also:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • I see. Thanks. I tried '_ and this code compiled. However, my real issue is still not solved. The example code I provided here is a stripped down version of what I am trying to accomplish. With '_ the code compiled, but in my "real application", the error is now showed in the outer layer of code. I have a Outer2 { o: Outer; futures_list: FuturesOrdered> } ... When I try to push the future returned by foo4 into the futures_list ... I get a new compilation error. With ' – user855 Nov 15 '20 at 01:17
  • https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8754d4d988348d321d2256befb512e5e – user855 Nov 15 '20 at 01:21
  • @user855 that's an iteration on [Why can't I store a value and a reference to that value in the same struct?](https://stackoverflow.com/q/32300132/155423). You can't do it. – Shepmaster Nov 15 '20 at 01:24
  • So, what is the workaround ... Where do I store this future? The reason I want to store it because I want to tokio::select! on that future with other futures ... What coding pattern do I follow? – user855 Nov 15 '20 at 01:25
  • Am I doomed? Can I not write this program in Rust? @shepmaster – user855 Nov 15 '20 at 01:27
  • I will try to write my application with this pattern instead -- https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=ab4e5e55f75a0703957257e618f49306 – user855 Nov 15 '20 at 01:51