summaryrefslogtreecommitdiff
path: root/samples
diff options
context:
space:
mode:
authorCasper <casperneo@uchicago.edu>2021-04-26 09:18:58 -0400
committerGitHub <noreply@github.com>2021-04-26 09:18:58 -0400
commitc24031c36bebd20f39c3237b4481e5604a9f0da9 (patch)
tree574d42485ca81000dacaa8771c816d518f2530f4 /samples
parent4ccc52c7a087caf4ea7665a60897aed40a59872e (diff)
downloadflatbuffers-c24031c36bebd20f39c3237b4481e5604a9f0da9.tar.gz
flatbuffers-c24031c36bebd20f39c3237b4481e5604a9f0da9.tar.bz2
flatbuffers-c24031c36bebd20f39c3237b4481e5604a9f0da9.zip
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>
Diffstat (limited to 'samples')
-rw-r--r--samples/monster_generated.rs12
-rw-r--r--samples/sample_flexbuffers.rs18
2 files changed, 8 insertions, 22 deletions
diff --git a/samples/monster_generated.rs b/samples/monster_generated.rs
index d2f17ee5..b3d82674 100644
--- a/samples/monster_generated.rs
+++ b/samples/monster_generated.rs
@@ -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); }
}
}
diff --git a/samples/sample_flexbuffers.rs b/samples/sample_flexbuffers.rs
index 677bcacb..237dbf0b 100644
--- a/samples/sample_flexbuffers.rs
+++ b/samples/sample_flexbuffers.rs
@@ -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.