1#![cfg_attr(not(test), no_std)]
2
3use thiserror::Error;
4
5#[doc(hidden)]
6#[path = "private.rs"]
7pub mod __private;
8#[doc(inline)]
9pub use macros::ffi_enum;
10
11pub mod prelude {
13 pub use crate::{ffi_enum, FfiEnum as _, FfiEnumExt as _};
14}
15
16pub trait FfiEnum: Copy + Eq {
18 type Enum: TryFrom<Self, Error = Error> + Into<Self>;
20
21 type Repr: Copy + From<Self> + Into<Self>;
23
24 const UNKNOWN: Self;
29}
30
31pub trait FfiEnumExt: FfiEnum {
33 fn is_known(self) -> bool;
35
36 fn repr(self) -> Self::Repr;
38
39 fn into_enum(self) -> Self::Enum;
41}
42
43impl<T: FfiEnum> FfiEnumExt for T {
44 #[inline(always)]
45 fn is_known(self) -> bool {
46 Self::Enum::try_from(self).is_ok()
47 }
48
49 #[inline(always)]
50 fn repr(self) -> Self::Repr {
51 self.into()
52 }
53
54 #[inline(always)]
55 fn into_enum(self) -> Self::Enum {
56 self.try_into().unwrap()
57 }
58}
59
60#[derive(Debug, Error, Clone, Copy, PartialEq, Eq)]
62pub enum Error {
63 #[error("value of ffi_enum type is an unknown variant")]
65 UnknownVariant,
66 #[error(transparent)]
68 TryFromInt(#[from] core::num::TryFromIntError),
69}