Skip to content

Commit 7d31a77

Browse files
committed
Have UtsName getters return &OsStr
1 parent bc75389 commit 7d31a77

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

src/features.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub use self::os::*;
33

44
#[cfg(any(target_os = "linux", target_os = "android"))]
55
mod os {
6+
use std::os::unix::ffi::OsStrExt;
67
use crate::sys::utsname::uname;
78
use crate::Result;
89

@@ -31,7 +32,7 @@ mod os {
3132
let mut minor: usize = 0;
3233
let mut patch: usize = 0;
3334

34-
for &b in u.release() {
35+
for &b in u.release().as_bytes() {
3536
if curr >= 3 {
3637
break;
3738
}

src/sys/utsname.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Get system identification
22
use std::mem;
3+
use std::os::unix::ffi::OsStrExt;
4+
use std::ffi::OsStr;
35
use libc::c_char;
46
use crate::{Errno, Result};
57

@@ -10,27 +12,27 @@ pub struct UtsName(libc::utsname);
1012

1113
impl UtsName {
1214
/// Name of the operating system implementation.
13-
pub fn sysname(&self) -> &[u8] {
15+
pub fn sysname(&self) -> &OsStr {
1416
cast_and_trim(&self.0.sysname)
1517
}
1618

1719
/// Network name of this machine.
18-
pub fn nodename(&self) -> &[u8] {
20+
pub fn nodename(&self) -> &OsStr {
1921
cast_and_trim(&self.0.nodename)
2022
}
2123

2224
/// Release level of the operating system.
23-
pub fn release(&self) -> &[u8] {
25+
pub fn release(&self) -> &OsStr {
2426
cast_and_trim(&self.0.release)
2527
}
2628

2729
/// Version level of the operating system.
28-
pub fn version(&self) -> &[u8] {
30+
pub fn version(&self) -> &OsStr {
2931
cast_and_trim(&self.0.version)
3032
}
3133

3234
/// Machine hardware platform.
33-
pub fn machine(&self) -> &[u8] {
35+
pub fn machine(&self) -> &OsStr {
3436
cast_and_trim(&self.0.machine)
3537
}
3638
}
@@ -44,30 +46,32 @@ pub fn uname() -> Result<UtsName> {
4446
}
4547
}
4648

47-
fn cast_and_trim(slice: &[c_char]) -> &[u8] {
49+
fn cast_and_trim(slice: &[c_char]) -> &OsStr {
4850
let length = slice.iter().position(|&byte| byte == 0).unwrap_or(slice.len());
49-
unsafe {
51+
let bytes = unsafe {
5052
std::slice::from_raw_parts(slice.as_ptr().cast(), length)
51-
}
53+
};
54+
55+
OsStr::from_bytes(bytes)
5256
}
5357

5458
#[cfg(test)]
5559
mod test {
5660
#[cfg(target_os = "linux")]
5761
#[test]
5862
pub fn test_uname_linux() {
59-
assert_eq!(super::uname().unwrap().sysname(), b"Linux");
63+
assert_eq!(super::uname().unwrap().sysname(), "Linux");
6064
}
6165

6266
#[cfg(any(target_os = "macos", target_os = "ios"))]
6367
#[test]
6468
pub fn test_uname_darwin() {
65-
assert_eq!(super::uname().unwrap().sysname(), b"Darwin");
69+
assert_eq!(super::uname().unwrap().sysname(), "Darwin");
6670
}
6771

6872
#[cfg(target_os = "freebsd")]
6973
#[test]
7074
pub fn test_uname_freebsd() {
71-
assert_eq!(super::uname().unwrap().sysname(), b"FreeBSD");
75+
assert_eq!(super::uname().unwrap().sysname(), "FreeBSD");
7276
}
7377
}

test/common/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ cfg_if! {
112112
.expect("Bad match_version provided");
113113

114114
let uname = nix::sys::utsname::uname().unwrap();
115-
println!("{}", std::str::from_utf8(uname.sysname()).unwrap());
116-
println!("{}", std::str::from_utf8(uname.nodename()).unwrap());
117-
println!("{}", std::str::from_utf8(uname.release()).unwrap());
118-
println!("{}", std::str::from_utf8(uname.version()).unwrap());
119-
println!("{}", std::str::from_utf8(uname.machine()).unwrap());
115+
println!("{}", uname.sysname().to_str().unwrap());
116+
println!("{}", uname.nodename().to_str().unwrap());
117+
println!("{}", uname.release().to_str().unwrap());
118+
println!("{}", uname.version().to_str().unwrap());
119+
println!("{}", uname.machine().to_str().unwrap());
120120

121121
// Fix stuff that the semver parser can't handle
122-
let fixed_release = &std::str::from_utf8(uname.release()).unwrap().to_string()
122+
let fixed_release = &uname.release().to_str().unwrap().to_string()
123123
// Fedora 33 reports version as 4.18.el8_2.x86_64 or
124124
// 5.18.200-fc33.x86_64. Remove the underscore.
125125
.replace("_", "-")

0 commit comments

Comments
 (0)