bulk code format fix (#8707)

This commit is contained in:
Derek Bailey
2025-09-23 21:50:27 -07:00
committed by GitHub
parent 0e047869da
commit caf3b494db
559 changed files with 38871 additions and 31276 deletions

View File

@@ -223,9 +223,7 @@ impl<'fbb, A: Allocator> FlatBufferBuilder<'fbb, A> {
/// new object.
pub fn reset(&mut self) {
// memset only the part of the buffer that could be dirty:
self.allocator[self.head.range_to_end()]
.iter_mut()
.for_each(|x| *x = 0);
self.allocator[self.head.range_to_end()].iter_mut().for_each(|x| *x = 0);
self.head = ReverseIndex::end();
self.written_vtable_revpos.clear();
@@ -627,15 +625,13 @@ impl<'fbb, A: Allocator> FlatBufferBuilder<'fbb, A> {
}
}
let new_vt_bytes = &self.allocator[vt_start_pos.range_to(vt_end_pos)];
let found = self
.written_vtable_revpos
.binary_search_by(|old_vtable_revpos: &UOffsetT| {
let old_vtable_pos = self.allocator.len() - *old_vtable_revpos as usize;
// Safety:
// Already written vtables are valid by construction
let old_vtable = unsafe { VTable::init(&self.allocator, old_vtable_pos) };
new_vt_bytes.cmp(old_vtable.as_bytes())
});
let found = self.written_vtable_revpos.binary_search_by(|old_vtable_revpos: &UOffsetT| {
let old_vtable_pos = self.allocator.len() - *old_vtable_revpos as usize;
// Safety:
// Already written vtables are valid by construction
let old_vtable = unsafe { VTable::init(&self.allocator, old_vtable_pos) };
new_vt_bytes.cmp(old_vtable.as_bytes())
});
let final_vtable_revpos = match found {
Ok(i) => {
// The new vtable is a duplicate so clear it.
@@ -680,9 +676,7 @@ impl<'fbb, A: Allocator> FlatBufferBuilder<'fbb, A> {
#[inline]
fn grow_allocator(&mut self) {
let starting_active_size = self.used_space();
self.allocator
.grow_downwards()
.expect("Flatbuffer allocation failure");
self.allocator.grow_downwards().expect("Flatbuffer allocation failure");
let ending_active_size = self.used_space();
debug_assert_eq!(starting_active_size, ending_active_size);
@@ -708,11 +702,7 @@ impl<'fbb, A: Allocator> FlatBufferBuilder<'fbb, A> {
// for the size prefix:
let b = if size_prefixed { SIZE_UOFFSET } else { 0 };
// for the file identifier (a string that is not zero-terminated):
let c = if file_identifier.is_some() {
FILE_IDENTIFIER_LENGTH
} else {
0
};
let c = if file_identifier.is_some() { FILE_IDENTIFIER_LENGTH } else { 0 };
a + b + c
};
@@ -767,10 +757,7 @@ impl<'fbb, A: Allocator> FlatBufferBuilder<'fbb, A> {
if self.unused_ready_space() >= want {
return want;
}
assert!(
want <= FLATBUFFERS_MAX_BUFFER_SIZE,
"cannot grow buffer beyond 2 gigabytes"
);
assert!(want <= FLATBUFFERS_MAX_BUFFER_SIZE, "cannot grow buffer beyond 2 gigabytes");
while self.unused_ready_space() < want {
self.grow_allocator();

View File

@@ -56,8 +56,8 @@ pub use crate::push::{Push, PushAlignment};
pub use crate::table::{buffer_has_identifier, Table};
pub use crate::vector::{follow_cast_ref, Vector, VectorIter};
pub use crate::verifier::{
ErrorTraceDetail, InvalidFlatbuffer, SimpleToVerifyInSlice, TableVerifier, Verifiable, Verifier,
VerifierOptions,
ErrorTraceDetail, InvalidFlatbuffer, SimpleToVerifyInSlice, TableVerifier, Verifiable,
Verifier, VerifierOptions,
};
pub use crate::vtable::field_index_to_field_offset;
pub use bitflags;

View File

@@ -31,11 +31,7 @@ impl<'a, T: 'a> Default for Vector<'a, T> {
fn default() -> Self {
// Static, length 0 vector.
// Note that derived default causes UB due to issues in read_scalar_at /facepalm.
Self(
&[0; core::mem::size_of::<UOffsetT>()],
0,
Default::default(),
)
Self(&[0; core::mem::size_of::<UOffsetT>()], 0, Default::default())
}
}
@@ -123,11 +119,11 @@ impl<'a, T: Follow<'a> + 'a> Vector<'a, T> {
Ordering::Equal => return Some(value),
Ordering::Less => left = mid + 1,
Ordering::Greater => {
if mid == 0 {
return None;
}
right = mid - 1;
},
if mid == 0 {
return None;
}
right = mid - 1;
}
}
}
@@ -209,12 +205,7 @@ impl<'a, T: 'a> VectorIter<'a, T> {
///
#[inline]
pub unsafe fn from_slice(buf: &'a [u8], items_num: usize) -> Self {
VectorIter {
buf,
loc: 0,
remaining: items_num,
phantom: PhantomData,
}
VectorIter { buf, loc: 0, remaining: items_num, phantom: PhantomData }
}
}

View File

@@ -20,18 +20,9 @@ use std::error::Error;
/// the other errors should not be producible by correct flatbuffers implementations.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ErrorTraceDetail {
VectorElement {
index: usize,
position: usize,
},
TableField {
field_name: Cow<'static, str>,
position: usize,
},
UnionVariant {
variant: Cow<'static, str>,
position: usize,
},
VectorElement { index: usize, position: usize },
TableField { field_name: Cow<'static, str>, position: usize },
UnionVariant { variant: Cow<'static, str>, position: usize },
}
#[derive(PartialEq, Eq, Default, Debug, Clone)]
@@ -100,33 +91,18 @@ impl Error for InvalidFlatbuffer {
impl core::fmt::Display for InvalidFlatbuffer {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
InvalidFlatbuffer::MissingRequiredField {
required,
error_trace,
} => {
InvalidFlatbuffer::MissingRequiredField { required, error_trace } => {
writeln!(f, "Missing required field `{}`.\n{}", required, error_trace)?;
}
InvalidFlatbuffer::InconsistentUnion {
field,
field_type,
error_trace,
} => {
InvalidFlatbuffer::InconsistentUnion { field, field_type, error_trace } => {
writeln!(
f,
"Exactly one of union discriminant (`{}`) and value (`{}`) are present.\n{}",
field_type, field, error_trace
)?;
}
InvalidFlatbuffer::Utf8Error {
error,
range,
error_trace,
} => {
writeln!(
f,
"Utf8 error for string in {:?}: {}\n{}",
range, error, error_trace
)?;
InvalidFlatbuffer::Utf8Error { error, range, error_trace } => {
writeln!(f, "Utf8 error for string in {:?}: {}\n{}", range, error, error_trace)?;
}
InvalidFlatbuffer::MissingNullTerminator { range, error_trace } => {
writeln!(
@@ -135,11 +111,7 @@ impl core::fmt::Display for InvalidFlatbuffer {
range.start, range.end, error_trace
)?;
}
InvalidFlatbuffer::Unaligned {
position,
unaligned_type,
error_trace,
} => {
InvalidFlatbuffer::Unaligned { position, unaligned_type, error_trace } => {
writeln!(
f,
"Type `{}` at position {} is unaligned.\n{}",
@@ -153,11 +125,7 @@ impl core::fmt::Display for InvalidFlatbuffer {
range.start, range.end, error_trace
)?;
}
InvalidFlatbuffer::SignedOffsetOutOfBounds {
soffset,
position,
error_trace,
} => {
InvalidFlatbuffer::SignedOffsetOutOfBounds { soffset, position, error_trace } => {
writeln!(
f,
"Signed offset at position {} has value {} which points out of bounds.\n{}",
@@ -190,10 +158,7 @@ impl core::fmt::Display for ErrorTrace {
index, position
)?;
}
TableField {
field_name,
position,
} => {
TableField { field_name, position } => {
writeln!(
f,
"\twhile verifying table field `{}` at position {:?}",
@@ -217,10 +182,7 @@ pub type Result<T> = core::result::Result<T, InvalidFlatbuffer>;
impl InvalidFlatbuffer {
fn new_range_oob<T>(start: usize, end: usize) -> Result<T> {
Err(Self::RangeOutOfBounds {
range: Range { start, end },
error_trace: Default::default(),
})
Err(Self::RangeOutOfBounds { range: Range { start, end }, error_trace: Default::default() })
}
pub fn new_inconsistent_union<T>(
field: impl Into<Cow<'static, str>>,
@@ -260,13 +222,7 @@ fn append_trace<T>(mut res: Result<T>, d: ErrorTraceDetail) -> Result<T> {
/// Adds a TableField trace detail if `res` is a data error.
fn trace_field<T>(res: Result<T>, field_name: Cow<'static, str>, position: usize) -> Result<T> {
append_trace(
res,
ErrorTraceDetail::TableField {
field_name,
position,
},
)
append_trace(res, ErrorTraceDetail::TableField { field_name, position })
}
/// Adds a TableField trace detail if `res` is a data error.
@@ -315,13 +271,7 @@ pub struct Verifier<'opts, 'buf> {
impl<'opts, 'buf> Verifier<'opts, 'buf> {
pub fn new(opts: &'opts VerifierOptions, buffer: &'buf [u8]) -> Self {
Self {
opts,
buffer,
depth: 0,
num_tables: 0,
apparent_size: 0,
}
Self { opts, buffer, depth: 0, num_tables: 0, apparent_size: 0 }
}
/// Resets verifier internal state.
#[inline]
@@ -435,12 +385,7 @@ impl<'opts, 'buf> Verifier<'opts, 'buf> {
if self.depth > self.opts.max_depth {
return Err(InvalidFlatbuffer::DepthLimitReached);
}
Ok(TableVerifier {
pos: table_pos,
vtable: vtable_pos,
vtable_len,
verifier: self,
})
Ok(TableVerifier { pos: table_pos, vtable: vtable_pos, vtable_len, verifier: self })
}
/// Runs the union variant's type's verifier assuming the variant is at the given position,
@@ -451,13 +396,7 @@ impl<'opts, 'buf> Verifier<'opts, 'buf> {
position: usize,
) -> Result<()> {
let res = T::run_verifier(self, position);
append_trace(
res,
ErrorTraceDetail::UnionVariant {
variant: variant.into(),
position,
},
)
append_trace(res, ErrorTraceDetail::UnionVariant { variant: variant.into(), position })
}
}
@@ -501,11 +440,7 @@ impl<'ver, 'opts, 'buf> TableVerifier<'ver, 'opts, 'buf> {
required: bool,
) -> Result<Self> {
if let Some(field_pos) = self.deref(field)? {
trace_field(
T::run_verifier(self.verifier, field_pos),
field_name.into(),
field_pos,
)?;
trace_field(T::run_verifier(self.verifier, field_pos), field_name.into(), field_pos)?;
return Ok(self);
}
if required {
@@ -545,11 +480,7 @@ impl<'ver, 'opts, 'buf> TableVerifier<'ver, 'opts, 'buf> {
}
}
(Some(k), Some(v)) => {
trace_field(
Key::run_verifier(self.verifier, k),
key_field_name.into(),
k,
)?;
trace_field(Key::run_verifier(self.verifier, k), key_field_name.into(), k)?;
// Safety:
// Run verifier on `k` above
let discriminant = unsafe { Key::follow(self.verifier.buffer, k) };
@@ -560,7 +491,10 @@ impl<'ver, 'opts, 'buf> TableVerifier<'ver, 'opts, 'buf> {
)?;
Ok(self)
}
_ => InvalidFlatbuffer::new_inconsistent_union(key_field_name.into(), val_field_name.into()),
_ => InvalidFlatbuffer::new_inconsistent_union(
key_field_name.into(),
val_field_name.into(),
),
}
}
pub fn finish(self) -> &'ver mut Verifier<'opts, 'buf> {
@@ -642,11 +576,7 @@ impl<T: Verifiable> Verifiable for Vector<'_, ForwardsUOffset<T>> {
let range = verify_vector_range::<ForwardsUOffset<T>>(v, pos)?;
let size = core::mem::size_of::<ForwardsUOffset<T>>();
for (i, element_pos) in range.step_by(size).enumerate() {
trace_elem(
<ForwardsUOffset<T>>::run_verifier(v, element_pos),
i,
element_pos,
)?;
trace_elem(<ForwardsUOffset<T>>::run_verifier(v, element_pos), i, element_pos)?;
}
Ok(())
}

View File

@@ -48,10 +48,7 @@ impl<'a> MapBuilder<'a> {
self.builder.push_key(key);
// Nested vector.
let start = Some(self.builder.values.len());
VectorBuilder {
builder: self.builder,
start,
}
VectorBuilder { builder: self.builder, start }
}
/// Starts a nested map which that will be pushed onto this map
/// with key `key` when it is dropped.
@@ -63,10 +60,7 @@ impl<'a> MapBuilder<'a> {
self.builder.push_key(key);
// Nested map.
let start = Some(self.builder.values.len());
MapBuilder {
builder: self.builder,
start,
}
MapBuilder { builder: self.builder, start }
}
/// `end_map` sorts the map by key and writes it to the buffer. This happens anyway
/// when the map builder is dropped.

View File

@@ -35,12 +35,8 @@ macro_rules! push_slice {
S: AsRef<[T]>,
{
let mut value = Value::$new_vec(xs.as_ref().len());
let mut width = xs
.as_ref()
.iter()
.map(|x| BitWidth::from((*x).into()))
.max()
.unwrap_or_default();
let mut width =
xs.as_ref().iter().map(|x| BitWidth::from((*x).into())).max().unwrap_or_default();
if !value.is_fixed_length_vector() {
let length = Value::UInt(xs.as_ref().len() as u64);
width = std::cmp::max(width, length.width_or_child_width());
@@ -135,16 +131,8 @@ impl Default for Builder {
impl<'a> Builder {
pub fn new(opts: BuilderOptions) -> Self {
let key_pool = if opts.contains(BuilderOptions::SHARE_KEYS) {
Some(vec![])
} else {
None
};
Builder {
key_pool,
values: Vec::new(),
buffer: Vec::new(),
}
let key_pool = if opts.contains(BuilderOptions::SHARE_KEYS) { Some(vec![]) } else { None };
Builder { key_pool, values: Vec::new(), buffer: Vec::new() }
}
/// Shows the internal flexbuffer. It will either be empty or populated with the most
/// recently built flexbuffer.
@@ -167,10 +155,7 @@ impl<'a> Builder {
}
}
fn push_key(&mut self, key: &str) {
debug_assert!(
key.bytes().all(|b| b != b'\0'),
"Keys must not have internal nulls."
);
debug_assert!(key.bytes().all(|b| b != b'\0'), "Keys must not have internal nulls.");
// Search key pool if there is one.
let found = self.key_pool.as_ref().map(|pool| {
pool.binary_search_by(|&CachedKey(addr)| {
@@ -217,11 +202,7 @@ impl<'a> Builder {
store_value(&mut self.buffer, length, width);
let address = self.buffer.len();
self.buffer.extend_from_slice(xs);
Value::Reference {
fxb_type: FlexBufferType::Blob,
address,
child_width: width,
}
Value::Reference { fxb_type: FlexBufferType::Blob, address, child_width: width }
}
fn push_str(&mut self, x: &str) {
let mut string = self.store_blob(x.as_bytes());
@@ -261,18 +242,12 @@ impl<'a> Builder {
/// The exact Flexbuffer vector type is dynamically inferred.
pub fn start_vector(&'a mut self) -> VectorBuilder<'a> {
self.reset();
VectorBuilder {
builder: self,
start: None,
}
VectorBuilder { builder: self, start: None }
}
/// Resets the builder and builds a new flexbuffer with a map at the root.
pub fn start_map(&'a mut self) -> MapBuilder<'a> {
self.reset();
MapBuilder {
builder: self,
start: None,
}
MapBuilder { builder: self, start: None }
}
/// Resets the builder and builds a new flexbuffer with the pushed value at the root.
pub fn build_singleton<P: Pushable>(&mut self, p: P) {

View File

@@ -49,19 +49,13 @@ impl FlexbufferSerializer {
Ok(())
}
fn start_vector(&mut self) {
let previous_end = if self.nesting.is_empty() {
None
} else {
Some(self.builder.values.len())
};
let previous_end =
if self.nesting.is_empty() { None } else { Some(self.builder.values.len()) };
self.nesting.push(previous_end);
}
fn start_map(&mut self) {
let previous_end = if self.nesting.is_empty() {
None
} else {
Some(self.builder.values.len())
};
let previous_end =
if self.nesting.is_empty() { None } else { Some(self.builder.values.len()) };
self.nesting.push(previous_end);
}
fn end_vector(&mut self) -> Result<(), Error> {

View File

@@ -47,26 +47,10 @@ macro_rules! new_typed_vector {
let address = 0;
let child_width = W8;
match n {
2 => Value::Reference {
address,
child_width,
fxb_type: $v2,
},
3 => Value::Reference {
address,
child_width,
fxb_type: $v3,
},
4 => Value::Reference {
address,
child_width,
fxb_type: $v4,
},
_ => Value::Reference {
address,
child_width,
fxb_type: $vn,
},
2 => Value::Reference { address, child_width, fxb_type: $v2 },
3 => Value::Reference { address, child_width, fxb_type: $v3 },
4 => Value::Reference { address, child_width, fxb_type: $v4 },
_ => Value::Reference { address, child_width, fxb_type: $vn },
}
}
};
@@ -74,40 +58,14 @@ macro_rules! new_typed_vector {
impl Value {
pub fn new_vector() -> Self {
Value::Reference {
address: 0,
child_width: W8,
fxb_type: Vector,
}
Value::Reference { address: 0, child_width: W8, fxb_type: Vector }
}
pub fn new_map() -> Self {
Value::Reference {
address: 0,
child_width: W8,
fxb_type: Map,
}
Value::Reference { address: 0, child_width: W8, fxb_type: Map }
}
new_typed_vector!(
new_int_vector,
VectorInt2,
VectorInt3,
VectorInt4,
VectorInt
);
new_typed_vector!(
new_uint_vector,
VectorUInt2,
VectorUInt3,
VectorUInt4,
VectorUInt
);
new_typed_vector!(
new_float_vector,
VectorFloat2,
VectorFloat3,
VectorFloat4,
VectorFloat
);
new_typed_vector!(new_int_vector, VectorInt2, VectorInt3, VectorInt4, VectorInt);
new_typed_vector!(new_uint_vector, VectorUInt2, VectorUInt3, VectorUInt4, VectorUInt);
new_typed_vector!(new_float_vector, VectorFloat2, VectorFloat3, VectorFloat4, VectorFloat);
pub fn fxb_type(&self) -> FlexBufferType {
match *self {
Value::Null => Null,
@@ -264,11 +222,7 @@ where
// Note that VectorString is deprecated for writing
_ => return Value::new_vector(),
};
Value::Reference {
address: 0,
child_width: W8,
fxb_type: vector_type,
}
Value::Reference { address: 0, child_width: W8, fxb_type: vector_type }
}
#[inline]
@@ -299,9 +253,6 @@ pub fn store_value(buffer: &mut Vec<u8>, mut value: Value, width: BitWidth) {
_ => unreachable!("Variant not considered: {:?}", value),
};
write_result.unwrap_or_else(|err| {
panic!(
"Error writing value {:?} with width {:?}: {:?}",
value, width, err
)
panic!("Error writing value {:?} with width {:?}: {:?}", value, width, err)
});
}

View File

@@ -38,19 +38,13 @@ impl<'a> VectorBuilder<'a> {
#[inline]
pub fn start_vector(&mut self) -> VectorBuilder {
let start = Some(self.builder.values.len());
VectorBuilder {
builder: self.builder,
start,
}
VectorBuilder { builder: self.builder, start }
}
/// Starts a nested map that will be pushed onto this vector when it is dropped.
#[inline]
pub fn start_map(&mut self) -> MapBuilder {
let start = Some(self.builder.values.len());
MapBuilder {
builder: self.builder,
start,
}
MapBuilder { builder: self.builder, start }
}
/// `end_vector` determines the type of the vector and writes it to the buffer.
/// This will happen automatically if the VectorBuilder is dropped.

View File

@@ -153,10 +153,7 @@ impl<'de> VariantAccess<'de> for Reader<&'de [u8]> {
V: Visitor<'de>,
{
let m = self.get_map()?;
visitor.visit_map(MapAccessor {
keys: m.keys_vector().iter(),
vals: m.iter_values(),
})
visitor.visit_map(MapAccessor { keys: m.keys_vector().iter(), vals: m.iter_values() })
}
}
@@ -190,10 +187,8 @@ impl<'de> Deserializer<'de> for Reader<&'de [u8]> {
(Blob, _) => visitor.visit_borrowed_bytes(self.get_blob()?.0),
(Map, _) => {
let m = self.get_map()?;
visitor.visit_map(MapAccessor {
keys: m.keys_vector().iter(),
vals: m.iter_values(),
})
visitor
.visit_map(MapAccessor { keys: m.keys_vector().iter(), vals: m.iter_values() })
}
(ty, _) if ty.is_vector() => visitor.visit_seq(self.as_vector().iter()),
(ty, bw) => unreachable!("TODO deserialize_any {:?} {:?}.", ty, bw),

View File

@@ -27,11 +27,7 @@ pub struct ReaderIterator<B> {
impl<B: Buffer> ReaderIterator<B> {
pub(super) fn new(reader: VectorReader<B>) -> Self {
let end = reader.len();
ReaderIterator {
reader,
front: 0,
end,
}
ReaderIterator { reader, front: 0, end }
}
}

View File

@@ -34,10 +34,7 @@ pub struct MapReader<B> {
impl<B: Buffer> Clone for MapReader<B> {
fn clone(&self) -> Self {
MapReader {
buffer: self.buffer.shallow_copy(),
..*self
}
MapReader { buffer: self.buffer.shallow_copy(), ..*self }
}
}
@@ -125,13 +122,7 @@ impl<B: Buffer> MapReader<B> {
.get(type_address)
.ok_or(Error::FlexbufferOutOfBounds)
.and_then(|&b| unpack_type(b))?;
Reader::new(
self.buffer.shallow_copy(),
data_address,
fxb_type,
width,
self.values_width,
)
Reader::new(self.buffer.shallow_copy(), data_address, fxb_type, width, self.values_width)
}
fn key_index(&self, k: &str) -> Result<Reader<B>, Error> {

View File

@@ -217,12 +217,7 @@ impl<B: Buffer> Reader<B> {
fxb_type = t;
}
}
Ok(Reader {
address,
fxb_type,
width,
buffer,
})
Ok(Reader { address, fxb_type, width, buffer })
}
/// Parses the flexbuffer from the given buffer. Assumes the flexbuffer root is the last byte
@@ -263,11 +258,7 @@ impl<B: Buffer> Reader<B> {
if let Some(len) = self.fxb_type.fixed_length_vector_length() {
len
} else if self.fxb_type.has_length_slot() && self.address >= self.width.n_bytes() {
read_usize(
&self.buffer,
self.address - self.width.n_bytes(),
self.width,
)
read_usize(&self.buffer, self.address - self.width.n_bytes(), self.width)
} else {
0
}
@@ -287,20 +278,14 @@ impl<B: Buffer> Reader<B> {
if self.fxb_type == ty {
Ok(())
} else {
Err(Error::UnexpectedFlexbufferType {
expected: ty,
actual: self.fxb_type,
})
Err(Error::UnexpectedFlexbufferType { expected: ty, actual: self.fxb_type })
}
}
fn expect_bw(&self, bw: BitWidth) -> Result<(), Error> {
if self.width == bw {
Ok(())
} else {
Err(Error::UnexpectedBitWidth {
expected: bw,
actual: self.width,
})
Err(Error::UnexpectedBitWidth { expected: bw, actual: self.width })
}
}
@@ -320,10 +305,8 @@ impl<B: Buffer> Reader<B> {
self.expect_bw(T::WIDTH)?;
}
let end = self.address + self.length() * std::mem::size_of::<T>();
let slice: &[u8] = self
.buffer
.get(self.address..end)
.ok_or(Error::FlexbufferOutOfBounds)?;
let slice: &[u8] =
self.buffer.get(self.address..end).ok_or(Error::FlexbufferOutOfBounds)?;
// `align_to` is required because the point of this function is to directly hand back a
// slice of scalars. This can fail because Rust's default allocator is not 16byte aligned
@@ -340,11 +323,7 @@ impl<B: Buffer> Reader<B> {
/// Otherwise Returns error.
pub fn get_bool(&self) -> Result<bool, Error> {
self.expect_type(FlexBufferType::Bool)?;
Ok(
self.buffer[self.address..self.address + self.width.n_bytes()]
.iter()
.any(|&b| b != 0),
)
Ok(self.buffer[self.address..self.address + self.width.n_bytes()].iter().any(|&b| b != 0))
}
/// Gets the length of the key if this type is a key.
@@ -387,9 +366,7 @@ impl<B: Buffer> Reader<B> {
/// is out of bounds.
pub fn get_str(&self) -> Result<B::BufferString, Error> {
self.expect_type(FlexBufferType::String)?;
let bytes = self
.buffer
.slice(self.address..self.address + self.length());
let bytes = self.buffer.slice(self.address..self.address + self.length());
Ok(bytes.ok_or(Error::ReadUsizeOverflowed)?.buffer_str()?)
}
@@ -425,22 +402,16 @@ impl<B: Buffer> Reader<B> {
/// address is out of bounds.
pub fn get_u64(&self) -> Result<u64, Error> {
self.expect_type(FlexBufferType::UInt)?;
let cursor = self
.buffer
.get(self.address..self.address + self.width.n_bytes());
let cursor = self.buffer.get(self.address..self.address + self.width.n_bytes());
match self.width {
BitWidth::W8 => cursor.map(|s| s[0] as u8).map(Into::into),
BitWidth::W16 => cursor
.and_then(|s| s.try_into().ok())
.map(<u16>::from_le_bytes)
.map(Into::into),
BitWidth::W32 => cursor
.and_then(|s| s.try_into().ok())
.map(<u32>::from_le_bytes)
.map(Into::into),
BitWidth::W64 => cursor
.and_then(|s| s.try_into().ok())
.map(<u64>::from_le_bytes),
BitWidth::W16 => {
cursor.and_then(|s| s.try_into().ok()).map(<u16>::from_le_bytes).map(Into::into)
}
BitWidth::W32 => {
cursor.and_then(|s| s.try_into().ok()).map(<u32>::from_le_bytes).map(Into::into)
}
BitWidth::W64 => cursor.and_then(|s| s.try_into().ok()).map(<u64>::from_le_bytes),
}
.ok_or(Error::FlexbufferOutOfBounds)
}
@@ -448,22 +419,16 @@ impl<B: Buffer> Reader<B> {
/// address is out of bounds.
pub fn get_i64(&self) -> Result<i64, Error> {
self.expect_type(FlexBufferType::Int)?;
let cursor = self
.buffer
.get(self.address..self.address + self.width.n_bytes());
let cursor = self.buffer.get(self.address..self.address + self.width.n_bytes());
match self.width {
BitWidth::W8 => cursor.map(|s| s[0] as i8).map(Into::into),
BitWidth::W16 => cursor
.and_then(|s| s.try_into().ok())
.map(<i16>::from_le_bytes)
.map(Into::into),
BitWidth::W32 => cursor
.and_then(|s| s.try_into().ok())
.map(<i32>::from_le_bytes)
.map(Into::into),
BitWidth::W64 => cursor
.and_then(|s| s.try_into().ok())
.map(<i64>::from_le_bytes),
BitWidth::W16 => {
cursor.and_then(|s| s.try_into().ok()).map(<i16>::from_le_bytes).map(Into::into)
}
BitWidth::W32 => {
cursor.and_then(|s| s.try_into().ok()).map(<i32>::from_le_bytes).map(Into::into)
}
BitWidth::W64 => cursor.and_then(|s| s.try_into().ok()).map(<i64>::from_le_bytes),
}
.ok_or(Error::FlexbufferOutOfBounds)
}
@@ -471,18 +436,13 @@ impl<B: Buffer> Reader<B> {
/// address is out of bounds, or if its a f16 or f8 (not currently supported).
pub fn get_f64(&self) -> Result<f64, Error> {
self.expect_type(FlexBufferType::Float)?;
let cursor = self
.buffer
.get(self.address..self.address + self.width.n_bytes());
let cursor = self.buffer.get(self.address..self.address + self.width.n_bytes());
match self.width {
BitWidth::W8 | BitWidth::W16 => return Err(Error::InvalidPackedType),
BitWidth::W32 => cursor
.and_then(|s| s.try_into().ok())
.map(f32_from_le_bytes)
.map(Into::into),
BitWidth::W64 => cursor
.and_then(|s| s.try_into().ok())
.map(f64_from_le_bytes),
BitWidth::W32 => {
cursor.and_then(|s| s.try_into().ok()).map(f32_from_le_bytes).map(Into::into)
}
BitWidth::W64 => cursor.and_then(|s| s.try_into().ok()).map(f64_from_le_bytes),
}
.ok_or(Error::FlexbufferOutOfBounds)
}
@@ -505,11 +465,9 @@ impl<B: Buffer> Reader<B> {
pub fn as_u64(&self) -> u64 {
match self.fxb_type {
FlexBufferType::UInt => self.get_u64().unwrap_or_default(),
FlexBufferType::Int => self
.get_i64()
.unwrap_or_default()
.try_into()
.unwrap_or_default(),
FlexBufferType::Int => {
self.get_i64().unwrap_or_default().try_into().unwrap_or_default()
}
FlexBufferType::Float => self.get_f64().unwrap_or_default() as u64,
FlexBufferType::String => {
if let Ok(s) = self.get_str() {
@@ -532,11 +490,9 @@ impl<B: Buffer> Reader<B> {
pub fn as_i64(&self) -> i64 {
match self.fxb_type {
FlexBufferType::Int => self.get_i64().unwrap_or_default(),
FlexBufferType::UInt => self
.get_u64()
.unwrap_or_default()
.try_into()
.unwrap_or_default(),
FlexBufferType::UInt => {
self.get_u64().unwrap_or_default().try_into().unwrap_or_default()
}
FlexBufferType::Float => self.get_f64().unwrap_or_default() as i64,
FlexBufferType::String => {
if let Ok(s) = self.get_str() {
@@ -590,10 +546,7 @@ impl<B: Buffer> Reader<B> {
if !self.fxb_type.is_vector() {
self.expect_type(FlexBufferType::Vector)?;
};
Ok(VectorReader {
reader: self.clone(),
length: self.length(),
})
Ok(VectorReader { reader: self.clone(), length: self.length() })
}
}

View File

@@ -29,19 +29,13 @@ pub struct VectorReader<B> {
impl<B: Buffer> Clone for VectorReader<B> {
fn clone(&self) -> Self {
VectorReader {
reader: self.reader.clone(),
..*self
}
VectorReader { reader: self.reader.clone(), ..*self }
}
}
impl<B: Buffer> Default for VectorReader<B> {
fn default() -> Self {
VectorReader {
reader: Reader::default(),
length: usize::default(),
}
VectorReader { reader: Reader::default(), length: usize::default() }
}
}

View File

@@ -84,12 +84,7 @@ pub unsafe fn get_field_integer<T: for<'a> Follow<'a, Inner = T> + PrimInt + Fro
if size_of::<T>() != get_type_size(field.type_().base_type()) {
return Err(FlatbufferError::FieldTypeMismatch(
std::any::type_name::<T>().to_string(),
field
.type_()
.base_type()
.variant_name()
.unwrap_or_default()
.to_string(),
field.type_().base_type().variant_name().unwrap_or_default().to_string(),
));
}
@@ -109,12 +104,7 @@ pub unsafe fn get_field_float<T: for<'a> Follow<'a, Inner = T> + Float>(
if size_of::<T>() != get_type_size(field.type_().base_type()) {
return Err(FlatbufferError::FieldTypeMismatch(
std::any::type_name::<T>().to_string(),
field
.type_()
.base_type()
.variant_name()
.unwrap_or_default()
.to_string(),
field.type_().base_type().variant_name().unwrap_or_default().to_string(),
));
}
@@ -134,12 +124,7 @@ pub unsafe fn get_field_string<'a>(
if field.type_().base_type() != BaseType::String {
return Err(FlatbufferError::FieldTypeMismatch(
String::from("String"),
field
.type_()
.base_type()
.variant_name()
.unwrap_or_default()
.to_string(),
field.type_().base_type().variant_name().unwrap_or_default().to_string(),
));
}
@@ -160,12 +145,7 @@ pub unsafe fn get_field_struct<'a>(
if field.type_().base_type() != BaseType::Obj {
return Err(FlatbufferError::FieldTypeMismatch(
String::from("Obj"),
field
.type_()
.base_type()
.variant_name()
.unwrap_or_default()
.to_string(),
field.type_().base_type().variant_name().unwrap_or_default().to_string(),
));
}
@@ -186,12 +166,7 @@ pub unsafe fn get_field_vector<'a, T: Follow<'a, Inner = T>>(
{
return Err(FlatbufferError::FieldTypeMismatch(
std::any::type_name::<T>().to_string(),
field
.type_()
.base_type()
.variant_name()
.unwrap_or_default()
.to_string(),
field.type_().base_type().variant_name().unwrap_or_default().to_string(),
));
}
@@ -210,12 +185,7 @@ pub unsafe fn get_field_table<'a>(
if field.type_().base_type() != BaseType::Obj {
return Err(FlatbufferError::FieldTypeMismatch(
String::from("Obj"),
field
.type_()
.base_type()
.variant_name()
.unwrap_or_default()
.to_string(),
field.type_().base_type().variant_name().unwrap_or_default().to_string(),
));
}
@@ -282,12 +252,7 @@ pub unsafe fn get_field_struct_in_struct<'a>(
if field.type_().base_type() != BaseType::Obj {
return Err(FlatbufferError::FieldTypeMismatch(
String::from("Obj"),
field
.type_()
.base_type()
.variant_name()
.unwrap_or_default()
.to_string(),
field.type_().base_type().variant_name().unwrap_or_default().to_string(),
));
}
@@ -442,15 +407,13 @@ pub unsafe fn set_field<T: EndianScalar>(
};
if buf.len() < field_loc.saturating_add(get_type_size(field_type)) {
return Err(FlatbufferError::VerificationError(
InvalidFlatbuffer::RangeOutOfBounds {
range: core::ops::Range {
start: field_loc,
end: field_loc.saturating_add(get_type_size(field_type)),
},
error_trace: Default::default(),
return Err(FlatbufferError::VerificationError(InvalidFlatbuffer::RangeOutOfBounds {
range: core::ops::Range {
start: field_loc,
end: field_loc.saturating_add(get_type_size(field_type)),
},
));
error_trace: Default::default(),
}));
}
// SAFETY: the buffer range was verified above.
@@ -488,49 +451,37 @@ pub unsafe fn set_string(
};
if buf.len() < field_loc + get_type_size(field_type) {
return Err(FlatbufferError::VerificationError(
InvalidFlatbuffer::RangeOutOfBounds {
range: core::ops::Range {
start: field_loc,
end: field_loc.saturating_add(get_type_size(field_type)),
},
error_trace: Default::default(),
return Err(FlatbufferError::VerificationError(InvalidFlatbuffer::RangeOutOfBounds {
range: core::ops::Range {
start: field_loc,
end: field_loc.saturating_add(get_type_size(field_type)),
},
));
error_trace: Default::default(),
}));
}
// SAFETY: the buffer range was verified above.
let string_loc = unsafe { deref_uoffset(buf, field_loc)? };
if buf.len() < string_loc.saturating_add(SIZE_UOFFSET) {
return Err(FlatbufferError::VerificationError(
InvalidFlatbuffer::RangeOutOfBounds {
range: core::ops::Range {
start: string_loc,
end: string_loc.saturating_add(SIZE_UOFFSET),
},
error_trace: Default::default(),
return Err(FlatbufferError::VerificationError(InvalidFlatbuffer::RangeOutOfBounds {
range: core::ops::Range {
start: string_loc,
end: string_loc.saturating_add(SIZE_UOFFSET),
},
));
error_trace: Default::default(),
}));
}
// SAFETY: the buffer range was verified above.
let len_old = unsafe { read_uoffset(buf, string_loc) };
if buf.len()
< string_loc
.saturating_add(SIZE_UOFFSET)
.saturating_add(len_old.try_into()?)
{
return Err(FlatbufferError::VerificationError(
InvalidFlatbuffer::RangeOutOfBounds {
range: core::ops::Range {
start: string_loc,
end: string_loc
.saturating_add(SIZE_UOFFSET)
.saturating_add(len_old.try_into()?),
},
error_trace: Default::default(),
if buf.len() < string_loc.saturating_add(SIZE_UOFFSET).saturating_add(len_old.try_into()?) {
return Err(FlatbufferError::VerificationError(InvalidFlatbuffer::RangeOutOfBounds {
range: core::ops::Range {
start: string_loc,
end: string_loc.saturating_add(SIZE_UOFFSET).saturating_add(len_old.try_into()?),
},
));
error_trace: Default::default(),
}));
}
let len_new = v.len();
@@ -628,9 +579,7 @@ unsafe fn get_any_value_integer(
BaseType::ULong => i64::from_u64(u64::follow(buf, loc)),
BaseType::Float => i64::from_f32(f32::follow(buf, loc)),
BaseType::Double => i64::from_f64(f64::follow(buf, loc)),
BaseType::String => ForwardsUOffset::<&str>::follow(buf, loc)
.parse::<i64>()
.ok(),
BaseType::String => ForwardsUOffset::<&str>::follow(buf, loc).parse::<i64>().ok(),
_ => None, // Tables & vectors do not make sense.
}
.ok_or(FlatbufferError::FieldTypeMismatch(
@@ -661,9 +610,7 @@ unsafe fn get_any_value_float(
BaseType::ULong => f64::from_u64(u64::follow(buf, loc)),
BaseType::Float => f64::from_f32(f32::follow(buf, loc)),
BaseType::Double => Some(f64::follow(buf, loc)),
BaseType::String => ForwardsUOffset::<&str>::follow(buf, loc)
.parse::<f64>()
.ok(),
BaseType::String => ForwardsUOffset::<&str>::follow(buf, loc).parse::<f64>().ok(),
_ => None,
}
.ok_or(FlatbufferError::FieldTypeMismatch(
@@ -685,9 +632,9 @@ unsafe fn get_any_value_string(
type_index: usize,
) -> String {
match base_type {
BaseType::Float | BaseType::Double => get_any_value_float(base_type, buf, loc)
.unwrap_or_default()
.to_string(),
BaseType::Float | BaseType::Double => {
get_any_value_float(base_type, buf, loc).unwrap_or_default().to_string()
}
BaseType::String => {
String::from_utf8_lossy(ForwardsUOffset::<&[u8]>::follow(buf, loc)).to_string()
}
@@ -728,9 +675,7 @@ unsafe fn get_any_value_string(
}
BaseType::Vector => String::from("[(elements)]"), // TODO inherited from C++: implement this as well.
BaseType::Union => String::from("(union)"), // TODO inherited from C++: implement this as well.
_ => get_any_value_integer(base_type, buf, loc)
.unwrap_or_default()
.to_string(),
_ => get_any_value_integer(base_type, buf, loc).unwrap_or_default().to_string(),
}
}
@@ -742,15 +687,13 @@ fn set_any_value_integer(
v: i64,
) -> FlatbufferResult<()> {
if buf.len() < get_type_size(base_type) {
return Err(FlatbufferError::VerificationError(
InvalidFlatbuffer::RangeOutOfBounds {
range: core::ops::Range {
start: field_loc,
end: field_loc.saturating_add(get_type_size(base_type)),
},
error_trace: Default::default(),
return Err(FlatbufferError::VerificationError(InvalidFlatbuffer::RangeOutOfBounds {
range: core::ops::Range {
start: field_loc,
end: field_loc.saturating_add(get_type_size(base_type)),
},
));
error_trace: Default::default(),
}));
}
let buf = &mut buf[field_loc..];
let type_name = base_type.variant_name().unwrap_or_default().to_string();
@@ -761,10 +704,7 @@ fn set_any_value_integer(
// SAFETY: buffer size is verified at the beginning of this function.
unsafe { Ok(emplace_scalar::<$ty>(buf, v)) }
} else {
Err(FlatbufferError::FieldTypeMismatch(
String::from("i64"),
type_name,
))
Err(FlatbufferError::FieldTypeMismatch(String::from("i64"), type_name))
}
};
}
@@ -804,10 +744,7 @@ fn set_any_value_integer(
// SAFETY: buffer size is verified at the beginning of this function.
unsafe { Ok(emplace_scalar::<f32>(buf, value)) }
} else {
Err(FlatbufferError::FieldTypeMismatch(
String::from("i64"),
type_name,
))
Err(FlatbufferError::FieldTypeMismatch(String::from("i64"), type_name))
}
}
BaseType::Double => {
@@ -815,10 +752,7 @@ fn set_any_value_integer(
// SAFETY: buffer size is verified at the beginning of this function.
unsafe { Ok(emplace_scalar::<f64>(buf, value)) }
} else {
Err(FlatbufferError::FieldTypeMismatch(
String::from("i64"),
type_name,
))
Err(FlatbufferError::FieldTypeMismatch(String::from("i64"), type_name))
}
}
_ => Err(FlatbufferError::SetValueNotSupported),
@@ -833,15 +767,13 @@ fn set_any_value_float(
v: f64,
) -> FlatbufferResult<()> {
if buf.len() < get_type_size(base_type) {
return Err(FlatbufferError::VerificationError(
InvalidFlatbuffer::RangeOutOfBounds {
range: core::ops::Range {
start: field_loc,
end: field_loc.saturating_add(get_type_size(base_type)),
},
error_trace: Default::default(),
return Err(FlatbufferError::VerificationError(InvalidFlatbuffer::RangeOutOfBounds {
range: core::ops::Range {
start: field_loc,
end: field_loc.saturating_add(get_type_size(base_type)),
},
));
error_trace: Default::default(),
}));
}
let buf = &mut buf[field_loc..];
let type_name = base_type.variant_name().unwrap_or_default().to_string();
@@ -936,10 +868,7 @@ fn set_any_value_float(
}
_ => return Err(FlatbufferError::SetValueNotSupported),
}
return Err(FlatbufferError::FieldTypeMismatch(
String::from("f64"),
type_name,
));
return Err(FlatbufferError::FieldTypeMismatch(String::from("f64"), type_name));
}
fn is_scalar(base_type: BaseType) -> bool {
@@ -1002,10 +931,7 @@ unsafe fn update_offset(
}
if field_type == BaseType::Obj
&& schema
.objects()
.get(field.type_().index().try_into()?)
.is_struct()
&& schema.objects().get(field.type_().index().try_into()?).is_struct()
{
continue;
}
@@ -1041,10 +967,7 @@ unsafe fn update_offset(
continue;
}
if elem_type == BaseType::Obj
&& schema
.objects()
.get(field.type_().index().try_into()?)
.is_struct()
&& schema.objects().get(field.type_().index().try_into()?).is_struct()
{
continue;
}

File diff suppressed because it is too large Load Diff

View File

@@ -156,12 +156,7 @@ fn verify_table(
}
_ => {
return Err(FlatbufferError::TypeNotSupported(
field
.type_()
.base_type()
.variant_name()
.unwrap_or_default()
.to_string(),
field.type_().base_type().variant_name().unwrap_or_default().to_string(),
));
}
};
@@ -342,12 +337,7 @@ fn verify_vector<'a, 'b, 'c>(
}
_ => {
return Err(FlatbufferError::TypeNotSupported(
field
.type_()
.base_type()
.variant_name()
.unwrap_or_default()
.to_string(),
field.type_().base_type().variant_name().unwrap_or_default().to_string(),
))
}
}
@@ -401,11 +391,7 @@ fn verify_union<'a, 'b, 'c>(
}
_ => {
return Err(FlatbufferError::TypeNotSupported(
enum_type
.base_type()
.variant_name()
.unwrap_or_default()
.to_string(),
enum_type.base_type().variant_name().unwrap_or_default().to_string(),
))
}
}

View File

@@ -49,11 +49,7 @@ impl<'a> SafeBuffer<'a> {
) -> FlatbufferResult<Self> {
let mut buf_loc_to_obj_idx = HashMap::new();
verify_with_options(&buf, schema, opts, &mut buf_loc_to_obj_idx)?;
Ok(SafeBuffer {
buf,
schema,
buf_loc_to_obj_idx,
})
Ok(SafeBuffer { buf, schema, buf_loc_to_obj_idx })
}
/// Gets the root table in the buffer.
@@ -61,10 +57,7 @@ impl<'a> SafeBuffer<'a> {
// SAFETY: the buffer was verified during construction.
let table = unsafe { get_any_root(self.buf) };
SafeTable {
safe_buf: self,
loc: table.loc(),
}
SafeTable { safe_buf: self, loc: table.loc() }
}
fn find_field_by_name(
@@ -74,9 +67,7 @@ impl<'a> SafeBuffer<'a> {
) -> FlatbufferResult<Option<Field>> {
Ok(self
.get_all_fields(buf_loc)?
.lookup_by_key(field_name, |field: &Field<'_>, key| {
field.key_compare_with_value(key)
}))
.lookup_by_key(field_name, |field: &Field<'_>, key| field.key_compare_with_value(key)))
}
fn get_all_fields(&self, buf_loc: usize) -> FlatbufferResult<Vector<ForwardsUOffset<Field>>> {
@@ -154,10 +145,7 @@ impl<'a> SafeTable<'a> {
// SAFETY: the buffer was verified during construction.
let optional_st =
unsafe { get_field_struct(&Table::new(&self.safe_buf.buf, self.loc), &field)? };
Ok(optional_st.map(|st| SafeStruct {
safe_buf: self.safe_buf,
loc: st.loc(),
}))
Ok(optional_st.map(|st| SafeStruct { safe_buf: self.safe_buf, loc: st.loc() }))
} else {
Err(FlatbufferError::FieldNotFound)
}
@@ -188,10 +176,7 @@ impl<'a> SafeTable<'a> {
// SAFETY: the buffer was verified during construction.
let optional_table =
unsafe { get_field_table(&Table::new(&self.safe_buf.buf, self.loc), &field)? };
Ok(optional_table.map(|t| SafeTable {
safe_buf: self.safe_buf,
loc: t.loc(),
}))
Ok(optional_table.map(|t| SafeTable { safe_buf: self.safe_buf, loc: t.loc() }))
} else {
Err(FlatbufferError::FieldNotFound)
}
@@ -260,10 +245,7 @@ impl<'a> SafeStruct<'a> {
let st = unsafe {
get_field_struct_in_struct(&Struct::new(&self.safe_buf.buf, self.loc), &field)?
};
Ok(SafeStruct {
safe_buf: self.safe_buf,
loc: st.loc(),
})
Ok(SafeStruct { safe_buf: self.safe_buf, loc: st.loc() })
} else {
Err(FlatbufferError::FieldNotFound)
}