1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use crate::{impl_wrapper_common, ConversionError, RawVal, Tag};
use core::cmp::Ordering;
use core::fmt::Debug;
use core::hash::{Hash, Hasher};
#[derive(Copy, Clone)]
pub struct BitSet(RawVal);
impl_wrapper_common!(BitSet);
#[derive(Debug)]
pub enum BitSetError {
TooManyBits(u64),
}
impl From<BitSetError> for ConversionError {
fn from(_: BitSetError) -> Self {
ConversionError
}
}
impl Hash for BitSet {
#[inline(always)]
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_raw().get_payload().hash(state);
}
}
impl PartialEq for BitSet {
#[inline(always)]
fn eq(&self, other: &Self) -> bool {
self.as_raw().get_payload() == other.as_raw().get_payload()
}
}
impl Eq for BitSet {}
impl PartialOrd for BitSet {
#[inline(always)]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for BitSet {
#[inline(always)]
fn cmp(&self, other: &Self) -> Ordering {
self.as_raw().get_body().cmp(&other.as_raw().get_body())
}
}
impl Debug for BitSet {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "BitSet({:#b})", self.as_raw().get_body())
}
}
impl BitSet {
#[inline(always)]
pub const fn try_from_u64(u: u64) -> Result<BitSet, BitSetError> {
if u & 0x0fff_ffff_ffff_ffff == u {
Ok(unsafe { Self::from_body(u) })
} else {
Err(BitSetError::TooManyBits(u))
}
}
#[inline(always)]
pub const fn to_u64(&self) -> u64 {
self.0.get_body()
}
}