Skip to main content

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