Skip to content

Commit 821e0fe

Browse files
authored
Rollup merge of #139081 - joboet:errno_dedup, r=Noratrieb
std: deduplicate `errno` accesses By marking `__errno_location` as `#[ffi_const]` and `std::sys::os::errno` as `#[inline]`, this PR allows merging multiple calls to `io::Error::last_os_error()` into one.
2 parents fb6d10e + dd4f616 commit 821e0fe

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@
297297
#![feature(extended_varargs_abi_support)]
298298
#![feature(f128)]
299299
#![feature(f16)]
300+
#![feature(ffi_const)]
300301
#![feature(formatting_options)]
301302
#![feature(if_let_guard)]
302303
#![feature(intra_doc_pointers)]

library/std/src/sys/pal/unix/os.rs

+8
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,14 @@ unsafe extern "C" {
5959
#[cfg_attr(any(target_os = "freebsd", target_vendor = "apple"), link_name = "__error")]
6060
#[cfg_attr(target_os = "haiku", link_name = "_errnop")]
6161
#[cfg_attr(target_os = "aix", link_name = "_Errno")]
62+
// SAFETY: this will always return the same pointer on a given thread.
63+
#[unsafe(ffi_const)]
6264
fn errno_location() -> *mut c_int;
6365
}
6466

6567
/// Returns the platform-specific value of errno
6668
#[cfg(not(any(target_os = "dragonfly", target_os = "vxworks", target_os = "rtems")))]
69+
#[inline]
6770
pub fn errno() -> i32 {
6871
unsafe { (*errno_location()) as i32 }
6972
}
@@ -72,16 +75,19 @@ pub fn errno() -> i32 {
7275
// needed for readdir and syscall!
7376
#[cfg(all(not(target_os = "dragonfly"), not(target_os = "vxworks"), not(target_os = "rtems")))]
7477
#[allow(dead_code)] // but not all target cfgs actually end up using it
78+
#[inline]
7579
pub fn set_errno(e: i32) {
7680
unsafe { *errno_location() = e as c_int }
7781
}
7882

7983
#[cfg(target_os = "vxworks")]
84+
#[inline]
8085
pub fn errno() -> i32 {
8186
unsafe { libc::errnoGet() }
8287
}
8388

8489
#[cfg(target_os = "rtems")]
90+
#[inline]
8591
pub fn errno() -> i32 {
8692
unsafe extern "C" {
8793
#[thread_local]
@@ -92,6 +98,7 @@ pub fn errno() -> i32 {
9298
}
9399

94100
#[cfg(target_os = "dragonfly")]
101+
#[inline]
95102
pub fn errno() -> i32 {
96103
unsafe extern "C" {
97104
#[thread_local]
@@ -103,6 +110,7 @@ pub fn errno() -> i32 {
103110

104111
#[cfg(target_os = "dragonfly")]
105112
#[allow(dead_code)]
113+
#[inline]
106114
pub fn set_errno(e: i32) {
107115
unsafe extern "C" {
108116
#[thread_local]

0 commit comments

Comments
 (0)