os_info/
bitness.rs

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// spell-checker:ignore getconf

use std::fmt::{self, Display, Formatter};
#[cfg(any(
    target_os = "aix",
    target_os = "dragonfly",
    target_os = "freebsd",
    target_os = "illumos",
    target_os = "linux",
    target_os = "macos",
    target_os = "netbsd",
    target_os = "openbsd"
))]
use std::process::{Command, Output};

/// Operating system architecture in terms of how many bits compose the basic values it can deal with.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Bitness {
    /// Unknown bitness (unable to determine).
    Unknown,
    /// 32-bit.
    X32,
    /// 64-bit.
    X64,
}

impl Display for Bitness {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match *self {
            Bitness::Unknown => write!(f, "unknown bitness"),
            Bitness::X32 => write!(f, "32-bit"),
            Bitness::X64 => write!(f, "64-bit"),
        }
    }
}

#[cfg(any(
    target_os = "dragonfly",
    target_os = "freebsd",
    target_os = "linux",
    target_os = "macos",
))]
pub fn get() -> Bitness {
    match &Command::new("getconf").arg("LONG_BIT").output() {
        Ok(Output { stdout, .. }) if stdout == b"32\n" => Bitness::X32,
        Ok(Output { stdout, .. }) if stdout == b"64\n" => Bitness::X64,
        _ => Bitness::Unknown,
    }
}

#[cfg(target_os = "netbsd")]
pub fn get() -> Bitness {
    match &Command::new("sysctl")
        .arg("-n")
        .arg("hw.machine_arch")
        .output()
    {
        Ok(Output { stdout, .. }) if stdout == b"amd64\n" => Bitness::X64,
        Ok(Output { stdout, .. }) if stdout == b"x86_64\n" => Bitness::X64,
        Ok(Output { stdout, .. }) if stdout == b"i386\n" => Bitness::X32,
        Ok(Output { stdout, .. }) if stdout == b"aarch64\n" => Bitness::X64,
        Ok(Output { stdout, .. }) if stdout == b"earmv7hf\n" => Bitness::X32,
        Ok(Output { stdout, .. }) if stdout == b"sparc64\n" => Bitness::X64,
        _ => Bitness::Unknown,
    }
}

#[cfg(target_os = "openbsd")]
pub fn get() -> Bitness {
    match &Command::new("sysctl").arg("-n").arg("hw.machine").output() {
        Ok(Output { stdout, .. }) if stdout == b"amd64\n" => Bitness::X64,
        Ok(Output { stdout, .. }) if stdout == b"x86_64\n" => Bitness::X64,
        Ok(Output { stdout, .. }) if stdout == b"i386\n" => Bitness::X32,
        Ok(Output { stdout, .. }) if stdout == b"aarch64\n" => Bitness::X64,
        Ok(Output { stdout, .. }) if stdout == b"earmv7hf\n" => Bitness::X32,
        Ok(Output { stdout, .. }) if stdout == b"sparc64\n" => Bitness::X64,
        _ => Bitness::Unknown,
    }
}

#[cfg(target_os = "illumos")]
pub fn get() -> Bitness {
    match &Command::new("isainfo").arg("-b").output() {
        Ok(Output { stdout, .. }) if stdout == b"64\n" => Bitness::X64,
        Ok(Output { stdout, .. }) if stdout == b"32\n" => Bitness::X32,
        _ => Bitness::Unknown,
    }
}

#[cfg(target_os = "aix")]
pub fn get() -> Bitness {
    match &Command::new("prtconf").arg("-c").output() {
        Ok(Output { stdout, .. }) if stdout == b"CPU Type: 64-bit\n" => Bitness::X64,
        Ok(Output { stdout, .. }) if stdout == b"CPU Type: 32-bit\n" => Bitness::X32,
        _ => Bitness::Unknown,
    }
}

#[cfg(all(
    test,
    any(
        target_os = "aix",
        target_os = "dragonfly",
        target_os = "freebsd",
        target_os = "linux",
        target_os = "macos",
        target_os = "netbsd",
        target_os = "openbsd"
    )
))]
mod tests {
    use super::*;
    use pretty_assertions::assert_ne;

    #[test]
    fn get_bitness() {
        let b = get();
        assert_ne!(b, Bitness::Unknown);
    }

    #[test]
    fn display() {
        let data = [
            (Bitness::Unknown, "unknown bitness"),
            (Bitness::X32, "32-bit"),
            (Bitness::X64, "64-bit"),
        ];

        for (bitness, expected) in &data {
            assert_eq!(&bitness.to_string(), expected);
        }
    }
}