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

@@ -76,7 +76,9 @@ impl<'a> flatbuffers::Follow<'a> for TestEnum {
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 TestEnum {
type Output = TestEnum;
#[inline]
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;
#[inline]
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)
}
}
@@ -80,7 +82,7 @@ impl flatbuffers::Push for FromInclude {
type Output = FromInclude;
#[inline]
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;
#[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)
};
unsafe { Self::from_bits_unchecked(b) }
}
}
@@ -281,7 +283,7 @@ impl flatbuffers::Push for Color {
type Output = Color;
#[inline]
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;
#[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)
}
}
@@ -374,7 +378,7 @@ impl flatbuffers::Push for Race {
type Output = Race;
#[inline]
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;
#[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)
}
}
@@ -467,7 +473,7 @@ impl flatbuffers::Push for Any {
type Output = Any;
#[inline]
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;
#[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)
}
}
@@ -656,7 +664,7 @@ impl flatbuffers::Push for AnyUniqueAliases {
type Output = AnyUniqueAliases;
#[inline]
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;
#[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)
}
}
@@ -845,7 +855,7 @@ impl flatbuffers::Push for AnyAmbiguousAliases {
type Output = AnyAmbiguousAliases;
#[inline]
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;
#[inline]
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)
}
}
@@ -68,7 +70,7 @@ impl flatbuffers::Push for ABC {
type Output = ABC;
#[inline]
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;
#[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)
}
}
@@ -81,7 +83,7 @@ impl flatbuffers::Push for UnionInNestedNS {
type Output = UnionInNestedNS;
#[inline]
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;
#[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)
}
}
@@ -218,7 +222,7 @@ impl flatbuffers::Push for EnumInNestedNS {
type Output = EnumInNestedNS;
#[inline]
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;
#[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)
}
}
@@ -81,7 +83,7 @@ impl flatbuffers::Push for UnionInNestedNS {
type Output = UnionInNestedNS;
#[inline]
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;
#[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)
}
}
@@ -218,7 +222,7 @@ impl flatbuffers::Push for EnumInNestedNS {
type Output = EnumInNestedNS;
#[inline]
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;
#[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)
}
}
@@ -77,7 +79,7 @@ impl flatbuffers::Push for OptionalByte {
type Output = OptionalByte;
#[inline]
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(3).get_u64(), Ok(7));
assert!(v.index(4).is_err());
#[allow(deprecated)]
#[cfg(target_endian = "little")]
{
assert_eq!(r.get_slice::<u8>().unwrap(), [2, 3, 5, 7]);
@@ -399,7 +400,7 @@ fn serde_serious() {
b: u16,
c: u32,
d: u64,
};
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
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) {
let mut buf = vec![0u8; ::std::mem::size_of::<T>()];
flatbuffers::emplace_scalar(&mut buf[..], x);
let y = flatbuffers::read_scalar(&buf[..]);
let y = unsafe {
flatbuffers::emplace_scalar(&mut buf[..], x);
flatbuffers::read_scalar(&buf[..])
};
assert_eq!(x, y);
}