pecos_core/sets/
element.rs1use core::fmt::Debug;
14use core::hash::Hash;
15
16pub trait Element: Debug + Clone + Ord + Copy + Hash {}
17
18impl<T: Debug + Clone + Ord + Copy + Hash> Element for T {}
20
21#[allow(clippy::module_name_repetitions)]
22pub trait IndexableElement: Element {
23 fn to_usize(&self) -> usize;
24 fn from_usize(value: usize) -> Self;
25}
26
27macro_rules! impl_indexable_element_safe {
28 ($t:ty) => {
29 impl IndexableElement for $t {
30 #[allow(clippy::as_conversions, clippy::cast_possible_truncation)]
31 #[inline(always)]
32 fn to_usize(&self) -> usize {
33 *self as usize
34 }
35
36 #[allow(clippy::as_conversions, clippy::cast_possible_truncation)]
37 #[inline(always)]
38 fn from_usize(value: usize) -> Self {
39 value as $t
40 }
41 }
42 };
43}
44
45impl_indexable_element_safe!(u8);
47impl_indexable_element_safe!(u16);
48impl_indexable_element_safe!(u32);
49impl_indexable_element_safe!(usize);
50
51#[cfg(target_pointer_width = "64")]
53impl_indexable_element_safe!(u64);
54
55#[cfg(target_pointer_width = "32")]
56impl IndexableElement for u64 {
57 #[inline(always)]
58 fn to_usize(&self) -> usize {
59 usize::try_from(*self).expect("u64 value too large for 32-bit usize")
60 }
61
62 #[allow(clippy::as_conversions)]
63 #[inline(always)]
64 fn from_usize(value: usize) -> Self {
65 value as u64
66 }
67}
68
69impl IndexableElement for u128 {
71 #[inline(always)]
72 fn to_usize(&self) -> usize {
73 panic!("u128 cannot be safely converted to usize without potential data loss")
74 }
75
76 #[inline(always)]
77 fn from_usize(_value: usize) -> Self {
78 panic!("usize cannot be safely converted to u128 on all platforms")
79 }
80}