Debian Codegraph @

src/target/arch.rs

raw
1 //! Rust architectures
2
3 use crate::error::Error;
4 use core::{fmt, str::FromStr};
5
6 #[cfg(feature = "serde")]
7 use serde::{de, de::Error as DeError, ser, Deserialize, Serialize};
8
9 /// `target_arch`: Target CPU architecture
10 #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
11 #[non_exhaustive]
12 pub enum Arch {
13 /// `aarch64`: ARMv8 64-bit architecture
14 AArch64,
15
16 /// `amdgpu`
17 Amdgpu,
18
19 /// `arm`: 32-bit ARM architecture
20 Arm,
21
22 /// `arm64ec`
23 Arm64ec,
24
25 /// `avr`
26 Avr,
27
28 /// `bpf`
29 Bpf,
30
31 /// `csky`
32 Csky,
33
34 /// `hexagon`
35 Hexagon,
36
37 /// `loongarch32`
38 Loongarch32,
39
40 /// `loongarch64`
41 Loongarch64,
42
43 /// `m68k`
44 M68k,
45
46 /// `mips`: 32-bit MIPS CPU architecture
47 Mips,
48
49 /// `mips32r6`
50 Mips32r6,
51
52 /// `mips64`: 64-bit MIPS CPU architecture
53 Mips64,
54
55 /// `mips64r6`
56 Mips64r6,
57
58 /// `msp430`: 16-bit MSP430 microcontrollers
59 Msp430,
60
61 /// `nvptx64`: 64-bit NVIDIA PTX
62 Nvptx64,
63
64 /// `powerpc`: 32-bit POWERPC platform
65 PowerPc,
66
67 /// `powerpc64`: 64-bit POWERPC platform
68 PowerPc64,
69
70 /// `riscv32`
71 Riscv32,
72
73 /// `riscv64`
74 Riscv64,
75
76 /// `s390x`: 64-bit IBM z/Architecture
77 S390X,
78
79 /// `sparc`: 32-bit SPARC CPU architecture
80 Sparc,
81
82 /// `sparc64`: 64-bit SPARC CPU architecture
83 Sparc64,
84
85 /// `wasm32`: Web Assembly (32-bit)
86 Wasm32,
87
88 /// `wasm64`
89 Wasm64,
90
91 /// `x86`: Generic x86 CPU architecture
92 X86,
93
94 /// `x86_64`: 'AMD64' CPU architecture
95 X86_64,
96
97 /// `xtensa`
98 Xtensa,
99 }
100
101 impl Arch {
102 /// String representing this `Arch` which matches `#[cfg(target_arch)]`
103 pub fn as_str(self) -> &'static str {
104 match self {
105 Arch::AArch64 => "aarch64",
106 Arch::Amdgpu => "amdgpu",
107 Arch::Arm => "arm",
108 Arch::Arm64ec => "arm64ec",
109 Arch::Avr => "avr",
110 Arch::Bpf => "bpf",
111 Arch::Csky => "csky",
112 Arch::Hexagon => "hexagon",
113 Arch::Loongarch32 => "loongarch32",
114 Arch::Loongarch64 => "loongarch64",
115 Arch::M68k => "m68k",
116 Arch::Mips => "mips",
117 Arch::Mips32r6 => "mips32r6",
118 Arch::Mips64 => "mips64",
119 Arch::Mips64r6 => "mips64r6",
120 Arch::Msp430 => "msp430",
121 Arch::Nvptx64 => "nvptx64",
122 Arch::PowerPc => "powerpc",
123 Arch::PowerPc64 => "powerpc64",
124 Arch::Riscv32 => "riscv32",
125 Arch::Riscv64 => "riscv64",
126 Arch::S390X => "s390x",
127 Arch::Sparc => "sparc",
128 Arch::Sparc64 => "sparc64",
129 Arch::Wasm32 => "wasm32",
130 Arch::Wasm64 => "wasm64",
131 Arch::X86 => "x86",
132 Arch::X86_64 => "x86_64",
133 Arch::Xtensa => "xtensa",
134 }
135 }
136 }
137
138 impl FromStr for Arch {
139 type Err = Error;
140
141 /// Create a new `Arch` from the given string
142 fn from_str(name: &str) -> Result<Self, Self::Err> {
143 let result = match name {
144 "aarch64" => Arch::AArch64,
145 "amdgpu" => Arch::Amdgpu,
146 "arm" => Arch::Arm,
147 "arm64ec" => Arch::Arm64ec,
148 "avr" => Arch::Avr,
149 "bpf" => Arch::Bpf,
150 "csky" => Arch::Csky,
151 "hexagon" => Arch::Hexagon,
152 "loongarch32" => Arch::Loongarch32,
153 "loongarch64" => Arch::Loongarch64,
154 "m68k" => Arch::M68k,
155 "mips" => Arch::Mips,
156 "mips32r6" => Arch::Mips32r6,
157 "mips64" => Arch::Mips64,
158 "mips64r6" => Arch::Mips64r6,
159 "msp430" => Arch::Msp430,
160 "nvptx64" => Arch::Nvptx64,
161 "powerpc" => Arch::PowerPc,
162 "powerpc64" => Arch::PowerPc64,
163 "riscv32" => Arch::Riscv32,
164 "riscv64" => Arch::Riscv64,
165 "s390x" => Arch::S390X,
166 "sparc" => Arch::Sparc,
167 "sparc64" => Arch::Sparc64,
168 "wasm32" => Arch::Wasm32,
169 "wasm64" => Arch::Wasm64,
170 "x86" => Arch::X86,
171 "x86_64" => Arch::X86_64,
172 "xtensa" => Arch::Xtensa,
173 _ => return Err(Error),
174 };
175
176 Ok(result)
177 }
178 }
179
180 impl fmt::Display for Arch {
181 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
182 f.write_str(self.as_str())
183 }
184 }
185
186 #[cfg(feature = "serde")]
187 impl Serialize for Arch {
188 fn serialize<S: ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
189 serializer.serialize_str(self.as_str())
190 }
191 }
192
193 #[cfg(all(feature = "serde", feature = "std"))]
194 impl<'de> Deserialize<'de> for Arch {
195 fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
196 let string = std::string::String::deserialize(deserializer)?;
197 string.parse().map_err(|_| {
198 D::Error::custom(std::format!(
199 "Unrecognized value '{}' for target_arch",
200 string
201 ))
202 })
203 }
204 }