fidl_next_protocol/
wire.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
5use core::mem::MaybeUninit;
6
7use fidl_next_codec::{
8    Decode, DecodeError, Encodable, Encode, EncodeError, EncodeRef, Slot, Wire, WireI32, WireU32,
9    WireU64,
10};
11
12use zerocopy::IntoBytes;
13
14/// A FIDL protocol message header
15#[derive(Clone, Copy, Debug, IntoBytes)]
16#[repr(C)]
17pub struct WireMessageHeader {
18    /// The transaction ID of the message header
19    pub txid: WireU32,
20    /// Flags
21    pub flags: [u8; 3],
22    /// Magic number
23    pub magic_number: u8,
24    /// The ordinal of the message following this header
25    pub ordinal: WireU64,
26}
27
28unsafe impl Wire for WireMessageHeader {
29    type Decoded<'de> = Self;
30
31    #[inline]
32    fn zero_padding(_: &mut MaybeUninit<Self>) {
33        // Wire message headers have no padding
34    }
35}
36
37/// The flag 0 bit indicating that the wire format is v2.
38pub const FLAG_0_WIRE_FORMAT_V2_BIT: u8 = 0b0000_0010;
39
40/// The magic number indicating FIDL protocol compatibility.
41pub const MAGIC_NUMBER: u8 = 0x01;
42
43impl Encodable for WireMessageHeader {
44    type Encoded = WireMessageHeader;
45}
46
47unsafe impl<E: ?Sized> Encode<E> for WireMessageHeader {
48    #[inline]
49    fn encode(
50        self,
51        encoder: &mut E,
52        out: &mut MaybeUninit<Self::Encoded>,
53    ) -> Result<(), EncodeError> {
54        self.encode_ref(encoder, out)
55    }
56}
57
58unsafe impl<E: ?Sized> EncodeRef<E> for WireMessageHeader {
59    #[inline]
60    fn encode_ref(
61        &self,
62        _: &mut E,
63        out: &mut MaybeUninit<Self::Encoded>,
64    ) -> Result<(), EncodeError> {
65        out.write(*self);
66        Ok(())
67    }
68}
69
70unsafe impl<D: ?Sized> Decode<D> for WireMessageHeader {
71    #[inline]
72    fn decode(_: Slot<'_, Self>, _: &mut D) -> Result<(), DecodeError> {
73        Ok(())
74    }
75}
76
77/// A FIDL protocol epitaph.
78#[derive(Clone, Copy, Debug, IntoBytes)]
79#[repr(C)]
80pub struct WireEpitaph {
81    /// The error status.
82    pub error: WireI32,
83}
84
85unsafe impl Wire for WireEpitaph {
86    type Decoded<'de> = Self;
87
88    #[inline]
89    fn zero_padding(_: &mut MaybeUninit<Self>) {
90        // Wire epitaphs have no padding
91    }
92}
93
94impl Encodable for WireEpitaph {
95    type Encoded = Self;
96}
97
98unsafe impl<E: ?Sized> Encode<E> for WireEpitaph {
99    #[inline]
100    fn encode(
101        self,
102        encoder: &mut E,
103        out: &mut MaybeUninit<Self::Encoded>,
104    ) -> Result<(), EncodeError> {
105        self.encode_ref(encoder, out)
106    }
107}
108
109unsafe impl<E: ?Sized> EncodeRef<E> for WireEpitaph {
110    #[inline]
111    fn encode_ref(
112        &self,
113        _: &mut E,
114        out: &mut MaybeUninit<Self::Encoded>,
115    ) -> Result<(), EncodeError> {
116        out.write(*self);
117        Ok(())
118    }
119}
120
121unsafe impl<D: ?Sized> Decode<D> for WireEpitaph {
122    #[inline]
123    fn decode(_: Slot<'_, Self>, _: &mut D) -> Result<(), DecodeError> {
124        Ok(())
125    }
126}