Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 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 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 17 | use core::marker::PhantomData; |
| 18 | use core::mem::size_of; |
| 19 | use core::ops::Deref; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 20 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 21 | use crate::endian_scalar::{emplace_scalar, read_scalar, read_scalar_at}; |
| 22 | use crate::follow::Follow; |
| 23 | use crate::push::Push; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 24 | |
| 25 | pub const FLATBUFFERS_MAX_BUFFER_SIZE: usize = (1u64 << 31) as usize; |
| 26 | |
| 27 | pub const FILE_IDENTIFIER_LENGTH: usize = 4; |
| 28 | |
| 29 | pub const VTABLE_METADATA_FIELDS: usize = 2; |
| 30 | |
| 31 | pub const SIZE_U8: usize = size_of::<u8>(); |
| 32 | pub const SIZE_I8: usize = size_of::<i8>(); |
| 33 | |
| 34 | pub const SIZE_U16: usize = size_of::<u16>(); |
| 35 | pub const SIZE_I16: usize = size_of::<i16>(); |
| 36 | |
| 37 | pub const SIZE_U32: usize = size_of::<u32>(); |
| 38 | pub const SIZE_I32: usize = size_of::<i32>(); |
| 39 | |
| 40 | pub const SIZE_U64: usize = size_of::<u64>(); |
| 41 | pub const SIZE_I64: usize = size_of::<i64>(); |
| 42 | |
| 43 | pub const SIZE_F32: usize = size_of::<f32>(); |
| 44 | pub const SIZE_F64: usize = size_of::<f64>(); |
| 45 | |
| 46 | pub const SIZE_SOFFSET: usize = SIZE_I32; |
| 47 | pub const SIZE_UOFFSET: usize = SIZE_U32; |
| 48 | pub const SIZE_VOFFSET: usize = SIZE_I16; |
| 49 | |
| 50 | pub const SIZE_SIZEPREFIX: usize = SIZE_UOFFSET; |
| 51 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 52 | /// SOffsetT is a relative pointer from tables to their vtables. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 53 | pub type SOffsetT = i32; |
| 54 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 55 | /// UOffsetT is used represent both for relative pointers and lengths of vectors. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 56 | pub type UOffsetT = u32; |
| 57 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 58 | /// VOffsetT is a relative pointer in vtables to point from tables to field data. |
| 59 | pub type VOffsetT = u16; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 60 | |
| 61 | /// TableFinishedWIPOffset marks a WIPOffset as being for a finished table. |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 62 | #[derive(Clone, Copy)] |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 63 | pub struct TableFinishedWIPOffset {} |
| 64 | |
| 65 | /// TableUnfinishedWIPOffset marks a WIPOffset as being for an unfinished table. |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 66 | #[derive(Clone, Copy)] |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 67 | pub struct TableUnfinishedWIPOffset {} |
| 68 | |
| 69 | /// UnionWIPOffset marks a WIPOffset as being for a union value. |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 70 | #[derive(Clone, Copy)] |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 71 | pub struct UnionWIPOffset {} |
| 72 | |
| 73 | /// VTableWIPOffset marks a WIPOffset as being for a vtable. |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 74 | #[derive(Clone, Copy)] |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 75 | pub struct VTableWIPOffset {} |
| 76 | |
| 77 | /// WIPOffset contains an UOffsetT with a special meaning: it is the location of |
| 78 | /// data relative to the *end* of an in-progress FlatBuffer. The |
| 79 | /// FlatBufferBuilder uses this to track the location of objects in an absolute |
| 80 | /// way. The impl of Push converts a WIPOffset into a ForwardsUOffset. |
| 81 | #[derive(Debug)] |
| 82 | pub struct WIPOffset<T>(UOffsetT, PhantomData<T>); |
| 83 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 84 | // We cannot use derive for these two impls, as the derived impls would only |
| 85 | // implement `Copy` and `Clone` for `T: Copy` and `T: Clone` respectively. |
| 86 | // However `WIPOffset<T>` can always be copied, no matter that `T` you |
| 87 | // have. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 88 | impl<T> Copy for WIPOffset<T> {} |
| 89 | impl<T> Clone for WIPOffset<T> { |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 90 | #[inline(always)] |
| 91 | fn clone(&self) -> Self { |
| 92 | *self |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 95 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 96 | impl<T> Eq for WIPOffset<T> {} |
| 97 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 98 | impl<T> PartialEq for WIPOffset<T> { |
| 99 | fn eq(&self, o: &WIPOffset<T>) -> bool { |
| 100 | self.value() == o.value() |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | impl<T> Deref for WIPOffset<T> { |
| 105 | type Target = UOffsetT; |
| 106 | #[inline] |
| 107 | fn deref(&self) -> &UOffsetT { |
| 108 | &self.0 |
| 109 | } |
| 110 | } |
| 111 | impl<'a, T: 'a> WIPOffset<T> { |
| 112 | /// Create a new WIPOffset. |
| 113 | #[inline] |
| 114 | pub fn new(o: UOffsetT) -> WIPOffset<T> { |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 115 | WIPOffset(o, PhantomData) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /// Return a wrapped value that brings its meaning as a union WIPOffset |
| 119 | /// into the type system. |
| 120 | #[inline(always)] |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 121 | pub fn as_union_value(self) -> WIPOffset<UnionWIPOffset> { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 122 | WIPOffset::new(self.0) |
| 123 | } |
| 124 | /// Get the underlying value. |
| 125 | #[inline(always)] |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 126 | pub fn value(self) -> UOffsetT { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 127 | self.0 |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | impl<T> Push for WIPOffset<T> { |
| 132 | type Output = ForwardsUOffset<T>; |
| 133 | |
| 134 | #[inline(always)] |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 135 | unsafe fn push(&self, dst: &mut [u8], written_len: usize) { |
| 136 | let n = (SIZE_UOFFSET + written_len - self.value() as usize) as UOffsetT; |
| 137 | emplace_scalar::<UOffsetT>(dst, n); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | impl<T> Push for ForwardsUOffset<T> { |
| 142 | type Output = Self; |
| 143 | |
| 144 | #[inline(always)] |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 145 | unsafe fn push(&self, dst: &mut [u8], written_len: usize) { |
| 146 | self.value().push(dst, written_len); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | /// ForwardsUOffset is used by Follow to traverse a FlatBuffer: the pointer |
| 151 | /// is incremented by the value contained in this type. |
| 152 | #[derive(Debug)] |
| 153 | pub struct ForwardsUOffset<T>(UOffsetT, PhantomData<T>); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 154 | |
| 155 | // We cannot use derive for these two impls, as the derived impls would only |
| 156 | // implement `Copy` and `Clone` for `T: Copy` and `T: Clone` respectively. |
| 157 | // However `ForwardsUOffset<T>` can always be copied, no matter that `T` you |
| 158 | // have. |
| 159 | impl<T> Copy for ForwardsUOffset<T> {} |
| 160 | impl<T> Clone for ForwardsUOffset<T> { |
| 161 | #[inline(always)] |
| 162 | fn clone(&self) -> Self { |
| 163 | *self |
| 164 | } |
| 165 | } |
| 166 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 167 | impl<T> ForwardsUOffset<T> { |
| 168 | #[inline(always)] |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 169 | pub fn value(self) -> UOffsetT { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 170 | self.0 |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | impl<'a, T: Follow<'a>> Follow<'a> for ForwardsUOffset<T> { |
| 175 | type Inner = T::Inner; |
| 176 | #[inline(always)] |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 177 | unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 178 | let slice = &buf[loc..loc + SIZE_UOFFSET]; |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 179 | let off = read_scalar::<u32>(slice) as usize; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 180 | T::follow(buf, loc + off) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /// ForwardsVOffset is used by Follow to traverse a FlatBuffer: the pointer |
| 185 | /// is incremented by the value contained in this type. |
| 186 | #[derive(Debug)] |
| 187 | pub struct ForwardsVOffset<T>(VOffsetT, PhantomData<T>); |
| 188 | impl<T> ForwardsVOffset<T> { |
| 189 | #[inline(always)] |
| 190 | pub fn value(&self) -> VOffsetT { |
| 191 | self.0 |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | impl<'a, T: Follow<'a>> Follow<'a> for ForwardsVOffset<T> { |
| 196 | type Inner = T::Inner; |
| 197 | #[inline(always)] |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 198 | unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 199 | let slice = &buf[loc..loc + SIZE_VOFFSET]; |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 200 | let off = read_scalar::<VOffsetT>(slice) as usize; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 201 | T::follow(buf, loc + off) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | impl<T> Push for ForwardsVOffset<T> { |
| 206 | type Output = Self; |
| 207 | |
| 208 | #[inline] |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 209 | unsafe fn push(&self, dst: &mut [u8], written_len: usize) { |
| 210 | self.value().push(dst, written_len); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | /// ForwardsSOffset is used by Follow to traverse a FlatBuffer: the pointer |
| 215 | /// is incremented by the *negative* of the value contained in this type. |
| 216 | #[derive(Debug)] |
| 217 | pub struct BackwardsSOffset<T>(SOffsetT, PhantomData<T>); |
| 218 | impl<T> BackwardsSOffset<T> { |
| 219 | #[inline(always)] |
| 220 | pub fn value(&self) -> SOffsetT { |
| 221 | self.0 |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | impl<'a, T: Follow<'a>> Follow<'a> for BackwardsSOffset<T> { |
| 226 | type Inner = T::Inner; |
| 227 | #[inline(always)] |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 228 | unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 229 | let slice = &buf[loc..loc + SIZE_SOFFSET]; |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 230 | let off = read_scalar::<SOffsetT>(slice); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 231 | T::follow(buf, (loc as SOffsetT - off) as usize) |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | impl<T> Push for BackwardsSOffset<T> { |
| 236 | type Output = Self; |
| 237 | |
| 238 | #[inline] |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 239 | unsafe fn push(&self, dst: &mut [u8], written_len: usize) { |
| 240 | self.value().push(dst, written_len); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | /// SkipSizePrefix is used by Follow to traverse a FlatBuffer: the pointer is |
| 245 | /// incremented by a fixed constant in order to skip over the size prefix value. |
| 246 | pub struct SkipSizePrefix<T>(PhantomData<T>); |
| 247 | impl<'a, T: Follow<'a> + 'a> Follow<'a> for SkipSizePrefix<T> { |
| 248 | type Inner = T::Inner; |
| 249 | #[inline(always)] |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 250 | unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 251 | T::follow(buf, loc + SIZE_SIZEPREFIX) |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /// SkipRootOffset is used by Follow to traverse a FlatBuffer: the pointer is |
| 256 | /// incremented by a fixed constant in order to skip over the root offset value. |
| 257 | pub struct SkipRootOffset<T>(PhantomData<T>); |
| 258 | impl<'a, T: Follow<'a> + 'a> Follow<'a> for SkipRootOffset<T> { |
| 259 | type Inner = T::Inner; |
| 260 | #[inline(always)] |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 261 | unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 262 | T::follow(buf, loc + SIZE_UOFFSET) |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /// FileIdentifier is used by Follow to traverse a FlatBuffer: the pointer is |
| 267 | /// dereferenced into a byte slice, whose bytes are the file identifer value. |
| 268 | pub struct FileIdentifier; |
| 269 | impl<'a> Follow<'a> for FileIdentifier { |
| 270 | type Inner = &'a [u8]; |
| 271 | #[inline(always)] |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 272 | unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 273 | &buf[loc..loc + FILE_IDENTIFIER_LENGTH] |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /// SkipFileIdentifier is used by Follow to traverse a FlatBuffer: the pointer |
| 278 | /// is incremented by a fixed constant in order to skip over the file |
| 279 | /// identifier value. |
| 280 | pub struct SkipFileIdentifier<T>(PhantomData<T>); |
| 281 | impl<'a, T: Follow<'a> + 'a> Follow<'a> for SkipFileIdentifier<T> { |
| 282 | type Inner = T::Inner; |
| 283 | #[inline(always)] |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 284 | unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 285 | T::follow(buf, loc + FILE_IDENTIFIER_LENGTH) |
| 286 | } |
| 287 | } |
| 288 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 289 | impl<'a> Follow<'a> for bool { |
| 290 | type Inner = bool; |
| 291 | #[inline(always)] |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 292 | unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { |
| 293 | read_scalar_at::<u8>(buf, loc) != 0 |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 297 | /// Follow trait impls for primitive types. |
| 298 | /// |
| 299 | /// Ideally, these would be implemented as a single impl using trait bounds on |
| 300 | /// EndianScalar, but implementing Follow that way causes a conflict with |
| 301 | /// other impls. |
| 302 | macro_rules! impl_follow_for_endian_scalar { |
| 303 | ($ty:ident) => { |
| 304 | impl<'a> Follow<'a> for $ty { |
| 305 | type Inner = $ty; |
| 306 | #[inline(always)] |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 307 | unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { |
| 308 | read_scalar_at::<$ty>(buf, loc) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | }; |
| 312 | } |
| 313 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 314 | impl_follow_for_endian_scalar!(u8); |
| 315 | impl_follow_for_endian_scalar!(u16); |
| 316 | impl_follow_for_endian_scalar!(u32); |
| 317 | impl_follow_for_endian_scalar!(u64); |
| 318 | impl_follow_for_endian_scalar!(i8); |
| 319 | impl_follow_for_endian_scalar!(i16); |
| 320 | impl_follow_for_endian_scalar!(i32); |
| 321 | impl_follow_for_endian_scalar!(i64); |
| 322 | impl_follow_for_endian_scalar!(f32); |
| 323 | impl_follow_for_endian_scalar!(f64); |