From 8fa3dfdb5dfbc0d77af4355fe65d7b127e5c7545 Mon Sep 17 00:00:00 2001 From: Casper Date: Thu, 29 Apr 2021 18:23:22 -0400 Subject: [PATCH] Introduce new_from_vec in Rust (also fix formatting) (#6599) * Introduce new_from_vec in Rust (also fix formatting) Also, rename `new_with_capacity` to `with_capacity` to match how `Vec` does it. * bump rust version * mut_finished_buffer Co-authored-by: Casper Neo --- rust/flatbuffers/Cargo.toml | 2 +- rust/flatbuffers/src/array.rs | 9 +++------ rust/flatbuffers/src/builder.rs | 34 +++++++++++++++++++++++++-------- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/rust/flatbuffers/Cargo.toml b/rust/flatbuffers/Cargo.toml index ee0f7cc30..1a425d2fc 100644 --- a/rust/flatbuffers/Cargo.toml +++ b/rust/flatbuffers/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "flatbuffers" -version = "0.8.5" +version = "0.8.6" edition = "2018" authors = ["Robert Winslow ", "FlatBuffers Maintainers"] license = "Apache-2.0" diff --git a/rust/flatbuffers/src/array.rs b/rust/flatbuffers/src/array.rs index c93876bf5..f00a56036 100644 --- a/rust/flatbuffers/src/array.rs +++ b/rust/flatbuffers/src/array.rs @@ -15,14 +15,11 @@ */ use crate::follow::Follow; -use crate::{ - vector::{SafeSliceAccess, VectorIter}, - EndianScalar, -}; +use crate::vector::VectorIter; +use crate::EndianScalar; use std::fmt::{Debug, Formatter, Result}; use std::marker::PhantomData; use std::mem::size_of; -use std::slice::from_raw_parts; #[derive(Copy, Clone)] pub struct Array<'a, T: 'a, const N: usize>(&'a [u8], PhantomData); @@ -38,7 +35,7 @@ where } #[allow(clippy::len_without_is_empty)] -#[allow(clippy::from_over_into)] // TODO(caspern): Go from From to Into. +#[allow(clippy::from_over_into)] // TODO(caspern): Go from From to Into. impl<'a, T: 'a, const N: usize> Array<'a, T, N> { #[inline(always)] pub fn new(buf: &'a [u8]) -> Self { diff --git a/rust/flatbuffers/src/builder.rs b/rust/flatbuffers/src/builder.rs index 3b536737f..bf1ad029d 100644 --- a/rust/flatbuffers/src/builder.rs +++ b/rust/flatbuffers/src/builder.rs @@ -62,24 +62,32 @@ pub struct FlatBufferBuilder<'fbb> { impl<'fbb> FlatBufferBuilder<'fbb> { /// Create a FlatBufferBuilder that is ready for writing. pub fn new() -> Self { - Self::new_with_capacity(0) + Self::with_capacity(0) + } + #[deprecated(note = "replaced with `with_capacity`", since = "0.8.5")] + pub fn new_with_capacity(size: usize) -> Self { + Self::with_capacity(size) } - /// Create a FlatBufferBuilder that is ready for writing, with a /// ready-to-use capacity of the provided size. /// /// The maximum valid value is `FLATBUFFERS_MAX_BUFFER_SIZE`. - pub fn new_with_capacity(size: usize) -> Self { + pub fn with_capacity(size: usize) -> Self { + Self::from_vec(vec![0; size]) + } + /// Create a FlatBufferBuilder that is ready for writing, reusing + /// an existing vector. + pub fn from_vec(buffer: Vec) -> Self { // we need to check the size here because we create the backing buffer // directly, bypassing the typical way of using grow_owned_buf: assert!( - size <= FLATBUFFERS_MAX_BUFFER_SIZE, + buffer.len() <= FLATBUFFERS_MAX_BUFFER_SIZE, "cannot initialize buffer bigger than 2 gigabytes" ); - + let head = buffer.len(); FlatBufferBuilder { - owned_buf: vec![0u8; size], - head: size, + owned_buf: buffer, + head, field_locs: Vec::new(), written_vtable_revpos: Vec::new(), @@ -407,11 +415,21 @@ impl<'fbb> FlatBufferBuilder<'fbb> { } /// Get the byte slice for the data that has been written after a call to /// one of the `finish` functions. + /// # Panics + /// Panics if the buffer is not finished. #[inline] pub fn finished_data(&self) -> &[u8] { self.assert_finished("finished_bytes cannot be called when the buffer is not yet finished"); &self.owned_buf[self.head..] } + /// Returns a mutable view of a finished buffer and location of where the flatbuffer starts. + /// Note that modifying the flatbuffer data may corrupt it. + /// # Panics + /// Panics if the flatbuffer is not finished. + #[inline] + pub fn mut_finished_buffer(&mut self) -> (&mut [u8], usize) { + (&mut self.owned_buf, self.head) + } /// Assert that a field is present in the just-finished Table. /// /// This is somewhat low-level and is mostly used by the generated code. @@ -765,6 +783,6 @@ fn padding_bytes(buf_size: usize, scalar_size: usize) -> usize { impl<'fbb> Default for FlatBufferBuilder<'fbb> { fn default() -> Self { - Self::new_with_capacity(0) + Self::with_capacity(0) } }