#![cfg_attr(feature = "external_doc", cfg_attr(all(), doc = include_str!("../README.md")))]
#![cfg_attr(
not(feature = "external_doc"),
doc = "See <https://docs.rs/num_enum> for more info about this crate."
)]
#![cfg_attr(not(feature = "std"), no_std)]
pub use ::num_enum_derive::{
Default, FromPrimitive, IntoPrimitive, TryFromPrimitive, UnsafeFromPrimitive,
};
use ::core::fmt;
pub trait FromPrimitive: Sized {
type Primitive: Copy + Eq;
fn from_primitive(number: Self::Primitive) -> Self;
}
pub trait TryFromPrimitive: Sized {
type Primitive: Copy + Eq + fmt::Debug;
type Error;
const NAME: &'static str;
fn try_from_primitive(number: Self::Primitive) -> Result<Self, Self::Error>;
}
pub trait UnsafeFromPrimitive: Sized {
type Primitive: Copy + Eq;
#[deprecated(
since = "0.6.0",
note = "Prefer to use `unchecked_transmute_from`, `from_unchecked` will be removed in a future release."
)]
unsafe fn from_unchecked(number: Self::Primitive) -> Self {
Self::unchecked_transmute_from(number)
}
unsafe fn unchecked_transmute_from(number: Self::Primitive) -> Self;
}
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct TryFromPrimitiveError<Enum: TryFromPrimitive> {
pub number: Enum::Primitive,
}
impl<Enum: TryFromPrimitive> TryFromPrimitiveError<Enum> {
pub fn new(number: Enum::Primitive) -> Self {
Self { number }
}
}
impl<Enum: TryFromPrimitive> fmt::Debug for TryFromPrimitiveError<Enum> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("TryFromPrimitiveError")
.field("number", &self.number)
.finish()
}
}
impl<Enum: TryFromPrimitive> fmt::Display for TryFromPrimitiveError<Enum> {
fn fmt(&self, stream: &'_ mut fmt::Formatter<'_>) -> fmt::Result {
write!(
stream,
"No discriminant in enum `{name}` matches the value `{input:?}`",
name = Enum::NAME,
input = self.number,
)
}
}
#[cfg(feature = "std")]
impl<Enum: TryFromPrimitive> ::std::error::Error for TryFromPrimitiveError<Enum> {}
#[doc(hidden)]
pub trait CannotDeriveBothFromPrimitiveAndTryFromPrimitive {}