forked from BigfootDev/flatbuffers
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 <cneo@google.com>
This commit is contained in:
@@ -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<u8>) -> 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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user