flatbuffers/
push.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 core::cmp::max;
18use core::mem::{align_of, size_of};
19
20use crate::endian_scalar::emplace_scalar;
21
22/// Trait to abstract over functionality needed to write values (either owned
23/// or referenced). Used in FlatBufferBuilder and implemented for generated
24/// types.
25pub trait Push: Sized {
26    type Output;
27
28    /// # Safety
29    ///
30    /// dst is aligned to [`Self::alignment`] and has length greater than or equal to [`Self::size`]
31    unsafe fn push(&self, dst: &mut [u8], written_len: usize);
32    #[inline]
33    fn size() -> usize {
34        size_of::<Self::Output>()
35    }
36    #[inline]
37    fn alignment() -> PushAlignment {
38        PushAlignment::new(align_of::<Self::Output>())
39    }
40}
41
42impl<'a, T: Push> Push for &'a T {
43    type Output = T::Output;
44
45    unsafe fn push(&self, dst: &mut [u8], written_len: usize) {
46        T::push(self, dst, written_len)
47    }
48
49    fn size() -> usize {
50        T::size()
51    }
52
53    fn alignment() -> PushAlignment {
54        T::alignment()
55    }
56}
57
58/// Ensure Push alignment calculations are typesafe (because this helps reduce
59/// implementation issues when using FlatBufferBuilder::align).
60pub struct PushAlignment(usize);
61impl PushAlignment {
62    #[inline]
63    pub fn new(x: usize) -> Self {
64        PushAlignment(x)
65    }
66    #[inline]
67    pub fn value(&self) -> usize {
68        self.0
69    }
70    #[inline]
71    pub fn max_of(&self, o: usize) -> Self {
72        PushAlignment::new(max(self.0, o))
73    }
74}
75
76/// Macro to implement Push for EndianScalar types.
77macro_rules! impl_push_for_endian_scalar {
78    ($ty:ident) => {
79        impl Push for $ty {
80            type Output = $ty;
81
82            #[inline]
83            unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
84                emplace_scalar::<$ty>(dst, *self);
85            }
86        }
87    };
88}
89
90impl_push_for_endian_scalar!(bool);
91impl_push_for_endian_scalar!(u8);
92impl_push_for_endian_scalar!(i8);
93impl_push_for_endian_scalar!(u16);
94impl_push_for_endian_scalar!(i16);
95impl_push_for_endian_scalar!(u32);
96impl_push_for_endian_scalar!(i32);
97impl_push_for_endian_scalar!(u64);
98impl_push_for_endian_scalar!(i64);
99impl_push_for_endian_scalar!(f32);
100impl_push_for_endian_scalar!(f64);