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:
Casper
2021-04-26 09:18:58 -04:00
committed by GitHub
parent 4ccc52c7a0
commit c24031c36b
20 changed files with 134 additions and 100 deletions

View File

@@ -37,6 +37,8 @@ where
} }
} }
#[allow(clippy::len_without_is_empty)]
#[allow(clippy::from_over_into)] // TODO(caspern): Go from From to Into.
impl<'a, T: 'a, const N: usize> Array<'a, T, N> { impl<'a, T: 'a, const N: usize> Array<'a, T, N> {
#[inline(always)] #[inline(always)]
pub fn new(buf: &'a [u8]) -> Self { pub fn new(buf: &'a [u8]) -> Self {
@@ -49,7 +51,7 @@ impl<'a, T: 'a, const N: usize> Array<'a, T, N> {
} }
#[inline(always)] #[inline(always)]
pub fn len(&self) -> usize { pub const fn len(&self) -> usize {
N N
} }
} }

View File

@@ -564,12 +564,14 @@ impl<'fbb> FlatBufferBuilder<'fbb> {
{ {
let n = self.head + self.used_space() - object_revloc_to_vtable.value() as usize; let n = self.head + self.used_space() - object_revloc_to_vtable.value() as usize;
let saw = read_scalar_at::<UOffsetT>(&self.owned_buf, n); let saw = unsafe { read_scalar_at::<UOffsetT>(&self.owned_buf, n) };
debug_assert_eq!(saw, 0xF0F0_F0F0); debug_assert_eq!(saw, 0xF0F0_F0F0);
emplace_scalar::<SOffsetT>( unsafe {
&mut self.owned_buf[n..n + SIZE_SOFFSET], emplace_scalar::<SOffsetT>(
vt_use as SOffsetT - object_revloc_to_vtable.value() as SOffsetT, &mut self.owned_buf[n..n + SIZE_SOFFSET],
); vt_use as SOffsetT - object_revloc_to_vtable.value() as SOffsetT,
);
}
} }
self.field_locs.clear(); self.field_locs.clear();

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#![allow(clippy::wrong_self_convention)]
use std::mem::size_of; use std::mem::size_of;
@@ -148,34 +149,36 @@ pub fn byte_swap_f64(x: f64) -> f64 {
/// Place an EndianScalar into the provided mutable byte slice. Performs /// Place an EndianScalar into the provided mutable byte slice. Performs
/// endian conversion, if necessary. /// endian conversion, if necessary.
/// # Safety
/// Caller must ensure `s.len() > size_of::<T>()`
/// and `x` does not overlap with `s`.
#[inline] #[inline]
pub fn emplace_scalar<T: EndianScalar>(s: &mut [u8], x: T) { pub unsafe fn emplace_scalar<T: EndianScalar>(s: &mut [u8], x: T) {
let x_le = x.to_little_endian(); let x_le = x.to_little_endian();
unsafe { core::ptr::copy_nonoverlapping(
core::ptr::copy_nonoverlapping( &x_le as *const T as *const u8,
&x_le as *const T as *const u8, s.as_mut_ptr() as *mut u8,
s.as_mut_ptr() as *mut u8, size_of::<T>(),
size_of::<T>(), );
);
}
} }
/// Read an EndianScalar from the provided byte slice at the specified location. /// Read an EndianScalar from the provided byte slice at the specified location.
/// Performs endian conversion, if necessary. /// Performs endian conversion, if necessary.
/// # Safety
/// Caller must ensure `s.len() > loc + size_of::<T>()`.
#[inline] #[inline]
pub fn read_scalar_at<T: EndianScalar>(s: &[u8], loc: usize) -> T { pub unsafe fn read_scalar_at<T: EndianScalar>(s: &[u8], loc: usize) -> T {
read_scalar(&s[loc..]) read_scalar(&s[loc..])
} }
/// Read an EndianScalar from the provided byte slice. Performs endian /// Read an EndianScalar from the provided byte slice. Performs endian
/// conversion, if necessary. /// conversion, if necessary.
/// # Safety
/// Caller must ensure `s.len() > size_of::<T>()`.
#[inline] #[inline]
pub fn read_scalar<T: EndianScalar>(s: &[u8]) -> T { pub unsafe fn read_scalar<T: EndianScalar>(s: &[u8]) -> T {
let mut mem = core::mem::MaybeUninit::<T>::uninit(); let mut mem = core::mem::MaybeUninit::<T>::uninit();
// Since [u8] has alignment 1, we copy it into T which may have higher alignment. // Since [u8] has alignment 1, we copy it into T which may have higher alignment.
let x = unsafe { core::ptr::copy_nonoverlapping(s.as_ptr(), mem.as_mut_ptr() as *mut u8, size_of::<T>());
core::ptr::copy_nonoverlapping(s.as_ptr(), mem.as_mut_ptr() as *mut u8, size_of::<T>()); mem.assume_init().from_little_endian()
mem.assume_init()
};
x.from_little_endian()
} }

View File

@@ -137,7 +137,9 @@ impl<T> Push for WIPOffset<T> {
#[inline(always)] #[inline(always)]
fn push(&self, dst: &mut [u8], rest: &[u8]) { fn push(&self, dst: &mut [u8], rest: &[u8]) {
let n = (SIZE_UOFFSET + rest.len() - self.value() as usize) as UOffsetT; let n = (SIZE_UOFFSET + rest.len() - self.value() as usize) as UOffsetT;
emplace_scalar::<UOffsetT>(dst, n); unsafe {
emplace_scalar::<UOffsetT>(dst, n);
}
} }
} }
@@ -179,7 +181,7 @@ impl<'a, T: Follow<'a>> Follow<'a> for ForwardsUOffset<T> {
#[inline(always)] #[inline(always)]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let slice = &buf[loc..loc + SIZE_UOFFSET]; let slice = &buf[loc..loc + SIZE_UOFFSET];
let off = read_scalar::<u32>(slice) as usize; let off = unsafe { read_scalar::<u32>(slice) as usize };
T::follow(buf, loc + off) T::follow(buf, loc + off)
} }
} }
@@ -200,7 +202,7 @@ impl<'a, T: Follow<'a>> Follow<'a> for ForwardsVOffset<T> {
#[inline(always)] #[inline(always)]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let slice = &buf[loc..loc + SIZE_VOFFSET]; let slice = &buf[loc..loc + SIZE_VOFFSET];
let off = read_scalar::<VOffsetT>(slice) as usize; let off = unsafe { read_scalar::<VOffsetT>(slice) as usize };
T::follow(buf, loc + off) T::follow(buf, loc + off)
} }
} }
@@ -230,7 +232,7 @@ impl<'a, T: Follow<'a>> Follow<'a> for BackwardsSOffset<T> {
#[inline(always)] #[inline(always)]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let slice = &buf[loc..loc + SIZE_SOFFSET]; let slice = &buf[loc..loc + SIZE_SOFFSET];
let off = read_scalar::<SOffsetT>(slice); let off = unsafe { read_scalar::<SOffsetT>(slice) };
T::follow(buf, (loc as SOffsetT - off) as usize) T::follow(buf, (loc as SOffsetT - off) as usize)
} }
} }
@@ -293,7 +295,7 @@ impl<'a> Follow<'a> for bool {
type Inner = bool; type Inner = bool;
#[inline(always)] #[inline(always)]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
read_scalar_at::<u8>(buf, loc) != 0 unsafe { read_scalar_at::<u8>(buf, loc) != 0 }
} }
} }
@@ -308,7 +310,7 @@ macro_rules! impl_follow_for_endian_scalar {
type Inner = $ty; type Inner = $ty;
#[inline(always)] #[inline(always)]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
read_scalar_at::<$ty>(buf, loc) unsafe { read_scalar_at::<$ty>(buf, loc) }
} }
} }
}; };

