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
use std::convert::TryInto;
#[derive(Clone, Debug, Copy)]
#[repr(u8)]
pub enum Architecture {
#[cfg(any(
target_pointer_width = "8",
target_pointer_width = "16",
target_pointer_width = "32",
target_pointer_width = "64",
target_pointer_width = "128"
))]
Arch8Bit = 1,
#[cfg(any(
target_pointer_width = "16",
target_pointer_width = "32",
target_pointer_width = "64",
target_pointer_width = "128"
))]
Arch16Bit = 2,
#[cfg(any(
target_pointer_width = "32",
target_pointer_width = "64",
target_pointer_width = "128"
))]
Arch32Bit = 4,
#[cfg(any(target_pointer_width = "64", target_pointer_width = "128"))]
Arch64Bit = 8,
#[cfg(target_pointer_width = "128")]
Arch128Bit = 16,
}
impl Architecture {
#[must_use]
pub fn from_native() -> Architecture {
#[cfg(target_pointer_width = "8")]
return Architecture::Arch8Bit;
#[cfg(target_pointer_width = "16")]
return Architecture::Arch16Bit;
#[cfg(target_pointer_width = "32")]
return Architecture::Arch32Bit;
#[cfg(target_pointer_width = "64")]
return Architecture::Arch64Bit;
#[cfg(target_pointer_width = "128")]
return Architecture::Arch128Bit;
}
#[must_use]
pub fn pointer_from_ne_bytes(self, bytes: &[u8]) -> usize {
match self {
#[allow(clippy::cast_possible_truncation)]
#[cfg(any(
target_pointer_width = "8",
target_pointer_width = "16",
target_pointer_width = "32",
target_pointer_width = "64",
target_pointer_width = "128"
))]
Architecture::Arch8Bit => u8::from_ne_bytes(bytes.try_into().unwrap()) as usize,
#[allow(clippy::cast_possible_truncation)]
#[cfg(any(
target_pointer_width = "16",
target_pointer_width = "32",
target_pointer_width = "64",
target_pointer_width = "128"
))]
Architecture::Arch16Bit => u16::from_ne_bytes(bytes.try_into().unwrap()) as usize,
#[allow(clippy::cast_possible_truncation)]
#[cfg(any(
target_pointer_width = "32",
target_pointer_width = "64",
target_pointer_width = "128"
))]
Architecture::Arch32Bit => u32::from_ne_bytes(bytes.try_into().unwrap()) as usize,
#[allow(clippy::cast_possible_truncation)]
#[cfg(any(target_pointer_width = "64", target_pointer_width = "128"))]
Architecture::Arch64Bit => u64::from_ne_bytes(bytes.try_into().unwrap()) as usize,
#[allow(clippy::cast_possible_truncation)]
#[cfg(target_pointer_width = "128")]
Architecture::Arch128Bit => u128::from_ne_bytes(bytes.try_into().unwrap()) as usize,
}
}
}