I've generated an Index
struct out of flatbuffers IDL schema and the building code was generated by flatc
compiler as follows:
#[inline]
pub fn get_root_as_index<'a>(buf: &'a [u8]) -> Index<'a> {
flatbuffers::get_root::<Index<'a>>(buf)
}
It means the created Index
lives only in 'a
and i have to care about who owns buf
. I'm trying to pass ownership to Index
together with buf
and i don't understand how to make it idiomatic in Rust:
pub struct Arena<'a> {
filter_factory: Box<dyn TFilterFactory<'a> + 'a>,
matcher: Box<dyn TMatcher<'a> + 'a>,
}
pub struct Context<'a> {
buffer: Vec<u8>,
arena: Arena<'a>
}
impl<'a> Arena<'a> {
pub fn new(file_path: &'a str) -> Context {
let mut file = File::open(file_path).unwrap();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).expect("File reading failed");
let index = get_root_as_index(&buffer[..]);
Context{
buffer,
arena: Arena {
filter_factory: Box::new(FilterFactory::new()),
matcher: Box::new(Matcher::new(
Box::new(FlatBuffersIndex::new(index)),
Box::new(FiltersLazyDataRegistry::new())
))
}
}
}
}
How can i pass the ownership of Index
together with buf
so i don't have to care about buf
(assuming the owner of Index
also owns buf
)?
I've tried to make a buffer a member of Arena
struct, borrow it when creating an Index
instance, but later i can't move this new struct.
PS. Ideally i'd love to have an Index
that owns it's buffer (so i can care about only 1 instance owner), but that's not how the code is generated.