View File

@@ -61,7 +61,9 @@ macro_rules! impl_push_for_endian_scalar {
#[inline] #[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) { fn push(&self, dst: &mut [u8], _rest: &[u8]) {
emplace_scalar::<$ty>(dst, *self); unsafe {
emplace_scalar::<$ty>(dst, *self);
}
} }
} }
}; };

View File

@@ -73,7 +73,7 @@ impl<'a, T: 'a> Vector<'a, T> {
#[inline(always)] #[inline(always)]
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
read_scalar_at::<UOffsetT>(&self.0, self.1) as usize unsafe { read_scalar_at::<UOffsetT>(&self.0, self.1) as usize }
} }
#[inline(always)] #[inline(always)]
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
@@ -84,7 +84,7 @@ impl<'a, T: 'a> Vector<'a, T> {
impl<'a, T: Follow<'a> + 'a> Vector<'a, T> { impl<'a, T: Follow<'a> + 'a> Vector<'a, T> {
#[inline(always)] #[inline(always)]
pub fn get(&self, idx: usize) -> T::Inner { pub fn get(&self, idx: usize) -> T::Inner {
debug_assert!(idx < read_scalar_at::<u32>(&self.0, self.1) as usize); debug_assert!(idx < self.len() as usize);
let sz = size_of::<T>(); let sz = size_of::<T>();
debug_assert!(sz > 0); debug_assert!(sz > 0);
T::follow(self.0, self.1 as usize + SIZE_UOFFSET + sz * idx) T::follow(self.0, self.1 as usize + SIZE_UOFFSET + sz * idx)
@@ -103,7 +103,7 @@ impl<'a, T: SafeSliceAccess + 'a> Vector<'a, T> {
let loc = self.1; let loc = self.1;
let sz = size_of::<T>(); let sz = size_of::<T>();
debug_assert!(sz > 0); debug_assert!(sz > 0);
let len = read_scalar_at::<UOffsetT>(&buf, loc) as usize; let len = unsafe { read_scalar_at::<UOffsetT>(&buf, loc) } as usize;
let data_buf = &buf[loc + SIZE_UOFFSET..loc + SIZE_UOFFSET + len * sz]; let data_buf = &buf[loc + SIZE_UOFFSET..loc + SIZE_UOFFSET + len * sz];
let ptr = data_buf.as_ptr() as *const T; let ptr = data_buf.as_ptr() as *const T;
let s: &'a [T] = unsafe { from_raw_parts(ptr, len) }; let s: &'a [T] = unsafe { from_raw_parts(ptr, len) };
@@ -144,7 +144,7 @@ pub fn follow_cast_ref<'a, T: Sized + 'a>(buf: &'a [u8], loc: usize) -> &'a T {
impl<'a> Follow<'a> for &'a str { impl<'a> Follow<'a> for &'a str {
type Inner = &'a str; type Inner = &'a str;
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let len = read_scalar_at::<UOffsetT>(&buf, loc) as usize; let len = unsafe { read_scalar_at::<UOffsetT>(&buf, loc) } as usize;
let slice = &buf[loc + SIZE_UOFFSET..loc + SIZE_UOFFSET + len]; let slice = &buf[loc + SIZE_UOFFSET..loc + SIZE_UOFFSET + len];
unsafe { from_utf8_unchecked(slice) } unsafe { from_utf8_unchecked(slice) }
} }
@@ -154,7 +154,7 @@ impl<'a> Follow<'a> for &'a str {
fn follow_slice_helper<T>(buf: &[u8], loc: usize) -> &[T] { fn follow_slice_helper<T>(buf: &[u8], loc: usize) -> &[T] {
let sz = size_of::<T>(); let sz = size_of::<T>();
debug_assert!(sz > 0); debug_assert!(sz > 0);
let len = read_scalar_at::<UOffsetT>(&buf, loc) as usize; let len = unsafe { read_scalar_at::<UOffsetT>(&buf, loc) as usize };
let data_buf = &buf[loc + SIZE_UOFFSET..loc + SIZE_UOFFSET + len * sz]; let data_buf = &buf[loc + SIZE_UOFFSET..loc + SIZE_UOFFSET + len * sz];
let ptr = data_buf.as_ptr() as *const T; let ptr = data_buf.as_ptr() as *const T;
let s: &[T] = unsafe { from_raw_parts(ptr, len) }; let s: &[T] = unsafe { from_raw_parts(ptr, len) };

View File

@@ -40,10 +40,10 @@ impl<'a> VTable<'a> {
(self.num_bytes() / SIZE_VOFFSET) - 2 (self.num_bytes() / SIZE_VOFFSET) - 2
} }
pub fn num_bytes(&self) -> usize { pub fn num_bytes(&self) -> usize {
read_scalar_at::<VOffsetT>(self.buf, self.loc) as usize unsafe { read_scalar_at::<VOffsetT>(self.buf, self.loc) as usize }
} }
pub fn object_inline_num_bytes(&self) -> usize { pub fn object_inline_num_bytes(&self) -> usize {
let n = read_scalar_at::<VOffsetT>(self.buf, self.loc + SIZE_VOFFSET); let n = unsafe { read_scalar_at::<VOffsetT>(self.buf, self.loc + SIZE_VOFFSET) };
n as usize n as usize
} }
pub fn get_field(&self, idx: usize) -> VOffsetT { pub fn get_field(&self, idx: usize) -> VOffsetT {
@@ -51,17 +51,19 @@ impl<'a> VTable<'a> {
if idx > self.num_fields() { if idx > self.num_fields() {
return 0; return 0;
} }
read_scalar_at::<VOffsetT>( unsafe {
self.buf, read_scalar_at::<VOffsetT>(
self.loc + SIZE_VOFFSET + SIZE_VOFFSET + SIZE_VOFFSET * idx, self.buf,
) self.loc + SIZE_VOFFSET + SIZE_VOFFSET + SIZE_VOFFSET * idx,
)
}
} }
pub fn get(&self, byte_loc: VOffsetT) -> VOffsetT { pub fn get(&self, byte_loc: VOffsetT) -> VOffsetT {
// TODO(rw): distinguish between None and 0? // TODO(rw): distinguish between None and 0?
if byte_loc as usize >= self.num_bytes() { if byte_loc as usize >= self.num_bytes() {
return 0; return 0;
} }
read_scalar_at::<VOffsetT>(self.buf, self.loc + byte_loc as usize) unsafe { read_scalar_at::<VOffsetT>(self.buf, self.loc + byte_loc as usize) }
} }
pub fn as_bytes(&self) -> &[u8] { pub fn as_bytes(&self) -> &[u8] {
let len = self.num_bytes(); let len = self.num_bytes();

View File

@@ -40,14 +40,18 @@ impl<'a> VTableWriter<'a> {
/// to the provided value. /// to the provided value.
#[inline(always)] #[inline(always)]
pub fn write_vtable_byte_length(&mut self, n: VOffsetT) { pub fn write_vtable_byte_length(&mut self, n: VOffsetT) {
emplace_scalar::<VOffsetT>(&mut self.buf[..SIZE_VOFFSET], n); unsafe {
emplace_scalar::<VOffsetT>(&mut self.buf[..SIZE_VOFFSET], n);
}
debug_assert_eq!(n as usize, self.buf.len()); debug_assert_eq!(n as usize, self.buf.len());
} }
/// Writes an object length (in bytes) into the vtable. /// Writes an object length (in bytes) into the vtable.
#[inline(always)] #[inline(always)]
pub fn write_object_inline_size(&mut self, n: VOffsetT) { pub fn write_object_inline_size(&mut self, n: VOffsetT) {
emplace_scalar::<VOffsetT>(&mut self.buf[SIZE_VOFFSET..2 * SIZE_VOFFSET], n); unsafe {
emplace_scalar::<VOffsetT>(&mut self.buf[SIZE_VOFFSET..2 * SIZE_VOFFSET], n);
}
} }
/// Gets an object field offset from the vtable. Only used for debugging. /// Gets an object field offset from the vtable. Only used for debugging.
@@ -57,7 +61,7 @@ impl<'a> VTableWriter<'a> {
#[inline(always)] #[inline(always)]
pub fn get_field_offset(&self, vtable_offset: VOffsetT) -> VOffsetT { pub fn get_field_offset(&self, vtable_offset: VOffsetT) -> VOffsetT {
let idx = vtable_offset as usize; let idx = vtable_offset as usize;
read_scalar_at::<VOffsetT>(&self.buf, idx) unsafe { read_scalar_at::<VOffsetT>(&self.buf, idx) }
} }
/// Writes an object field offset into the vtable. /// Writes an object field offset into the vtable.
@@ -67,7 +71,9 @@ impl<'a> VTableWriter<'a> {
#[inline(always)] #[inline(always)]
pub fn write_field_offset(&mut self, vtable_offset: VOffsetT, object_data_offset: VOffsetT) { pub fn write_field_offset(&mut self, vtable_offset: VOffsetT, object_data_offset: VOffsetT) {
let idx = vtable_offset as usize; let idx = vtable_offset as usize;
emplace_scalar::<VOffsetT>(&mut self.buf[idx..idx + SIZE_VOFFSET], object_data_offset); unsafe {
emplace_scalar::<VOffsetT>(&mut self.buf[idx..idx + SIZE_VOFFSET], object_data_offset);
}
} }
/// Clears all data in this VTableWriter. Used to cleanly undo a /// Clears all data in this VTableWriter. Used to cleanly undo a

View File

@@ -76,7 +76,9 @@ impl<'a> flatbuffers::Follow<'a> for Color {
type Inner = Self; type Inner = Self;
#[inline] #[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { 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) Self(b)
} }
} }
@@ -85,7 +87,7 @@ impl flatbuffers::Push for Color {
type Output = Color; type Output = Color;
#[inline] #[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) { 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; type Inner = Self;
#[inline] #[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { 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) Self(b)
} }
} }
@@ -170,7 +174,7 @@ impl flatbuffers::Push for Equipment {
type Output = Equipment; type Output = Equipment;
#[inline] #[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) { fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<u8>(dst, self.0); unsafe { flatbuffers::emplace_scalar::<u8>(dst, self.0); }
} }
} }

View File

@@ -149,24 +149,6 @@ fn main() {
.iter() .iter()
.map(|r| r.as_u8()) .map(|r| r.as_u8())
.eq(vec![5, 10, 25, 25, 25, 100].into_iter())); .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 // 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. // reused internals won't need to reallocate leading to a potential 2x speedup.

View File

@@ -746,9 +746,9 @@ class RustGenerator : public BaseGenerator {
code_ += " type Inner = Self;"; code_ += " type Inner = Self;";
code_ += " #[inline]"; code_ += " #[inline]";
code_ += " fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {"; code_ += " fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {";
code_ += code_ += " let b = unsafe {";
" let b = flatbuffers::read_scalar_at::<{{BASE_TYPE}}>(buf," code_ += " flatbuffers::read_scalar_at::<{{BASE_TYPE}}>(buf, loc)";
" loc);"; code_ += " };";
code_ += " {{FROM_BASE}}"; code_ += " {{FROM_BASE}}";
code_ += " }"; code_ += " }";
code_ += "}"; code_ += "}";
@@ -758,8 +758,8 @@ class RustGenerator : public BaseGenerator {
code_ += " #[inline]"; code_ += " #[inline]";
code_ += " fn push(&self, dst: &mut [u8], _rest: &[u8]) {"; code_ += " fn push(&self, dst: &mut [u8], _rest: &[u8]) {";
code_ += code_ +=
" flatbuffers::emplace_scalar::<{{BASE_TYPE}}>" " unsafe { flatbuffers::emplace_scalar::<{{BASE_TYPE}}>"
"(dst, {{INTO_BASE}});"; "(dst, {{INTO_BASE}}); }";
code_ += " }"; code_ += " }";
code_ += "}"; code_ += "}";
code_ += ""; code_ += "";

View File

@@ -76,7 +76,9 @@ impl<'a> flatbuffers::Follow<'a> for TestEnum {
type Inner = Self; type Inner = Self;
#[inline] #[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { 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) Self(b)
} }
} }
@@ -85,7 +87,7 @@ impl flatbuffers::Push for TestEnum {
type Output = TestEnum; type Output = TestEnum;
#[inline] #[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) { fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<i8>(dst, self.0); unsafe { flatbuffers::emplace_scalar::<i8>(dst, self.0); }
} }
} }

