Skip to content

Commit beb5cc9

Browse files
Rollup merge of rust-lang#107519 - joboet:raw_os_error_ty, r=Amanieu
Add type alias for raw OS errors Implement rust-lang/libs-team#173. `@rustbot` label +S-waiting-on-ACP +T-libs-api
2 parents e7e8b91 + 42cc28a commit beb5cc9

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed

library/std/src/io/error.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,23 @@ impl From<alloc::ffi::NulError> for Error {
8888
// doesn't accidentally get printed.
8989
#[cfg_attr(test, derive(Debug))]
9090
enum ErrorData<C> {
91-
Os(i32),
91+
Os(RawOsError),
9292
Simple(ErrorKind),
9393
SimpleMessage(&'static SimpleMessage),
9494
Custom(C),
9595
}
9696

97+
/// The type of raw OS error codes returned by [`Error::raw_os_error`].
98+
///
99+
/// This is an [`i32`] on all currently supported platforms, but platforms
100+
/// added in the future (such as UEFI) may use a different primitive type like
101+
/// [`usize`]. Use `as`or [`into`] conversions where applicable to ensure maximum
102+
/// portability.
103+
///
104+
/// [`into`]: Into::into
105+
#[unstable(feature = "raw_os_error_ty", issue = "none")]
106+
pub type RawOsError = i32;
107+
97108
// `#[repr(align(4))]` is probably redundant, it should have that value or
98109
// higher already. We include it just because repr_bitpacked.rs's encoding
99110
// requires an alignment >= 4 (note that `#[repr(align)]` will not reduce the
@@ -579,7 +590,7 @@ impl Error {
579590
#[must_use]
580591
#[inline]
581592
pub fn last_os_error() -> Error {
582-
Error::from_raw_os_error(sys::os::errno() as i32)
593+
Error::from_raw_os_error(sys::os::errno())
583594
}
584595

585596
/// Creates a new instance of an [`Error`] from a particular OS error code.
@@ -610,7 +621,7 @@ impl Error {
610621
#[stable(feature = "rust1", since = "1.0.0")]
611622
#[must_use]
612623
#[inline]
613-
pub fn from_raw_os_error(code: i32) -> Error {
624+
pub fn from_raw_os_error(code: RawOsError) -> Error {
614625
Error { repr: Repr::new_os(code) }
615626
}
616627

@@ -646,7 +657,7 @@ impl Error {
646657
#[stable(feature = "rust1", since = "1.0.0")]
647658
#[must_use]
648659
#[inline]
649-
pub fn raw_os_error(&self) -> Option<i32> {
660+
pub fn raw_os_error(&self) -> Option<RawOsError> {
650661
match self.repr.data() {
651662
ErrorData::Os(i) => Some(i),
652663
ErrorData::Custom(..) => None,

library/std/src/io/error/repr_bitpacked.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
//! to use a pointer type to store something that may hold an integer, some of
103103
//! the time.
104104
105-
use super::{Custom, ErrorData, ErrorKind, SimpleMessage};
105+
use super::{Custom, ErrorData, ErrorKind, RawOsError, SimpleMessage};
106106
use alloc::boxed::Box;
107107
use core::marker::PhantomData;
108108
use core::mem::{align_of, size_of};
@@ -172,7 +172,7 @@ impl Repr {
172172
}
173173

174174
#[inline]
175-
pub(super) fn new_os(code: i32) -> Self {
175+
pub(super) fn new_os(code: RawOsError) -> Self {
176176
let utagged = ((code as usize) << 32) | TAG_OS;
177177
// Safety: `TAG_OS` is not zero, so the result of the `|` is not 0.
178178
let res = Self(unsafe { NonNull::new_unchecked(ptr::invalid_mut(utagged)) }, PhantomData);
@@ -250,7 +250,7 @@ where
250250
let bits = ptr.as_ptr().addr();
251251
match bits & TAG_MASK {
252252
TAG_OS => {
253-
let code = ((bits as i64) >> 32) as i32;
253+
let code = ((bits as i64) >> 32) as RawOsError;
254254
ErrorData::Os(code)
255255
}
256256
TAG_SIMPLE => {
@@ -374,6 +374,9 @@ static_assert!((TAG_MASK + 1).is_power_of_two());
374374
static_assert!(align_of::<SimpleMessage>() >= TAG_MASK + 1);
375375
static_assert!(align_of::<Custom>() >= TAG_MASK + 1);
376376

377+
// `RawOsError` must be an alias for `i32`.
378+
const _: fn(RawOsError) -> i32 = |os| os;
379+
377380
static_assert!(@usize_eq: TAG_MASK & TAG_SIMPLE_MESSAGE, TAG_SIMPLE_MESSAGE);
378381
static_assert!(@usize_eq: TAG_MASK & TAG_CUSTOM, TAG_CUSTOM);
379382
static_assert!(@usize_eq: TAG_MASK & TAG_OS, TAG_OS);

library/std/src/io/error/repr_unpacked.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! non-64bit targets, where the packed 64 bit representation wouldn't work, and
33
//! would have no benefit.
44
5-
use super::{Custom, ErrorData, ErrorKind, SimpleMessage};
5+
use super::{Custom, ErrorData, ErrorKind, RawOsError, SimpleMessage};
66
use alloc::boxed::Box;
77

88
type Inner = ErrorData<Box<Custom>>;
@@ -18,7 +18,7 @@ impl Repr {
1818
Self(Inner::Custom(b))
1919
}
2020
#[inline]
21-
pub(super) fn new_os(code: i32) -> Self {
21+
pub(super) fn new_os(code: RawOsError) -> Self {
2222
Self(Inner::Os(code))
2323
}
2424
#[inline]

library/std/src/io/error/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn test_const() {
7171

7272
#[test]
7373
fn test_os_packing() {
74-
for code in -20i32..20i32 {
74+
for code in -20..20 {
7575
let e = Error::from_raw_os_error(code);
7676
assert_eq!(e.raw_os_error(), Some(code));
7777
assert_matches!(

library/std/src/io/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ use crate::sys_common::memchr;
262262

263263
#[stable(feature = "bufwriter_into_parts", since = "1.56.0")]
264264
pub use self::buffered::WriterPanicked;
265+
#[unstable(feature = "raw_os_error_ty", issue = "none")]
266+
pub use self::error::RawOsError;
265267
pub(crate) use self::stdio::attempt_print_to_stderr;
266268
#[unstable(feature = "internal_output_capture", issue = "none")]
267269
#[doc(no_inline, hidden)]

0 commit comments

Comments
 (0)