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
use std::fmt::{self, Display, Formatter};
#[cfg(any(target_os = "linux", target_os = "macos"))]
use std::process::{Command, Output};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Bitness {
Unknown,
X32,
X64,
}
impl Display for Bitness {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
Bitness::Unknown => write!(f, "unknown"),
Bitness::X32 => write!(f, "32-bit"),
Bitness::X64 => write!(f, "64-bit"),
}
}
}
#[cfg(any(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(all(test, any(target_os = "linux", target_os = "macos")))]
mod tests {
use super::*;
use pretty_assertions::assert_ne;
#[test]
fn get_bitness() {
let b = get();
assert_ne!(b, Bitness::Unknown);
}
}