target_lexicon/
lib.rs

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"))]
21extern crate alloc;
22#[cfg(feature = "std")]
23extern crate std as alloc;
24
25mod data_model;
26mod host;
27mod parse_error;
28mod targets;
29#[macro_use]
30mod triple;
31
32pub use self::data_model::{CDataModel, Size};
33pub use self::host::HOST;
34pub use self::parse_error::ParseError;
35pub use self::targets::{
36    Aarch64Architecture, Architecture, ArmArchitecture, BinaryFormat, CustomVendor, Environment,
37    Mips32Architecture, Mips64Architecture, OperatingSystem, Riscv32Architecture,
38    Riscv64Architecture, Vendor, X86_32Architecture,
39};
40pub 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)]
45pub struct DefaultToHost(pub Triple);
46
47impl 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)]
56pub struct DefaultToUnknown(pub Triple);
57
58impl 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")]
68impl 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")]
75impl<'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]
84fn 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]
99fn 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}