use core::num::NonZeroU32;
use std::cmp::Ordering;
use std::fmt::{Debug, Display, Formatter};
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct ClapId(NonZeroU32);
impl ClapId {
#[inline]
pub const fn new(id: u32) -> Self {
match Self::from_raw(id) {
None => panic!("Invalid ClapId"),
Some(id) => id,
}
}
#[inline]
pub const fn from_raw(raw: u32) -> Option<Self> {
match NonZeroU32::new(raw.wrapping_add(1)) {
Some(x) => Some(Self(x)),
None => None,
}
}
#[inline]
pub const fn get(self) -> u32 {
self.0.get().wrapping_sub(1)
}
#[inline]
pub const fn optional_to_raw(value: Option<ClapId>) -> u32 {
match value {
None => u32::MAX,
Some(value) => value.get(),
}
}
}
impl Debug for ClapId {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("ClapId").field(&self.get()).finish()
}
}
impl Display for ClapId {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.get(), f)
}
}
impl From<u32> for ClapId {
#[inline]
fn from(value: u32) -> Self {
ClapId::new(value)
}
}
impl From<ClapId> for u32 {
#[inline]
fn from(value: ClapId) -> Self {
value.get()
}
}
impl PartialEq<u32> for ClapId {
#[inline]
fn eq(&self, other: &u32) -> bool {
self.get() == *other
}
}
impl PartialEq<ClapId> for u32 {
#[inline]
fn eq(&self, other: &ClapId) -> bool {
*self == other.get()
}
}
impl PartialOrd<u32> for ClapId {
#[inline]
fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
self.get().partial_cmp(other)
}
}
impl PartialOrd<ClapId> for u32 {
#[inline]
fn partial_cmp(&self, other: &ClapId) -> Option<Ordering> {
self.partial_cmp(&other.get())
}
}
impl PartialEq<Option<ClapId>> for ClapId {
#[inline]
fn eq(&self, other: &Option<ClapId>) -> bool {
match other {
None => false,
Some(other) => self.0 == other.0,
}
}
}
impl PartialEq<ClapId> for Option<ClapId> {
#[inline]
fn eq(&self, other: &ClapId) -> bool {
match self {
None => false,
Some(s) => s.0 == other.0,
}
}
}