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, Encode, EncodeError, Slot, Unconstrained, Wire, WireI32, WireU32, WireU64,
9    bitflags,
10};
11
12use zerocopy::IntoBytes;
13
14/// The transactional message header flags in byte 0.
15#[derive(Clone, Copy, Debug, IntoBytes)]
16#[repr(transparent)]
17pub struct MessageHeaderFlags0(u8);
18
19/// The transactional message header flags in byte 1.
20#[derive(Clone, Copy, Debug, IntoBytes)]
21#[repr(transparent)]
22pub struct MessageHeaderFlags1(u8);
23
24/// The transactional message header flags in byte 2.
25#[derive(Clone, Copy, Debug, IntoBytes)]
26#[repr(transparent)]
27pub struct MessageHeaderFlags2(u8);
28
29bitflags::bitflags! {
30    impl MessageHeaderFlags0: u8 {
31        /// The bit set to indicate that the FIDL wire format is version 2.
32        const WIRE_FORMAT_V2 = 1 << 1;
33    }
34
35    impl MessageHeaderFlags1: u8 {
36    }
37
38    impl MessageHeaderFlags2: u8 {
39        /// The bit set to indicate that the FIDL method is flexible.
40        const FLEXIBLE_METHOD = 1 << 7;
41    }
42}
43
44/// A FIDL protocol message header
45#[derive(Clone, Copy, Debug, IntoBytes)]
46#[repr(C)]
47pub struct WireMessageHeader {
48    /// The transaction ID of the message header
49    pub txid: WireU32,
50    /// Flags byte 0
51    pub flags_0: MessageHeaderFlags0,
52    /// Flags byte 1
53    pub flags_1: MessageHeaderFlags1,
54    /// Flags byte 2
55    pub flags_2: MessageHeaderFlags2,
56    /// Magic number
57    pub magic_number: u8,
58    /// The ordinal of the message following this header
59    pub ordinal: WireU64,
60}
61
62unsafe impl Wire for WireMessageHeader {
63    type Owned<'de> = Self;
64
65    #[inline]
66    fn zero_padding(_: &mut MaybeUninit<Self>) {
67        // Wire message headers have no padding
68    }
69}
70
71unsafe impl<E: ?Sized> Encode<WireMessageHeader, E> for WireMessageHeader {
72    #[inline]
73    fn encode(
74        self,
75        _: &mut E,
76        out: &mut MaybeUninit<WireMessageHeader>,
77        _: (),
78    ) -> Result<(), EncodeError> {
79        out.write(self);
80        Ok(())
81    }
82}
83
84unsafe impl<E: ?Sized> Encode<WireMessageHeader, E> for &WireMessageHeader {
85    #[inline]
86    fn encode(
87        self,
88        encoder: &mut E,
89        out: &mut MaybeUninit<WireMessageHeader>,
90        constraint: (),
91    ) -> Result<(), EncodeError> {
92        Encode::encode(*self, encoder, out, constraint)
93    }
94}
95
96impl Unconstrained for WireMessageHeader {}
97
98unsafe impl<D: ?Sized> Decode<D> for WireMessageHeader {
99    #[inline]
100    fn decode(_: Slot<'_, Self>, _: &mut D, _: ()) -> Result<(), DecodeError> {
101        Ok(())
102    }
103}
104
105/// A FIDL protocol epitaph.
106#[derive(Clone, Copy, Debug, IntoBytes)]
107#[repr(C)]
108pub struct WireEpitaph {
109    /// The error status.
110    pub error: WireI32,
111}
112
113unsafe impl Wire for WireEpitaph {
114    type Owned<'de> = Self;
115
116    #[inline]
117    fn zero_padding(_: &mut MaybeUninit<Self>) {
118        // Wire epitaphs have no padding
119    }
120}
121
122unsafe impl<E: ?Sized> Encode<WireEpitaph, E> for WireEpitaph {
123    #[inline]
124    fn encode(
125        self,
126        _: &mut E,
127        out: &mut MaybeUninit<WireEpitaph>,
128        _: (),
129    ) -> Result<(), EncodeError> {
130        out.write(self);
131        Ok(())
132    }
133}
134
135unsafe impl<E: ?Sized> Encode<WireEpitaph, E> for &WireEpitaph {
136    #[inline]
137    fn encode(
138        self,
139        encoder: &mut E,
140        out: &mut MaybeUninit<WireEpitaph>,
141        constraint: (),
142    ) -> Result<(), EncodeError> {
143        Encode::encode(*self, encoder, out, constraint)
144    }
145}
146
147impl Unconstrained for WireEpitaph {}
148
149unsafe impl<D: ?Sized> Decode<D> for WireEpitaph {
150    #[inline]
151    fn decode(_: Slot<'_, Self>, _: &mut D, _: ()) -> Result<(), DecodeError> {
152        Ok(())
153    }
154}