1use std::fmt::{Debug, Formatter};
2use std::num::{NonZeroU16, NonZeroU8};
3
4use crate::name_of;
5
6#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
8#[repr(transparent)]
9pub struct ConnHandle(NonZeroU16);
10
11impl ConnHandle {
12 pub(crate) const BITS: u16 = 12;
14 const MAX: u16 = 0xEFF;
16
17 #[inline]
19 #[must_use]
20 pub(crate) fn new(mut v: u16) -> Option<Self> {
21 v &= (1 << Self::BITS) - 1;
22 (v <= Self::MAX).then_some(Self(unsafe { NonZeroU16::new_unchecked(!v) }))
24 }
25}
26
27impl From<ConnHandle> for u16 {
28 #[inline]
29 fn from(cn: ConnHandle) -> Self {
30 !cn.0.get()
31 }
32}
33
34impl Debug for ConnHandle {
35 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
36 write!(f, "{}({:#05X})", name_of!(ConnHandle), u16::from(*self))
37 }
38}
39
40#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
42#[repr(transparent)]
43pub struct AdvHandle(NonZeroU8);
44
45impl AdvHandle {
46 pub(super) const MIN: u8 = 0x00;
48 pub(super) const MAX: u8 = 0xEF;
50
51 #[inline]
53 #[must_use]
54 pub(super) fn new(v: u8) -> Option<Self> {
55 (v <= Self::MAX).then_some(Self(unsafe { NonZeroU8::new_unchecked(!v) }))
57 }
58}
59
60impl From<AdvHandle> for u8 {
61 #[inline]
62 fn from(h: AdvHandle) -> Self {
63 !h.0.get()
64 }
65}
66
67impl Debug for AdvHandle {
68 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
69 write!(f, "{}({:#04X})", name_of!(AdvHandle), u8::from(*self))
70 }
71}
72
73crate::impl_display_via_debug! { ConnHandle, AdvHandle }