Skip to content

Commit 83df0dd

Browse files
committed
rust: Add Follow with a different lifetime
This adds a FollowWith trait, which allows accessing a table type with a different lifetime in generic code. This is necessary for writing generic containers that own flatbuffers. I'm using this (with a flatbuffers fork that already includes this change) at https://github.com/frc971/971-Robot-Code/blob/f4cae52cf7d990572e157ced324aee43cf89c523/aos/flatbuffers.rs. I would be happy to move this to flatbuffers itself if there's interested, but I think generating the small amount of code in this commit to allow writing wrappers like that is independently valuable. I think this is what was discussed in #5749, for reference.
1 parent 8804619 commit 83df0dd

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

rust/flatbuffers/src/follow.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@ impl<'a, T: Follow<'a>> Follow<'a> for FollowStart<T> {
5353
T::follow(buf, loc)
5454
}
5555
}
56+
57+
pub trait FollowWith<'a> {
58+
type Inner: Follow<'a>;
59+
}

rust/flatbuffers/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub use crate::builder::FlatBufferBuilder;
5151
pub use crate::endian_scalar::{
5252
byte_swap_f32, byte_swap_f64, emplace_scalar, read_scalar, read_scalar_at, EndianScalar,
5353
};
54-
pub use crate::follow::{Follow, FollowStart};
54+
pub use crate::follow::{Follow, FollowStart, FollowWith};
5555
pub use crate::primitives::*;
5656
pub use crate::push::Push;
5757
pub use crate::table::{buffer_has_identifier, Table};

src/idl_gen_rust.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,10 @@ class RustGenerator : public BaseGenerator {
16551655
code_ += " }";
16561656
code_ += "}";
16571657
code_ += "";
1658+
code_ += "impl<'a, 'b> flatbuffers::FollowWith<'a> for {{STRUCT_TY}}<'b> {";
1659+
code_ += " type Inner = {{STRUCT_TY}}<'a>;";
1660+
code_ += "}";
1661+
code_ += "";
16581662
code_ += "impl<'a> {{STRUCT_TY}}<'a> {";
16591663

16601664
// Generate field id constants.

0 commit comments

Comments
 (0)