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

@@ -137,7 +137,9 @@ impl<T> Push for WIPOffset<T> {
#[inline(always)]
fn push(&self, dst: &mut [u8], rest: &[u8]) {
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)]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
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)
}
}
@@ -200,7 +202,7 @@ impl<'a, T: Follow<'a>> Follow<'a> for ForwardsVOffset<T> {
#[inline(always)]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
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)
}
}
@@ -230,7 +232,7 @@ impl<'a, T: Follow<'a>> Follow<'a> for BackwardsSOffset<T> {
#[inline(always)]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
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)
}
}
@@ -293,7 +295,7 @@ impl<'a> Follow<'a> for bool {
type Inner = bool;
#[inline(always)]
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;
#[inline(always)]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
read_scalar_at::<$ty>(buf, loc)
unsafe { read_scalar_at::<$ty>(buf, loc) }
}
}
};