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::cmp::max; |
| 18 | use core::mem::{align_of, size_of}; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 19 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 20 | use crate::endian_scalar::emplace_scalar; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 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. |
| 25 | pub trait Push: Sized { |
| 26 | type Output; |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 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); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 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 | |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 42 | impl<'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 Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 58 | /// Ensure Push alignment calculations are typesafe (because this helps reduce |
| 59 | /// implementation issues when using FlatBufferBuilder::align). |
| 60 | pub struct PushAlignment(usize); |
| 61 | impl PushAlignment { |
| 62 | #[inline] |
| 63 | pub fn new(x: usize) -> Self { |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 64 | PushAlignment(x) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 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. |
| 77 | macro_rules! impl_push_for_endian_scalar { |
| 78 | ($ty:ident) => { |
| 79 | impl Push for $ty { |
| 80 | type Output = $ty; |
| 81 | |
| 82 | #[inline] |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 83 | unsafe fn push(&self, dst: &mut [u8], _written_len: usize) { |
| 84 | emplace_scalar::<$ty>(dst, *self); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | }; |
| 88 | } |
| 89 | |
| 90 | impl_push_for_endian_scalar!(bool); |
| 91 | impl_push_for_endian_scalar!(u8); |
| 92 | impl_push_for_endian_scalar!(i8); |
| 93 | impl_push_for_endian_scalar!(u16); |
| 94 | impl_push_for_endian_scalar!(i16); |
| 95 | impl_push_for_endian_scalar!(u32); |
| 96 | impl_push_for_endian_scalar!(i32); |
| 97 | impl_push_for_endian_scalar!(u64); |
| 98 | impl_push_for_endian_scalar!(i64); |
| 99 | impl_push_for_endian_scalar!(f32); |
| 100 | impl_push_for_endian_scalar!(f64); |