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

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