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, WireU32, WireU64,
9    ZeroPadding,
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 ZeroPadding for WireMessageHeader {
29    #[inline]
30    fn zero_padding(_: &mut MaybeUninit<Self>) {
31        // Wire message headers have no padding
32    }
33}
34
35/// The flag 0 bit indicating that the wire format is v2.
36pub const FLAG_0_WIRE_FORMAT_V2_BIT: u8 = 0b0000_0010;
37
38/// The magic number indicating FIDL protocol compatibility.
39pub const MAGIC_NUMBER: u8 = 0x01;
40
41impl Encodable for WireMessageHeader {
42    type Encoded = WireMessageHeader;
43}
44
45unsafe impl<E: ?Sized> Encode<E> for WireMessageHeader {
46    #[inline]
47    fn encode(
48        self,
49        encoder: &mut E,
50        out: &mut MaybeUninit<Self::Encoded>,
51    ) -> Result<(), EncodeError> {
52        self.encode_ref(encoder, out)
53    }
54}
55
56unsafe impl<E: ?Sized> EncodeRef<E> for WireMessageHeader {
57    #[inline]
58    fn encode_ref(
59        &self,
60        _: &mut E,
61        out: &mut MaybeUninit<Self::Encoded>,
62    ) -> Result<(), EncodeError> {
63        out.write(*self);
64        Ok(())
65    }
66}
67
68unsafe impl<D: ?Sized> Decode<D> for WireMessageHeader {
69    #[inline]
70    fn decode(_: Slot<'_, Self>, _: &mut D) -> Result<(), DecodeError> {
71        Ok(())
72    }
73}