Commit b80ad7e4 authored by jean-airoldie's avatar jean-airoldie Committed by Robert Winslow

[rust] Use read_scalar_at where possible (#5385)

This slightly improves readability.
parent 16aef8ac
......@@ -21,7 +21,7 @@ use std::marker::PhantomData;
use std::ptr::write_bytes;
use std::slice::from_raw_parts;
use endian_scalar::{read_scalar, emplace_scalar};
use endian_scalar::{read_scalar_at, emplace_scalar};
use primitives::*;
use push::{Push, PushAlignment};
use table::Table;
......@@ -459,7 +459,7 @@ impl<'fbb> FlatBufferBuilder<'fbb> {
{
let n = self.head + self.used_space() - object_revloc_to_vtable.value() as usize;
let saw = read_scalar::<UOffsetT>(&self.owned_buf[n..n + SIZE_SOFFSET]);
let saw = read_scalar_at::<UOffsetT>(&self.owned_buf, n);
debug_assert_eq!(saw, 0xF0F0F0F0);
emplace_scalar::<SOffsetT>(&mut self.owned_buf[n..n + SIZE_SOFFSET],
vt_use as SOffsetT - object_revloc_to_vtable.value() as SOffsetT);
......
......@@ -21,7 +21,7 @@ use std::str::from_utf8_unchecked;
#[cfg(target_endian = "little")]
use endian_scalar::EndianScalar;
use endian_scalar::read_scalar;
use endian_scalar::{read_scalar, read_scalar_at};
use follow::Follow;
use primitives::*;
......@@ -61,7 +61,7 @@ impl<'a, T: SafeSliceAccess + 'a> Vector<'a, T> {
let loc = self.1;
let sz = size_of::<T>();
debug_assert!(sz > 0);
let len = read_scalar::<UOffsetT>(&buf[loc..loc + SIZE_UOFFSET]) as usize;
let len = read_scalar_at::<UOffsetT>(&buf, loc) as usize;
let data_buf = &buf[loc + SIZE_UOFFSET..loc + SIZE_UOFFSET + len * sz];
let ptr = data_buf.as_ptr() as *const T;
let s: &'a [T] = unsafe { from_raw_parts(ptr, len) };
......@@ -100,7 +100,7 @@ pub fn follow_cast_ref<'a, T: Sized + 'a>(buf: &'a [u8], loc: usize) -> &'a T {
impl<'a> Follow<'a> for &'a str {
type Inner = &'a str;
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let len = read_scalar::<UOffsetT>(&buf[loc..loc + SIZE_UOFFSET]) as usize;
let len = read_scalar_at::<UOffsetT>(&buf, loc) as usize;
let slice = &buf[loc + SIZE_UOFFSET..loc + SIZE_UOFFSET + len];
let s = unsafe { from_utf8_unchecked(slice) };
s
......@@ -111,7 +111,7 @@ impl<'a> Follow<'a> for &'a str {
fn follow_slice_helper<T>(buf: &[u8], loc: usize) -> &[T] {
let sz = size_of::<T>();
debug_assert!(sz > 0);
let len = read_scalar::<UOffsetT>(&buf[loc..loc + SIZE_UOFFSET]) as usize;
let len = read_scalar_at::<UOffsetT>(&buf, loc) as usize;
let data_buf = &buf[loc + SIZE_UOFFSET..loc + SIZE_UOFFSET + len * sz];
let ptr = data_buf.as_ptr() as *const T;
let s: &[T] = unsafe { from_raw_parts(ptr, len) };
......
......@@ -16,7 +16,7 @@
use std::ptr::write_bytes;
use endian_scalar::{emplace_scalar, read_scalar};
use endian_scalar::{emplace_scalar, read_scalar_at};
use primitives::*;
/// VTableWriter compartmentalizes actions needed to create a vtable.
......@@ -57,7 +57,7 @@ impl<'a> VTableWriter<'a> {
#[inline(always)]
pub fn get_field_offset(&self, vtable_offset: VOffsetT) -> VOffsetT {
let idx = vtable_offset as usize;
read_scalar::<VOffsetT>(&self.buf[idx..idx + SIZE_VOFFSET])
read_scalar_at::<VOffsetT>(&self.buf, idx)
}
/// Writes an object field offset into the vtable.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment