4

Why does FlatBufferBuilder from the Rust flatbuffer library have a lifetime associated with it?

The lifetime makes it difficult to use in structs, as I then need to add a lifetime to them. I see that the lifetime is used for a few methods, but it seems like those could use the lifetime of self instead.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
cberner
  • 3,000
  • 3
  • 22
  • 34
  • Have you tried using `'static` as a lifetime? Then you wouldn't need to parameterize whatever embeds it. – Matthieu M. Jun 21 '19 at 06:37
  • See [this on unused lifetime parameters](https://doc.rust-lang.org/std/marker/struct.PhantomData.html#unused-lifetime-parameters). It still does not explain what in the `FlatFileBuilder` struct requires the lifetime though... – Bruno Grieder Jun 21 '19 at 06:48

1 Answers1

1

New to Rust myself so was trying & struggling to deal with this too. From looking at flatbuffers's builder.rs code, seems to be due to the fact that methods like below return references to data inside the builder, which needs to know the builder's lifetime to be valid.

pub fn create_string<'a: 'b, 'b>(&'a mut self, s: &'b str) -> WIPOffset<&'fbb str>

Using 'static like Matthieu mentioned above appears to enable me to not add more explicit lifetimes to structs that have a member FlatBufferBuilder. But still struggling to understand why putting 'static on a struct that isn't static is working ...

kdlee
  • 11
  • 2