stable_id_traits/
cast_usize.rs

1use crate::CastUsize;
2
3impl CastUsize for u8 {
4    fn cast_to(self) -> usize {
5        self as usize
6    }
7
8    fn cast_from(val: usize) -> Self {
9        assert!(val < Self::max_value().into());
10
11        val as Self
12    }
13}
14
15impl CastUsize for u16 {
16    fn cast_to(self) -> usize {
17        self as usize
18    }
19
20    fn cast_from(val: usize) -> Self {
21        assert!(val < Self::max_value().into());
22
23        val as Self
24    }
25}
26
27#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
28impl CastUsize for u32 {
29    fn cast_to(self) -> usize {
30        self as usize
31    }
32
33    fn cast_from(val: usize) -> Self {
34        assert!(val < Self::max_value() as usize);
35
36        val as Self
37    }
38}
39
40#[cfg(any(target_pointer_width = "64"))]
41impl CastUsize for u64 {
42    fn cast_to(self) -> usize {
43        self as usize
44    }
45
46    fn cast_from(val: usize) -> Self {
47        assert!(val < Self::max_value() as usize);
48
49        val as Self
50    }
51}
52
53impl CastUsize for usize {
54    fn cast_to(self) -> usize {
55        self
56    }
57
58    fn cast_from(val: usize) -> Self {
59        val
60    }
61}