Mutable references vs. dyn

Playground

Here's another problem where the compiler tells me I need a static lifetime, but I shouldn't need one.
The idea here is that World owns many ViewableObjects, which it keeps in a Vec.
ViewableObjects are dyn trait objects of various types. Here in the playground version the only ViewableObject type is Primitive.

Up at the top, something external calls processudppkt, which takes in "decoder", which is of an impl of DecoderUser. That object owns World.

What's strange about this is is that the error reported is about "decoder". But the problem seems to be further down. If the ViewableObject part is done with a plain structure instead of a trait, the compiler is happy.

Is there some way to get more info from the compiler about these deep errors? "cargo build --verbose" does not seem to help. Thanks.

error[E0477]: the type `PacketLogDecoder<'a>` does not fulfill the required lifetime
  --> src/lib.rs:34:29
   |
34 |     DecoderUser::udpindummy(decoder); // pass to decoder
   |                             ^^^^^^^
   |
   = note: type must satisfy the static lifetime

error: aborting due to previous error

impl dyn Trait { ... } has an implicit 'static bound, because of default trait object lifetimes.

You can override this by adding any different lifetime bound, for example:

impl dyn DecoderUser + '_ {
    pub fn udpindummy(&mut self) {}
}
2 Likes

Ah. Thanks. My big program compiles now. I need to become familiar with more of the special cases like that.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.