0

I have a following struct:

pub struct Foo {
    v: Vec<T>, // `T` is a struct that does not implement `Copy` trait
    index: u32,
}

inside this struct's implementation I have following method:

fn foo(&mut self) -> Box<dyn E> {
    let t = self.peek();
    Box::new(Bar::new(t).unwrap()) // constructor of `Bar` takes `&T` and returns `Option<Bar>`
}

// returns `T` at current `index`
// since `T` does not implement `Copy` I have to return reference
fn peek(&self) -> &T{
    &self.v[self.index as usize]
}

E is some trait that returned value should implement, Bar is a struct that takes reference to T. Again, since T does not implement Copy, I have to pass it as a reference, which also forces me to impose lifetime requirements on struct Bar:

// if my understanding is correct, this means `Bar` lives as long as `field` does  
pub struct Bar<'a> {
    field: &'a T,
}

However, when I try to compile the code I get following error message:

error[E0759]: `self` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
  --> src\parser.rs:78:29
   |
64 |          fn foo(&mut self) -> Box<dyn Expr> {
   |                 --------- this data with an anonymous lifetime `'_`...
...
78 |              let t = self.peek();
   |                        ---- ^^^^
   |                        |
   |                        ...is captured here...
79 |         Box::new(Binary::new(t).unwrap())
   |         ---------------------------------------------------- ...and is required to live as long as `'static` here
   |
help: to declare that the trait object captures data from argument `self`, you can add an explicit `'_` lifetime bound
   |
64 |         fn foo(&mut self) -> Box<dyn Expr + '_> {
   |                                            ^^^^

Why is the error occurring here? Why is the compiler asking for the 'static lifetime?

Ach113
  • 1,775
  • 3
  • 18
  • 40
  • Unless you explicitly state otherwise (as the compiler has suggested in its “help” message), a `Box`ed trait object will by default have `'static` lifetime… and that trait object borrows the peeked element. – eggyal May 18 '21 at 12:41
  • @eggyal does that mean that the only way to solve my problem is to either have `self.peek()` return a reference with `'static` lifetime (which I am not sure is possible) or have it return a struct instead of a reference (which would force me to implement `Copy` trait for `T`, something I wanted to avoid). – Ach113 May 18 '21 at 12:48
  • Have you tried following the compiler’s “help” suggestion? – eggyal May 18 '21 at 12:49
  • I did change the function signature to `Box` as compiler suggested, but I still get the same error – Ach113 May 18 '21 at 12:50
  • Really? [Works for me](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=ce649c095775b8c89e0e10c775871041). – eggyal May 18 '21 at 12:56
  • @eggyal my bad, omitted some parts which I thought were irrelevant to the problmem. [Here](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7ba00e61352ed33fcee28dc8832d8efb) is the reproduction of my issue: – Ach113 May 18 '21 at 13:13
  • The answer to the immediate issue is that you need to specify the lifetime of the trait object parameter in `Bar`'s constructor (line 30); however you will then encounter the problem that in `foo` you are attempting to simultaneously borrow `self` both mutably and immutably, which obviously is not allowed. Solving *that* will require some deeper understanding of the actual problem you're trying to model, and in particular what the `bar` method is doing. – eggyal May 18 '21 at 13:22

0 Answers0