flatbuffers_reflection/
struct.rs

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
17use flatbuffers::Follow;
18
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
20pub struct Struct<'a> {
21    buf: &'a [u8],
22    loc: usize,
23}
24
25impl<'a> Struct<'a> {
26    #[inline]
27    pub fn buf(&self) -> &'a [u8] {
28        self.buf
29    }
30
31    #[inline]
32    pub fn loc(&self) -> usize {
33        self.loc
34    }
35
36    /// # Safety
37    ///
38    /// [buf] must contain a valid struct at [loc]
39    #[inline]
40    pub unsafe fn new(buf: &'a [u8], loc: usize) -> Self {
41        Struct { buf, loc }
42    }
43
44    /// Retrieves the value at the provided [byte_loc]. No field in [Struct] is optional.
45    ///
46    /// # Safety
47    ///
48    /// The value of the corresponding slot must have type T
49    #[inline]
50    pub unsafe fn get<T: Follow<'a> + 'a>(&self, byte_loc: usize) -> T::Inner {
51        <T>::follow(self.buf, self.loc + byte_loc)
52    }
53}
54
55impl<'a> Follow<'a> for Struct<'a> {
56    type Inner = Struct<'a>;
57    #[inline]
58    unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
59        Struct { buf, loc }
60    }
61}