1 | //! Target triple support. |
2 | |
3 | #![deny (missing_docs, trivial_numeric_casts, unused_extern_crates)] |
4 | #![warn (unused_import_braces)] |
5 | #![cfg_attr ( |
6 | feature = "cargo-clippy" , |
7 | warn( |
8 | clippy::float_arithmetic, |
9 | clippy::mut_mut, |
10 | clippy::nonminimal_bool, |
11 | clippy::option_map_unwrap_or, |
12 | clippy::option_map_unwrap_or_else, |
13 | clippy::print_stdout, |
14 | clippy::unicode_not_nfc, |
15 | clippy::use_self, |
16 | ) |
17 | )] |
18 | #![cfg_attr (not(feature = "std" ), no_std)] |
19 | |
20 | #[cfg (not(feature = "std" ))] |
21 | extern crate alloc; |
22 | #[cfg (feature = "std" )] |
23 | extern crate std as alloc; |
24 | |
25 | mod data_model; |
26 | mod host; |
27 | mod parse_error; |
28 | mod targets; |
29 | #[macro_use ] |
30 | mod triple; |
31 | |
32 | pub use self::data_model::{CDataModel, Size}; |
33 | pub use self::host::HOST; |
34 | pub use self::parse_error::ParseError; |
35 | pub use self::targets::{ |
36 | Aarch64Architecture, Architecture, ArmArchitecture, BinaryFormat, CustomVendor, Environment, |
37 | Mips32Architecture, Mips64Architecture, OperatingSystem, Riscv32Architecture, |
38 | Riscv64Architecture, Vendor, X86_32Architecture, |
39 | }; |
40 | pub use self::triple::{CallingConvention, Endianness, PointerWidth, Triple}; |
41 | |
42 | /// A simple wrapper around `Triple` that provides an implementation of |
43 | /// `Default` which defaults to `Triple::host()`. |
44 | #[derive (Clone, Debug, PartialEq, Eq, Hash)] |
45 | pub struct DefaultToHost(pub Triple); |
46 | |
47 | impl Default for DefaultToHost { |
48 | fn default() -> Self { |
49 | Self(Triple::host()) |
50 | } |
51 | } |
52 | |
53 | /// A simple wrapper around `Triple` that provides an implementation of |
54 | /// `Default` which defaults to `Triple::unknown()`. |
55 | #[derive (Clone, Debug, PartialEq, Eq, Hash)] |
56 | pub struct DefaultToUnknown(pub Triple); |
57 | |
58 | impl Default for DefaultToUnknown { |
59 | fn default() -> Self { |
60 | Self(Triple::unknown()) |
61 | } |
62 | } |
63 | |
64 | // For some reason, the below `serde` impls don't work when they're in the |
65 | // `triple` module. |
66 | |
67 | #[cfg (feature = "serde_support" )] |
68 | impl serde::Serialize for Triple { |
69 | fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { |
70 | serializer.serialize_str(&self.to_string()) |
71 | } |
72 | } |
73 | |
74 | #[cfg (feature = "serde_support" )] |
75 | impl<'de> serde::de::Deserialize<'de> for Triple { |
76 | fn deserialize<D: serde::de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { |
77 | let s = String::deserialize(deserializer)?; |
78 | s.parse().map_err(serde::de::Error::custom) |
79 | } |
80 | } |
81 | |
82 | #[cfg (feature = "serde_support" )] |
83 | #[test ] |
84 | fn test_serialize() { |
85 | let triples: Vec<Triple> = vec![ |
86 | "x86_64-unknown-linux-gnu" .parse().unwrap(), |
87 | "i686-pc-windows-gnu" .parse().unwrap(), |
88 | ]; |
89 | |
90 | let json = serde_json::to_string(&triples).unwrap(); |
91 | assert_eq!( |
92 | json, |
93 | r#"["x86_64-unknown-linux-gnu","i686-pc-windows-gnu"]"# |
94 | ); |
95 | } |
96 | |
97 | #[cfg (feature = "serde_support" )] |
98 | #[test ] |
99 | fn test_deserialize() { |
100 | let triples: Vec<Triple> = vec![ |
101 | "x86_64-unknown-linux-gnu" .parse().unwrap(), |
102 | "i686-pc-windows-gnu" .parse().unwrap(), |
103 | ]; |
104 | |
105 | let vals: Vec<Triple> = |
106 | serde_json::from_str(r#"["x86_64-unknown-linux-gnu","i686-pc-windows-gnu"]"# ).unwrap(); |
107 | |
108 | assert_eq!(vals, triples); |
109 | } |
110 | |