Skip to content

Commit f18aaa4

Browse files
committed
mark error paths as #[cold]
1 parent 73b6b7a commit f18aaa4

File tree

6 files changed

+50
-18
lines changed

6 files changed

+50
-18
lines changed

src/desktop.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
use std::ffi::CStr;
22

3+
#[inline]
4+
#[cold]
5+
const fn unknown() -> &'static str { "Unknown" }
6+
37
#[must_use]
48
#[cfg_attr(feature = "hotpath", hotpath::measure)]
59
pub fn get_desktop_info() -> String {
610
// Retrieve the environment variables and handle Result types
711
let desktop_str = unsafe {
812
let ptr = libc::getenv(c"XDG_CURRENT_DESKTOP".as_ptr());
913
if ptr.is_null() {
10-
"Unknown"
14+
unknown()
1115
} else {
12-
let s = CStr::from_ptr(ptr).to_str().unwrap_or("Unknown");
16+
let s = CStr::from_ptr(ptr).to_str().unwrap_or_else(|_| unknown());
1317
s.strip_prefix("none+").unwrap_or(s)
1418
}
1519
};
1620

1721
let backend_str = unsafe {
1822
let ptr = libc::getenv(c"XDG_SESSION_TYPE".as_ptr());
1923
if ptr.is_null() {
20-
"Unknown"
24+
unknown()
2125
} else {
22-
let s = CStr::from_ptr(ptr).to_str().unwrap_or("Unknown");
23-
if s.is_empty() { "Unknown" } else { s }
26+
let s = CStr::from_ptr(ptr).to_str().unwrap_or_else(|_| unknown());
27+
if s.is_empty() { unknown() } else { s }
2428
}
2529
};
2630

src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ pub mod syscall;
66
pub mod system;
77
pub mod uptime;
88

9-
use std::mem::MaybeUninit;
9+
use std::{io, mem::MaybeUninit};
10+
11+
#[inline]
12+
#[cold]
13+
pub fn last_os_error<T>() -> io::Result<T> {
14+
Err(io::Error::last_os_error())
15+
}
1016

1117
/// Wrapper for `libc::utsname` with safe accessor methods
1218
pub struct UtsName(libc::utsname);
@@ -20,7 +26,7 @@ impl UtsName {
2026
pub fn uname() -> Result<Self, std::io::Error> {
2127
let mut uts = MaybeUninit::uninit();
2228
if unsafe { libc::uname(uts.as_mut_ptr()) } != 0 {
23-
return Err(std::io::Error::last_os_error());
29+
return last_os_error();
2430
}
2531
Ok(Self(unsafe { uts.assume_init() }))
2632
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod uptime;
88

99
use std::io::{self, Cursor, Write};
1010

11-
pub use microfetch_lib::UtsName;
11+
pub use microfetch_lib::{UtsName, last_os_error};
1212

1313
use crate::{
1414
desktop::get_desktop_info,

src/syscall.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
1111
use std::io;
1212

13+
use crate::last_os_error;
14+
1315
/// Direct syscall to open a file
1416
///
1517
/// # Returns
@@ -169,6 +171,12 @@ pub unsafe fn sys_close(fd: i32) -> i32 {
169171
}
170172
}
171173

174+
#[inline]
175+
#[cold]
176+
fn path_too_long() -> io::Result<usize> {
177+
Err(io::Error::new(io::ErrorKind::InvalidInput, "Path too long"))
178+
}
179+
172180
/// Read entire file using direct syscalls. This avoids libc overhead and can be
173181
/// significantly faster for small files.
174182
///
@@ -182,7 +190,7 @@ pub fn read_file_fast(path: &str, buffer: &mut [u8]) -> io::Result<usize> {
182190
// Use stack-allocated buffer for null-terminated path (max 256 bytes)
183191
let path_bytes = path.as_bytes();
184192
if path_bytes.len() >= 256 {
185-
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Path too long"));
193+
return path_too_long();
186194
}
187195

188196
let mut path_buf = [0u8; 256];
@@ -192,14 +200,14 @@ pub fn read_file_fast(path: &str, buffer: &mut [u8]) -> io::Result<usize> {
192200
unsafe {
193201
let fd = sys_open(path_buf.as_ptr(), O_RDONLY);
194202
if fd < 0 {
195-
return Err(io::Error::last_os_error());
203+
return last_os_error();
196204
}
197205

198206
let bytes_read = sys_read(fd, buffer.as_mut_ptr(), buffer.len());
199207
let _ = sys_close(fd);
200208

201209
if bytes_read < 0 {
202-
return Err(io::Error::last_os_error());
210+
return last_os_error();
203211
}
204212

205213
#[allow(clippy::cast_sign_loss)]

src/system.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
use std::{ffi::CStr, fmt::Write as _, io, mem::MaybeUninit};
22

3-
use crate::{UtsName, colors::COLORS, syscall::read_file_fast};
3+
use crate::{colors::COLORS, last_os_error, syscall::read_file_fast, UtsName};
4+
5+
#[inline]
6+
#[cold]
7+
const fn unknown_user() -> &'static str { "unknown_user" }
8+
9+
#[inline]
10+
#[cold]
11+
const fn unknown_host() -> &'static str { "unknown_host" }
412

513
#[must_use]
614
#[cfg_attr(feature = "hotpath", hotpath::measure)]
715
pub fn get_username_and_hostname(utsname: &UtsName) -> String {
816
let username = unsafe {
917
let ptr = libc::getenv(c"USER".as_ptr());
1018
if ptr.is_null() {
11-
"unknown_user"
19+
unknown_user()
1220
} else {
13-
CStr::from_ptr(ptr).to_str().unwrap_or("unknown_user")
21+
CStr::from_ptr(ptr).to_str().unwrap_or_else(|_| unknown_user())
1422
}
1523
};
16-
let hostname = utsname.nodename().to_str().unwrap_or("unknown_host");
24+
let hostname = utsname.nodename().to_str().unwrap_or_else(|_| unknown_host());
1725

1826
let capacity = COLORS.yellow.len()
1927
+ username.len()
@@ -35,13 +43,17 @@ pub fn get_username_and_hostname(utsname: &UtsName) -> String {
3543
result
3644
}
3745

46+
#[inline]
47+
#[cold]
48+
const fn unknown_shell() -> &'static str { "unknown_shell" }
49+
3850
#[must_use]
3951
#[cfg_attr(feature = "hotpath", hotpath::measure)]
4052
pub fn get_shell() -> String {
4153
unsafe {
4254
let ptr = libc::getenv(c"SHELL".as_ptr());
4355
if ptr.is_null() {
44-
return "unknown_shell".into();
56+
return unknown_shell().into();
4557
}
4658

4759
let bytes = CStr::from_ptr(ptr).to_bytes();
@@ -63,7 +75,7 @@ pub fn get_root_disk_usage() -> Result<String, io::Error> {
6375
let path = b"/\0";
6476

6577
if unsafe { libc::statvfs(path.as_ptr().cast(), vfs.as_mut_ptr()) } != 0 {
66-
return Err(io::Error::last_os_error());
78+
return last_os_error();
6779
}
6880

6981
let vfs = unsafe { vfs.assume_init() };

src/uptime.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::{io, mem::MaybeUninit};
22

3+
use crate::last_os_error;
4+
35
/// Faster integer to string conversion without the formatting overhead.
46
#[inline]
57
fn itoa(mut n: u64, buf: &mut [u8]) -> &str {
@@ -73,7 +75,7 @@ pub fn get_current() -> Result<String, io::Error> {
7375
let uptime_seconds = {
7476
let mut info = MaybeUninit::uninit();
7577
if unsafe { sys_sysinfo(info.as_mut_ptr()) } != 0 {
76-
return Err(io::Error::last_os_error());
78+
return last_os_error();
7779
}
7880
#[allow(clippy::cast_sign_loss)]
7981
unsafe {

0 commit comments

Comments
 (0)