View File

@@ -71,7 +71,9 @@ impl<'a> flatbuffers::Follow<'a> for FromInclude {
type Inner = Self; type Inner = Self;
#[inline] #[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let b = flatbuffers::read_scalar_at::<i64>(buf, loc); let b = unsafe {
flatbuffers::read_scalar_at::<i64>(buf, loc)
};
Self(b) Self(b)
} }
} }
@@ -80,7 +82,7 @@ impl flatbuffers::Push for FromInclude {
type Output = FromInclude; type Output = FromInclude;
#[inline] #[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) { fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<i64>(dst, self.0); unsafe { flatbuffers::emplace_scalar::<i64>(dst, self.0); }
} }
} }

View File

@@ -272,7 +272,9 @@ impl<'a> flatbuffers::Follow<'a> for Color {
type Inner = Self; type Inner = Self;
#[inline] #[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { 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)
};
unsafe { Self::from_bits_unchecked(b) } unsafe { Self::from_bits_unchecked(b) }
} }
} }
@@ -281,7 +283,7 @@ impl flatbuffers::Push for Color {
type Output = Color; type Output = Color;
#[inline] #[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) { fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<u8>(dst, self.bits()); unsafe { flatbuffers::emplace_scalar::<u8>(dst, self.bits()); }
} }
} }
@@ -365,7 +367,9 @@ impl<'a> flatbuffers::Follow<'a> for Race {
type Inner = Self; type Inner = Self;
#[inline] #[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { 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) Self(b)
} }
} }
@@ -374,7 +378,7 @@ impl flatbuffers::Push for Race {
type Output = Race; type Output = Race;
#[inline] #[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) { fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<i8>(dst, self.0); unsafe { flatbuffers::emplace_scalar::<i8>(dst, self.0); }
} }
} }
@@ -458,7 +462,9 @@ impl<'a> flatbuffers::Follow<'a> for Any {
type Inner = Self; type Inner = Self;
#[inline] #[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { 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) Self(b)
} }
} }
@@ -467,7 +473,7 @@ impl flatbuffers::Push for Any {
type Output = Any; type Output = Any;
#[inline] #[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) { fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<u8>(dst, self.0); unsafe { flatbuffers::emplace_scalar::<u8>(dst, self.0); }
} }
} }
@@ -647,7 +653,9 @@ impl<'a> flatbuffers::Follow<'a> for AnyUniqueAliases {
type Inner = Self; type Inner = Self;
#[inline] #[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { 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) Self(b)
} }
} }
@@ -656,7 +664,7 @@ impl flatbuffers::Push for AnyUniqueAliases {
type Output = AnyUniqueAliases; type Output = AnyUniqueAliases;
#[inline] #[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) { fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<u8>(dst, self.0); unsafe { flatbuffers::emplace_scalar::<u8>(dst, self.0); }
} }
} }
@@ -836,7 +844,9 @@ impl<'a> flatbuffers::Follow<'a> for AnyAmbiguousAliases {
type Inner = Self; type Inner = Self;
#[inline] #[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { 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) Self(b)
} }
} }
@@ -845,7 +855,7 @@ impl flatbuffers::Push for AnyAmbiguousAliases {
type Output = AnyAmbiguousAliases; type Output = AnyAmbiguousAliases;
#[inline] #[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) { fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<u8>(dst, self.0); unsafe { flatbuffers::emplace_scalar::<u8>(dst, self.0); }
} }
} }

