mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-14 16:36:55 +00:00
Mark endian_scalar as unsafe. (#6588)
* Mark endian_scalar as unsafe. Also - removed the deprecated flexbuffer slice from example - fixed some cargo warnings * Assertions and read_scalar made unsafe * Clippy lints * Add to Safety Co-authored-by: Casper Neo <cneo@google.com>
This commit is contained in:
@@ -76,7 +76,9 @@ impl<'a> flatbuffers::Follow<'a> for Color {
|
||||
type Inner = Self;
|
||||
#[inline]
|
||||
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
let b = flatbuffers::read_scalar_at::<i8>(buf, loc);
|
||||
let b = unsafe {
|
||||
flatbuffers::read_scalar_at::<i8>(buf, loc)
|
||||
};
|
||||
Self(b)
|
||||
}
|
||||
}
|
||||
@@ -85,7 +87,7 @@ impl flatbuffers::Push for Color {
|
||||
type Output = Color;
|
||||
#[inline]
|
||||
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
|
||||
flatbuffers::emplace_scalar::<i8>(dst, self.0);
|
||||
unsafe { flatbuffers::emplace_scalar::<i8>(dst, self.0); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,7 +163,9 @@ impl<'a> flatbuffers::Follow<'a> for Equipment {
|
||||
type Inner = Self;
|
||||
#[inline]
|
||||
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
let b = flatbuffers::read_scalar_at::<u8>(buf, loc);
|
||||
let b = unsafe {
|
||||
flatbuffers::read_scalar_at::<u8>(buf, loc)
|
||||
};
|
||||
Self(b)
|
||||
}
|
||||
}
|
||||
@@ -170,7 +174,7 @@ impl flatbuffers::Push for Equipment {
|
||||
type Output = Equipment;
|
||||
#[inline]
|
||||
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
|
||||
flatbuffers::emplace_scalar::<u8>(dst, self.0);
|
||||
unsafe { flatbuffers::emplace_scalar::<u8>(dst, self.0); }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -149,24 +149,6 @@ fn main() {
|
||||
.iter()
|
||||
.map(|r| r.as_u8())
|
||||
.eq(vec![5, 10, 25, 25, 25, 100].into_iter()));
|
||||
// For very speed sensitive applications, you can directly read the slice if all of the
|
||||
// following are true:
|
||||
//
|
||||
// * The provided data buffer contains a valid flexbuffer.
|
||||
// * You correctly specify the flexbuffer type and width.
|
||||
// * The host machine is little endian.
|
||||
// * The provided data buffer itself is aligned in memory to 8 bytes.
|
||||
//
|
||||
// Vec<u8> has alignment 1 so special care is needed to get your buffer's alignment to 8.
|
||||
#[cfg(target_endian = "little")]
|
||||
{
|
||||
if monster_coins.is_aligned() {
|
||||
assert_eq!(
|
||||
monster_coins.get_slice::<i8>().unwrap(),
|
||||
&[5, 10, 25, 25, 25, 100]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Build the answer to life the universe and everything. Reusing a builder resets it. The
|
||||
// reused internals won't need to reallocate leading to a potential 2x speedup.
|
||||
|
||||
Reference in New Issue
Block a user