0

I have some flatbuffers IDL:

table Filter {
  text: string (required);
}

which generates the following .rs:

...
pub struct Filter<'a> {
  pub _tab: flatbuffers::Table<'a>,
}
...

I'm trying to add some trait impl for it:

pub trait TFilter {
    fn as_any(&self) -> &dyn Any;
    fn as_any_mut(&mut self) -> &mut dyn Any;
}
...
impl TFilter for crate::schema_generated::Filter<'_> {
    fn as_any(&self) -> &dyn Any { self }
    fn as_any_mut(&mut self) -> &mut dyn Any { self }
}

I'm getting the following:

cannot infer an appropriate lifetime due to conflicting requirements

note: ...so that the type `schema_generated::Filter<'_>` will meet its required lifetime bounds
note: but, the lifetime must be valid for the static lifetime...
note: ...so that the expression is assignable

If i add lifetimes:

// flatbuffers-based impl
impl<'a> TFilter for crate::schema_generated::Filter<'a> {
    fn as_any(&self) -> &'a dyn Any { self }
    fn as_any_mut(&mut self) -> &'a mut dyn Any { self }
}

i'm getting the following:

the type `schema_generated::Filter<'a>` does not fulfill the required lifetime

note: type must satisfy the static lifetimerustc(E0477)

I'm not sure i understand that is meant. If i understand correctly i need to mark as_any living no longer than crate::schema_generated::Filter, is it correct?

Any clue? Where did 'static requirement come from?

PS. I've found the following, but i'm not sure if it's related.

4ntoine
  • 19,816
  • 21
  • 96
  • 220
  • @E_net4thecurator yes, but can you provide a small example for my case? BTW that answer is not accepted.. – 4ntoine Mar 19 '21 at 12:00
  • There's no example to make. The answer there is correct that you can't use `Any` for types with lifetimes, `Any` requires `'static`. You *could* `impl TFilter for Filter<'static>`, but that's probably not helpful. – kmdreko Mar 19 '21 at 17:47
  • As the answer to the other question also says, *without further context, it is difficult to say.* It seems you are trying to use `Any` to solve some problem you encountered with `Filter`, but if `Filter` contains lifetimes, `Any` simply is not the solution. Perhaps there are other ways you could achieve the same thing without using `Any`? – trent Mar 20 '21 at 15:30

0 Answers0