View File

@@ -59,7 +59,9 @@ impl<'a> flatbuffers::Follow<'a> for ABC {
type Inner = Self; type Inner = Self;
#[inline] #[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let b = flatbuffers::read_scalar_at::<i32>(buf, loc); let b = unsafe {
flatbuffers::read_scalar_at::<i32>(buf, loc)
};
Self(b) Self(b)
} }
} }
@@ -68,7 +70,7 @@ impl flatbuffers::Push for ABC {
type Output = ABC; type Output = ABC;
#[inline] #[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) { fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<i32>(dst, self.0); unsafe { flatbuffers::emplace_scalar::<i32>(dst, self.0); }
} }
} }

View File

@@ -72,7 +72,9 @@ impl<'a> flatbuffers::Follow<'a> for UnionInNestedNS {
type Inner = Self; type Inner = Self;
#[inline] #[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { 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) Self(b)
} }
} }
@@ -81,7 +83,7 @@ impl flatbuffers::Push for UnionInNestedNS {
type Output = UnionInNestedNS; type Output = UnionInNestedNS;
#[inline] #[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) { fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<u8>(dst, self.0); unsafe { flatbuffers::emplace_scalar::<u8>(dst, self.0); }
} }
} }
@@ -209,7 +211,9 @@ impl<'a> flatbuffers::Follow<'a> for EnumInNestedNS {
type Inner = Self; type Inner = Self;
#[inline] #[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { 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) Self(b)
} }
} }
@@ -218,7 +222,7 @@ impl flatbuffers::Push for EnumInNestedNS {
type Output = EnumInNestedNS; type Output = EnumInNestedNS;
#[inline] #[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) { fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<i8>(dst, self.0); unsafe { flatbuffers::emplace_scalar::<i8>(dst, self.0); }
} }
} }

