blob: 20bacd8e2e5c58d80d54424818c5bd50c976d7cb [file] [log] [blame]
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001/*
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 Kuszmaul8e62b022022-03-22 09:33:25 -070017use core::cmp::max;
18use core::mem::{align_of, size_of};
Austin Schuhe89fa2d2019-08-14 20:24:23 -070019
Austin Schuh272c6132020-11-14 16:37:52 -080020use crate::endian_scalar::emplace_scalar;
Austin Schuhe89fa2d2019-08-14 20:24:23 -070021
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;
James Kuszmaul3b15b0c2022-11-08 14:03:16 -080027
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);
Austin Schuhe89fa2d2019-08-14 20:24:23 -070032 #[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
James Kuszmaul3b15b0c2022-11-08 14:03:16 -080042impl<'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
Austin Schuhe89fa2d2019-08-14 20:24:23 -070058/// 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 {
James Kuszmaul3b15b0c2022-11-08 14:03:16 -080064 PushAlignment(x)
Austin Schuhe89fa2d2019-08-14 20:24:23 -070065 }
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]
James Kuszmaul3b15b0c2022-11-08 14:03:16 -080083 unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
84 emplace_scalar::<$ty>(dst, *self);
Austin Schuhe89fa2d2019-08-14 20:24:23 -070085 }
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);