Skip to main content

fidl_next_protocol/wire/
empty.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    Constrained, Decode, Encode, EncodeError, FromWire, FromWireRef, IntoNatural, Slot,
9    ValidationError, Wire,
10};
11
12/// The wire type for an empty message body.
13#[derive(Clone, Copy, Debug)]
14#[repr(C)]
15pub struct EmptyMessageBody;
16
17impl Constrained for EmptyMessageBody {
18    type Constraint = ();
19
20    fn validate(_: Slot<'_, Self>, _: Self::Constraint) -> Result<(), ValidationError> {
21        Ok(())
22    }
23}
24
25unsafe impl Wire for EmptyMessageBody {
26    type Narrowed<'de> = Self;
27
28    #[inline]
29    fn zero_padding(_: &mut MaybeUninit<Self>) {
30        // Empty message bodies have no padding
31    }
32}
33
34unsafe impl<E: ?Sized> Encode<EmptyMessageBody, E> for () {
35    #[inline]
36    fn encode(
37        self,
38        _: &mut E,
39        _: &mut MaybeUninit<EmptyMessageBody>,
40        _: (),
41    ) -> Result<(), EncodeError> {
42        Ok(())
43    }
44}
45
46unsafe impl<E: ?Sized> Encode<EmptyMessageBody, E> for &() {
47    #[inline]
48    fn encode(
49        self,
50        encoder: &mut E,
51        out: &mut MaybeUninit<EmptyMessageBody>,
52        _: (),
53    ) -> Result<(), EncodeError> {
54        Encode::encode((), encoder, out, ())
55    }
56}
57
58unsafe impl<D: ?Sized> Decode<D> for EmptyMessageBody {
59    #[inline]
60    fn decode(
61        _: Slot<'_, Self>,
62        _: &mut D,
63        _: Self::Constraint,
64    ) -> Result<(), fidl_next_codec::DecodeError> {
65        Ok(())
66    }
67}
68
69impl FromWire<EmptyMessageBody> for () {
70    fn from_wire(_: EmptyMessageBody) -> Self {}
71}
72
73impl FromWireRef<EmptyMessageBody> for () {
74    fn from_wire_ref(_: &EmptyMessageBody) -> Self {}
75}
76
77impl IntoNatural for EmptyMessageBody {
78    type Natural = ();
79}