View File

@@ -72,7 +72,9 @@ impl<'a> flatbuffers::Follow<'a> for UnionInNestedNS {
type Inner = Self; type Inner = Self;
#[inline] #[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { 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) Self(b)
} }
} }
@@ -81,7 +83,7 @@ impl flatbuffers::Push for UnionInNestedNS {
type Output = UnionInNestedNS; type Output = UnionInNestedNS;
#[inline] #[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) { fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<u8>(dst, self.0); unsafe { flatbuffers::emplace_scalar::<u8>(dst, self.0); }
} }
} }
@@ -209,7 +211,9 @@ impl<'a> flatbuffers::Follow<'a> for EnumInNestedNS {
type Inner = Self; type Inner = Self;
#[inline] #[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { 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) Self(b)
} }
} }
@@ -218,7 +222,7 @@ impl flatbuffers::Push for EnumInNestedNS {
type Output = EnumInNestedNS; type Output = EnumInNestedNS;
#[inline] #[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) { fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<i8>(dst, self.0); unsafe { flatbuffers::emplace_scalar::<i8>(dst, self.0); }
} }
} }

View File

@@ -68,7 +68,9 @@ impl<'a> flatbuffers::Follow<'a> for OptionalByte {
type Inner = Self; type Inner = Self;
#[inline] #[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { 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) Self(b)
} }
} }
@@ -77,7 +79,7 @@ impl flatbuffers::Push for OptionalByte {
type Output = OptionalByte; type Output = OptionalByte;
#[inline] #[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) { fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<i8>(dst, self.0); unsafe { flatbuffers::emplace_scalar::<i8>(dst, self.0); }
} }
} }

