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, WireU32, WireU64,
9};
10
11use zerocopy::IntoBytes;
12
13/// A FIDL protocol message header
14#[derive(Clone, Copy, Debug, IntoBytes)]
15#[repr(C)]
16pub struct WireMessageHeader {
17    /// The transaction ID of the message header
18    pub txid: WireU32,
19    /// Flags
20    pub flags: [u8; 3],
21    /// Magic number
22    pub magic_number: u8,
23    /// The ordinal of the message following this header
24    pub ordinal: WireU64,
25}
26
27unsafe impl Wire for WireMessageHeader {
28    type Decoded<'de> = Self;
29
30    #[inline]
31    fn zero_padding(_: &mut MaybeUninit<Self>) {
32        // Wire message headers have no padding
33    }
34}
35
36/// The flag 0 bit indicating that the wire format is v2.
37pub const FLAG_0_WIRE_FORMAT_V2_BIT: u8 = 0b0000_0010;
38
39/// The magic number indicating FIDL protocol compatibility.
40pub const MAGIC_NUMBER: u8 = 0x01;
41
42impl Encodable for WireMessageHeader {
43    type Encoded = WireMessageHeader;
44}
45
46unsafe impl<E: ?Sized> Encode<E> for WireMessageHeader {
47    #[inline]
48    fn encode(
49        self,
50        encoder: &mut E,
51        out: &mut MaybeUninit<Self::Encoded>,
52    ) -> Result<(), EncodeError> {
53        self.encode_ref(encoder, out)
54    }
55}
56
57unsafe impl<E: ?Sized> EncodeRef<E> for WireMessageHeader {
58    #[inline]
59    fn encode_ref(
60        &self,
61        _: &mut E,
62        out: &mut MaybeUninit<Self::Encoded>,
63    ) -> Result<(), EncodeError> {
64        out.write(*self);
65        Ok(())
66    }
67}
68
69unsafe impl<D: ?Sized> Decode<D> for WireMessageHeader {
70    #[inline]
71    fn decode(_: Slot<'_, Self>, _: &mut D) -> Result<(), DecodeError> {
72        Ok(())
73    }
74}