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