summaryrefslogtreecommitdiff
path: root/rust
diff options
context:
space:
mode:
authorKamil Rojewski <kamil.rojewski@gmail.com>2020-09-28 18:45:48 +0200
committerGitHub <noreply@github.com>2020-09-28 09:45:48 -0700
commit2eedc769d55cfd538208bff8b3ad5f66bd9239d9 (patch)
tree97c55273b1958c357f9ab35438266a3ce1e113dc /rust
parentab01ae1620682ae214522d0be221685643fe6cf3 (diff)
downloadflatbuffers-2eedc769d55cfd538208bff8b3ad5f66bd9239d9.tar.gz
flatbuffers-2eedc769d55cfd538208bff8b3ad5f66bd9239d9.tar.bz2
flatbuffers-2eedc769d55cfd538208bff8b3ad5f66bd9239d9.zip
possibility to create a vector from an iterator (#6135)
Diffstat (limited to 'rust')
-rw-r--r--rust/flatbuffers/src/builder.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/rust/flatbuffers/src/builder.rs b/rust/flatbuffers/src/builder.rs
index 702b07cb..5c63f934 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::<UOffsetT>(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<T: Push + Copy>(
+ &mut self,
+ items: impl ExactSizeIterator<Item = T> + DoubleEndedIterator,
+ ) -> WIPOffset<Vector<'fbb, T::Output>> {
+ 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::<UOffsetT>(len as UOffsetT).value())
+ }
+
/// Set whether default values are stored.
///
/// In order to save space, fields that are set to their default value