View File

@@ -255,6 +255,7 @@ fn vector_uint4() {
assert_eq!(v.idx(2).get_u64(), Ok(5)); assert_eq!(v.idx(2).get_u64(), Ok(5));
assert_eq!(v.idx(3).get_u64(), Ok(7)); assert_eq!(v.idx(3).get_u64(), Ok(7));
assert!(v.index(4).is_err()); assert!(v.index(4).is_err());
#[allow(deprecated)]
#[cfg(target_endian = "little")] #[cfg(target_endian = "little")]
{ {
assert_eq!(r.get_slice::<u8>().unwrap(), [2, 3, 5, 7]); assert_eq!(r.get_slice::<u8>().unwrap(), [2, 3, 5, 7]);
@@ -399,7 +400,7 @@ fn serde_serious() {
b: u16, b: u16,
c: u32, c: u32,
d: u64, d: u64,
}; }
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
struct MyUnitStruct(Vec<String>); struct MyUnitStruct(Vec<String>);

View File

@@ -1682,8 +1682,10 @@ mod roundtrip_scalars {
fn prop<T: PartialEq + ::std::fmt::Debug + Copy + flatbuffers::EndianScalar>(x: T) { fn prop<T: PartialEq + ::std::fmt::Debug + Copy + flatbuffers::EndianScalar>(x: T) {
let mut buf = vec![0u8; ::std::mem::size_of::<T>()]; let mut buf = vec![0u8; ::std::mem::size_of::<T>()];
flatbuffers::emplace_scalar(&mut buf[..], x); let y = unsafe {
let y = flatbuffers::read_scalar(&buf[..]); flatbuffers::emplace_scalar(&mut buf[..], x);
flatbuffers::read_scalar(&buf[..])
};
assert_eq!(x, y); assert_eq!(x, y);
} }