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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use core::num::{
NonZeroU16,
NonZeroU32,
NonZeroUsize,
};
pub trait Symbol: Copy + Eq {
fn try_from_usize(index: usize) -> Option<Self>;
fn to_usize(self) -> usize;
}
pub(crate) fn expect_valid_symbol<S>(index: usize) -> S
where
S: Symbol,
{
S::try_from_usize(index).expect("encountered invalid symbol")
}
pub type DefaultSymbol = SymbolU32;
macro_rules! gen_symbol_for {
(
$( #[$doc:meta] )*
struct $name:ident($non_zero:ty; $base_ty:ty);
) => {
$( #[$doc] )*
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $name {
value: $non_zero,
}
impl Symbol for $name {
#[inline]
fn try_from_usize(index: usize) -> Option<Self> {
if index < usize::MAX {
return Some(Self {
value: unsafe { <$non_zero>::new_unchecked(index as $base_ty + 1) },
})
}
None
}
#[inline]
fn to_usize(self) -> usize {
self.value.get() as usize - 1
}
}
};
}
gen_symbol_for!(
struct SymbolU16(NonZeroU16; u16);
);
gen_symbol_for!(
struct SymbolU32(NonZeroU32; u32);
);
gen_symbol_for!(
struct SymbolUsize(NonZeroUsize; usize);
);
#[cfg(test)]
mod tests {
use super::*;
use core::mem::size_of;
#[test]
fn same_size_as_u32() {
assert_eq!(size_of::<DefaultSymbol>(), size_of::<u32>());
}
#[test]
fn same_size_as_optional() {
assert_eq!(
size_of::<DefaultSymbol>(),
size_of::<Option<DefaultSymbol>>()
);
}
}