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    Constrained, Decode, DecodeError, Encodable, Encode, EncodeError, EncodeRef, FromWire,
10    FromWireRef, IntoNatural, Slot, 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 Decoded<'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
64impl Encodable for FrameworkError {
65    type Encoded = WireFrameworkError;
66}
67
68unsafe impl<E: ?Sized> Encode<E> for FrameworkError {
69    fn encode(
70        self,
71        encoder: &mut E,
72        out: &mut MaybeUninit<Self::Encoded>,
73        constraint: <Self::Encoded as Constrained>::Constraint,
74    ) -> Result<(), EncodeError> {
75        self.encode_ref(encoder, out, constraint)
76    }
77}
78
79unsafe impl<E: ?Sized> EncodeRef<E> for FrameworkError {
80    fn encode_ref(
81        &self,
82        _: &mut E,
83        out: &mut MaybeUninit<Self::Encoded>,
84        _constraint: <Self::Encoded as Constrained>::Constraint,
85    ) -> Result<(), EncodeError> {
86        munge!(let WireFrameworkError { inner } = out);
87        inner.write(WireI32(match self {
88            Self::UnknownMethod => -2,
89        }));
90
91        Ok(())
92    }
93}
94
95impl FromWire<WireFrameworkError> for FrameworkError {
96    #[inline]
97    fn from_wire(wire: WireFrameworkError) -> Self {
98        Self::from_wire_ref(&wire)
99    }
100}
101
102impl IntoNatural for WireFrameworkError {
103    type Natural = FrameworkError;
104}
105
106impl FromWireRef<WireFrameworkError> for FrameworkError {
107    #[inline]
108    fn from_wire_ref(wire: &WireFrameworkError) -> Self {
109        Self::from(*wire)
110    }
111}