diff --git a/rust/flatbuffers/src/builder.rs b/rust/flatbuffers/src/builder.rs index 702b07cb5..5c63f9345 100644 --- a/rust/flatbuffers/src/builder.rs +++ b/rust/flatbuffers/src/builder.rs @@ -20,6 +20,7 @@ use std::cmp::max; use std::marker::PhantomData; use std::ptr::write_bytes; use std::slice::from_raw_parts; +use std::iter::{DoubleEndedIterator, ExactSizeIterator}; use endian_scalar::{emplace_scalar, read_scalar_at}; use primitives::*; @@ -328,6 +329,24 @@ impl<'fbb> FlatBufferBuilder<'fbb> { WIPOffset::new(self.push::(items.len() as UOffsetT).value()) } + /// Create a vector of Push-able objects. + /// + /// Speed-sensitive users may wish to reduce memory usage by creating the + /// vector manually: use `start_vector`, `push`, and `end_vector`. + #[inline] + pub fn create_vector_from_iter( + &mut self, + items: impl ExactSizeIterator + DoubleEndedIterator, + ) -> WIPOffset> { + let elem_size = T::size(); + let len = items.len(); + self.align(len * elem_size, T::alignment().max_of(SIZE_UOFFSET)); + for item in items.rev() { + self.push(item); + } + WIPOffset::new(self.push::(len as UOffsetT).value()) + } + /// Set whether default values are stored. /// /// In order to save space, fields that are set to their default value