fidl_next_protocol/
framework_error.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::fmt;
6use core::mem::MaybeUninit;
7
8use fidl_next_codec::{
9    Decode, DecodeError, Encode, EncodeError, FromWire, FromWireRef, IntoNatural, Slot,
10    Unconstrained, Wire, WireI32, munge,
11};
12
13use crate::concurrency::hint::unreachable_unchecked;
14
15/// An internal framework error.
16#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17#[repr(i32)]
18pub enum FrameworkError {
19    /// The protocol method was not recognized by the receiver.
20    UnknownMethod = -2,
21}
22
23/// An internal framework error.
24#[derive(Clone, Copy)]
25#[repr(transparent)]
26pub struct WireFrameworkError {
27    inner: WireI32,
28}
29
30unsafe impl Wire for WireFrameworkError {
31    type Owned<'de> = Self;
32
33    #[inline]
34    fn zero_padding(_: &mut MaybeUninit<Self>) {}
35}
36
37impl Unconstrained for WireFrameworkError {}
38
39impl fmt::Debug for WireFrameworkError {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        FrameworkError::from(*self).fmt(f)
42    }
43}
44
45impl From<WireFrameworkError> for FrameworkError {
46    fn from(value: WireFrameworkError) -> Self {
47        match *value.inner {
48            -2 => Self::UnknownMethod,
49            _ => unsafe { unreachable_unchecked() },
50        }
51    }
52}
53
54unsafe impl<D: ?Sized> Decode<D> for WireFrameworkError {
55    fn decode(slot: Slot<'_, Self>, _: &mut D, _: ()) -> Result<(), DecodeError> {
56        munge!(let Self { inner } = slot);
57        match **inner {
58            -2 => Ok(()),
59            code => Err(DecodeError::InvalidFrameworkError(code)),
60        }
61    }
62}
63
64unsafe impl<E: ?Sized> Encode<WireFrameworkError, E> for FrameworkError {
65    fn encode(
66        self,
67        _: &mut E,
68        out: &mut MaybeUninit<WireFrameworkError>,
69        _: (),
70    ) -> Result<(), EncodeError> {
71        munge!(let WireFrameworkError { inner } = out);
72        inner.write(WireI32(match self {
73            FrameworkError::UnknownMethod => -2,
74        }));
75
76        Ok(())
77    }
78}
79
80unsafe impl<E: ?Sized> Encode<WireFrameworkError, E> for &FrameworkError {
81    fn encode(
82        self,
83        encoder: &mut E,
84        out: &mut MaybeUninit<WireFrameworkError>,
85        constraint: (),
86    ) -> Result<(), EncodeError> {
87        Encode::encode(*self, encoder, out, constraint)
88    }
89}
90
91impl FromWire<WireFrameworkError> for FrameworkError {
92    #[inline]
93    fn from_wire(wire: WireFrameworkError) -> Self {
94        Self::from_wire_ref(&wire)
95    }
96}
97
98impl IntoNatural for WireFrameworkError {
99    type Natural = FrameworkError;
100}
101
102impl FromWireRef<WireFrameworkError> for FrameworkError {
103    #[inline]
104    fn from_wire_ref(wire: &WireFrameworkError) -> Self {
105        Self::from(*wire)
106    }
107}