std: add type alias for raw OS errors

Implement rust-lang/libs-team#173.
This commit is contained in:
joboet
2023-01-31 17:20:37 +01:00
parent dc3e59cb3f
commit 42cc28ac8b
5 changed files with 26 additions and 10 deletions

View File

@@ -88,12 +88,23 @@ impl From<alloc::ffi::NulError> for Error {
// doesn't accidentally get printed.
#[cfg_attr(test, derive(Debug))]
enum ErrorData<C> {
Os(i32),
Os(RawOsError),
Simple(ErrorKind),
SimpleMessage(&'static SimpleMessage),
Custom(C),
}
/// The type of raw OS error codes returned by [`Error::raw_os_error`].
///
/// This is an [`i32`] on all currently supported platforms, but platforms
/// added in the future (such as UEFI) may use a different primitive type like
/// [`usize`]. Use `as`or [`into`] conversions where applicable to ensure maximum
/// portability.
///
/// [`into`]: Into::into
#[unstable(feature = "raw_os_error_ty", issue = "none")]
pub type RawOsError = i32;
// `#[repr(align(4))]` is probably redundant, it should have that value or
// higher already. We include it just because repr_bitpacked.rs's encoding
// requires an alignment >= 4 (note that `#[repr(align)]` will not reduce the
@@ -579,7 +590,7 @@ impl Error {
#[must_use]
#[inline]
pub fn last_os_error() -> Error {
Error::from_raw_os_error(sys::os::errno() as i32)
Error::from_raw_os_error(sys::os::errno())
}
/// Creates a new instance of an [`Error`] from a particular OS error code.
@@ -610,7 +621,7 @@ impl Error {
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use]
#[inline]
pub fn from_raw_os_error(code: i32) -> Error {
pub fn from_raw_os_error(code: RawOsError) -> Error {
Error { repr: Repr::new_os(code) }
}
@@ -646,7 +657,7 @@ impl Error {
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use]
#[inline]
pub fn raw_os_error(&self) -> Option<i32> {
pub fn raw_os_error(&self) -> Option<RawOsError> {
match self.repr.data() {
ErrorData::Os(i) => Some(i),
ErrorData::Custom(..) => None,