fidl_next_codec/wire/
mod.rs

1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5mod boxed;
6mod empty_struct;
7mod envelope;
8mod ptr;
9mod result;
10mod string;
11mod table;
12mod union;
13mod vec;
14
15pub use self::boxed::*;
16pub use self::empty_struct::*;
17pub use self::envelope::*;
18pub use self::ptr::*;
19pub use self::result::*;
20pub use self::string::*;
21pub use self::table::*;
22pub use self::union::*;
23pub use self::vec::*;
24
25use core::mem::MaybeUninit;
26
27use crate::{WireF32, WireF64, WireI16, WireI32, WireI64, WireU16, WireU32, WireU64};
28
29/// A FIDL wire type.
30///
31/// # Safety
32///
33/// - References to decoded data yielded by `Self::Decoded<'de>` must not outlive `'de`.
34/// - `zero_padding` must write zeroes to (at least) the padding bytes of `out`.
35pub unsafe trait Wire: 'static + Sized {
36    /// The decoded wire type, restricted to the `'de` lifetime.
37    type Decoded<'de>: 'de;
38
39    /// Writes zeroes to the padding for this type, if any.
40    fn zero_padding(out: &mut MaybeUninit<Self>);
41}
42
43macro_rules! impl_primitive {
44    ($ty:ty) => {
45        unsafe impl Wire for $ty {
46            type Decoded<'de> = Self;
47
48            #[inline]
49            fn zero_padding(_: &mut MaybeUninit<Self>) {}
50        }
51    };
52}
53
54macro_rules! impl_primitives {
55    ($($ty:ty),* $(,)?) => {
56        $(
57            impl_primitive!($ty);
58        )*
59    }
60}
61
62impl_primitives! {
63    (),
64    bool,
65    i8, WireI16, WireI32, WireI64,
66    u8, WireU16, WireU32, WireU64,
67    WireF32, WireF64,
68}
69
70unsafe impl<T: Wire, const N: usize> Wire for [T; N] {
71    type Decoded<'de> = [T::Decoded<'de>; N];
72
73    #[inline]
74    fn zero_padding(out: &mut MaybeUninit<Self>) {
75        for i in 0..N {
76            let out_i = unsafe { &mut *out.as_mut_ptr().cast::<MaybeUninit<T>>().add(i) };
77            T::zero_padding(out_i);
78        }
79    }
80}