1 /*
2 * Copyright 2018 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 use std::marker::PhantomData;
18 use std::mem::size_of;
19 use std::slice::from_raw_parts;
20 use std::str::from_utf8_unchecked;
21
22 use endian_scalar::{EndianScalar, read_scalar};
23 use follow::Follow;
24 use primitives::*;
25
26 #[derive(Debug)]
27 pub struct Vector<'a, T: 'a>(&'a [u8], usize, PhantomData<T>);
28
29 impl<'a, T: 'a> Vector<'a, T> {
30 #[inline(always)]
new(buf: &'a [u8], loc: usize) -> Self31 pub fn new(buf: &'a [u8], loc: usize) -> Self {
32 Vector {
33 0: buf,
34 1: loc,
35 2: PhantomData,
36 }
37 }
38
39 #[inline(always)]
len(&self) -> usize40 pub fn len(&self) -> usize {
41 read_scalar::<UOffsetT>(&self.0[self.1 as usize..]) as usize
42 }
43 }
44
45 impl<'a, T: Follow<'a> + 'a> Vector<'a, T> {
46 #[inline(always)]
get(&self, idx: usize) -> T::Inner47 pub fn get(&self, idx: usize) -> T::Inner {
48 debug_assert!(idx < read_scalar::<u32>(&self.0[self.1 as usize..]) as usize);
49 let sz = size_of::<T>();
50 debug_assert!(sz > 0);
51 T::follow(self.0, self.1 as usize + SIZE_UOFFSET + sz * idx)
52 }
53 }
54
55 pub trait SafeSliceAccess {}
56 impl<'a, T: SafeSliceAccess + 'a> Vector<'a, T> {
safe_slice(self) -> &'a [T]57 pub fn safe_slice(self) -> &'a [T] {
58 let buf = self.0;
59 let loc = self.1;
60 let sz = size_of::<T>();
61 debug_assert!(sz > 0);
62 let len = read_scalar::<UOffsetT>(&buf[loc..loc + SIZE_UOFFSET]) as usize;
63 let data_buf = &buf[loc + SIZE_UOFFSET..loc + SIZE_UOFFSET + len * sz];
64 let ptr = data_buf.as_ptr() as *const T;
65 let s: &'a [T] = unsafe { from_raw_parts(ptr, len) };
66 s
67 }
68 }
69
70 impl SafeSliceAccess for u8 {}
71 impl SafeSliceAccess for i8 {}
72 impl SafeSliceAccess for bool {}
73
74 #[cfg(target_endian = "little")]
75 mod le_safe_slice_impls {
76 impl super::SafeSliceAccess for u16 {}
77 impl super::SafeSliceAccess for u32 {}
78 impl super::SafeSliceAccess for u64 {}
79
80 impl super::SafeSliceAccess for i16 {}
81 impl super::SafeSliceAccess for i32 {}
82 impl super::SafeSliceAccess for i64 {}
83
84 impl super::SafeSliceAccess for f32 {}
85 impl super::SafeSliceAccess for f64 {}
86 }
87
88 pub use self::le_safe_slice_impls::*;
89
follow_cast_ref<'a, T: Sized + 'a>(buf: &'a [u8], loc: usize) -> &'a T90 pub fn follow_cast_ref<'a, T: Sized + 'a>(buf: &'a [u8], loc: usize) -> &'a T {
91 let sz = size_of::<T>();
92 let buf = &buf[loc..loc + sz];
93 let ptr = buf.as_ptr() as *const T;
94 unsafe { &*ptr }
95 }
96
97 impl<'a> Follow<'a> for &'a str {
98 type Inner = &'a str;
follow(buf: &'a [u8], loc: usize) -> Self::Inner99 fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
100 let len = read_scalar::<UOffsetT>(&buf[loc..loc + SIZE_UOFFSET]) as usize;
101 let slice = &buf[loc + SIZE_UOFFSET..loc + SIZE_UOFFSET + len];
102 let s = unsafe { from_utf8_unchecked(slice) };
103 s
104 }
105 }
106
follow_slice_helper<T>(buf: &[u8], loc: usize) -> &[T]107 fn follow_slice_helper<T>(buf: &[u8], loc: usize) -> &[T] {
108 let sz = size_of::<T>();
109 debug_assert!(sz > 0);
110 let len = read_scalar::<UOffsetT>(&buf[loc..loc + SIZE_UOFFSET]) as usize;
111 let data_buf = &buf[loc + SIZE_UOFFSET..loc + SIZE_UOFFSET + len * sz];
112 let ptr = data_buf.as_ptr() as *const T;
113 let s: &[T] = unsafe { from_raw_parts(ptr, len) };
114 s
115 }
116
117 /// Implement direct slice access if the host is little-endian.
118 #[cfg(target_endian = "little")]
119 impl<'a, T: EndianScalar> Follow<'a> for &'a [T] {
120 type Inner = &'a [T];
follow(buf: &'a [u8], loc: usize) -> Self::Inner121 fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
122 follow_slice_helper::<T>(buf, loc)
123 }
124 }
125
126 /// Implement Follow for all possible Vectors that have Follow-able elements.
127 impl<'a, T: Follow<'a> + 'a> Follow<'a> for Vector<'a, T> {
128 type Inner = Vector<'a, T>;
follow(buf: &'a [u8], loc: usize) -> Self::Inner129 fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
130 Vector::new(buf, loc)
131 }
132 }
133
134