#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum OwnershipKind {
Owned = 0,
Strong = 1,
Weak = 2,
Ref = 3,
}
impl OwnershipKind {
pub const ALL: [OwnershipKind; 4] = [
OwnershipKind::Owned,
OwnershipKind::Strong,
OwnershipKind::Weak,
OwnershipKind::Ref,
];
#[inline]
pub const fn as_u8(self) -> u8 {
self as u8
}
#[inline]
pub const fn from_u8(v: u8) -> Option<Self> {
match v {
0 => Some(OwnershipKind::Owned),
1 => Some(OwnershipKind::Strong),
2 => Some(OwnershipKind::Weak),
3 => Some(OwnershipKind::Ref),
_ => None,
}
}
pub const fn is_owning(self) -> bool {
matches!(self, OwnershipKind::Owned | OwnershipKind::Strong)
}
}
impl From<OwnershipKind> for u8 {
#[inline]
fn from(k: OwnershipKind) -> u8 {
k.as_u8()
}
}
impl TryFrom<u8> for OwnershipKind {
type Error = InvalidOwnershipKind;
#[inline]
fn try_from(v: u8) -> Result<Self, InvalidOwnershipKind> {
OwnershipKind::from_u8(v).ok_or(InvalidOwnershipKind(v))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InvalidOwnershipKind(pub u8);
impl core::fmt::Display for InvalidOwnershipKind {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "invalid OwnershipKind discriminant: {}", self.0)
}
}
impl std::error::Error for InvalidOwnershipKind {}