lite_strtab/types/
string_index.rs1use core::fmt::{Debug, Display};
11
12pub trait StringIndex:
21 private::Sealed + Copy + Eq + Ord + Debug + Display + Send + Sync + 'static
22{
23 const TYPE_NAME: &'static str;
25
26 fn try_from_usize(value: usize) -> Option<Self>;
28
29 fn to_usize(self) -> usize;
31}
32
33macro_rules! impl_string_index {
38 ($($ty:ty),+ $(,)?) => {
39 $(
40 impl StringIndex for $ty {
41 const TYPE_NAME: &'static str = stringify!($ty);
42
43 #[inline]
44 fn try_from_usize(value: usize) -> Option<Self> {
45 <Self as core::convert::TryFrom<usize>>::try_from(value).ok()
46 }
47
48 #[inline]
49 fn to_usize(self) -> usize {
50 self as usize
51 }
52 }
53 )+
54 };
55}
56
57#[cfg(target_pointer_width = "64")]
58impl_string_index!(u8, u16, u32, u64, usize);
59
60#[cfg(target_pointer_width = "32")]
61impl_string_index!(u8, u16, u32, usize);
62
63#[cfg(target_pointer_width = "16")]
64impl_string_index!(u8, u16, usize);
65
66#[cfg(not(any(
68 target_pointer_width = "16",
69 target_pointer_width = "32",
70 target_pointer_width = "64"
71)))]
72compile_error!("lite-strtab requires a 16-bit, 32-bit, or 64-bit target");
73
74mod private {
76 pub trait Sealed {}
77
78 macro_rules! impl_sealed {
79 ($($ty:ty),+ $(,)?) => {
80 $(
81 impl Sealed for $ty {}
82 )+
83 };
84 }
85
86 #[cfg(target_pointer_width = "64")]
87 impl_sealed!(u8, u16, u32, u64, usize);
88
89 #[cfg(target_pointer_width = "32")]
90 impl_sealed!(u8, u16, u32, usize);
91
92 #[cfg(target_pointer_width = "16")]
93 impl_sealed!(u8, u16, usize);
94}