Skip to main content

fidl_next_protocol/wire/
epitaph.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, DecodeError, Encode, EncodeError, Slot, ValidationError, Wire,
9};
10use zerocopy::IntoBytes;
11
12use crate::wire;
13
14/// A FIDL protocol epitaph.
15#[derive(Clone, Copy, Debug, IntoBytes)]
16#[repr(C)]
17pub struct Epitaph {
18    /// The error status.
19    pub error: wire::Int32,
20}
21
22impl Epitaph {
23    /// Returns a new epitaph with the given error.
24    pub fn new(error: i32) -> Self {
25        Self { error: wire::Int32(error) }
26    }
27}
28
29impl Constrained for Epitaph {
30    type Constraint = ();
31
32    fn validate(_: Slot<'_, Self>, _: Self::Constraint) -> Result<(), ValidationError> {
33        Ok(())
34    }
35}
36
37unsafe impl Wire for Epitaph {
38    type Narrowed<'de> = Self;
39
40    #[inline]
41    fn zero_padding(_: &mut MaybeUninit<Self>) {
42        // Wire epitaphs have no padding
43    }
44}
45
46unsafe impl<E: ?Sized> Encode<Epitaph, E> for Epitaph {
47    #[inline]
48    fn encode(self, _: &mut E, out: &mut MaybeUninit<Epitaph>, _: ()) -> Result<(), EncodeError> {
49        out.write(self);
50        Ok(())
51    }
52}
53
54unsafe impl<E: ?Sized> Encode<Epitaph, E> for &Epitaph {
55    #[inline]
56    fn encode(
57        self,
58        encoder: &mut E,
59        out: &mut MaybeUninit<Epitaph>,
60        constraint: (),
61    ) -> Result<(), EncodeError> {
62        Encode::encode(*self, encoder, out, constraint)
63    }
64}
65
66unsafe impl<D: ?Sized> Decode<D> for Epitaph {
67    #[inline]
68    fn decode(_: Slot<'_, Self>, _: &mut D, _: ()) -> Result<(), DecodeError> {
69        Ok(())
70    }
71}