use std::fmt::{Debug, Formatter};
use std::num::{NonZeroU16, NonZeroU8};
use crate::name_of;
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct ConnHandle(NonZeroU16);
impl ConnHandle {
pub(crate) const BITS: u16 = 12;
const MAX: u16 = 0xEFF;
#[inline]
#[must_use]
pub(crate) fn new(mut v: u16) -> Option<Self> {
v &= (1 << Self::BITS) - 1;
(v <= Self::MAX).then_some(Self(unsafe { NonZeroU16::new_unchecked(!v) }))
}
}
impl From<ConnHandle> for u16 {
#[inline]
fn from(cn: ConnHandle) -> Self {
!cn.0.get()
}
}
impl Debug for ConnHandle {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}({:#05X})", name_of!(ConnHandle), u16::from(*self))
}
}
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct AdvHandle(NonZeroU8);
impl AdvHandle {
pub(super) const MIN: u8 = 0x00;
pub(super) const MAX: u8 = 0xEF;
#[inline]
#[must_use]
pub(super) fn new(v: u8) -> Option<Self> {
(v <= Self::MAX).then_some(Self(unsafe { NonZeroU8::new_unchecked(!v) }))
}
}
impl From<AdvHandle> for u8 {
#[inline]
fn from(h: AdvHandle) -> Self {
!h.0.get()
}
}
impl Debug for AdvHandle {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}({:#04X})", name_of!(AdvHandle), u8::from(*self))
}
}
crate::impl_display_via_debug! { ConnHandle, AdvHandle }