From 28452875467d00fda1c8c7c28b244a0c39d5f717 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Mon, 16 Dec 2024 16:38:17 +0100 Subject: [PATCH] io: always `impl Error for Errno` in rustc 1.81+ In rust 1.81 the feature [`error_in_core`] was stabilized. This makes the `Error` trait available in `core::error` instead of `std::error`, so it can be used in a no-std context, too. [`error_in_core`]: --- Cargo.toml | 1 + build.rs | 1 + src/io/errno.rs | 4 +++- src/lib.rs | 1 + 4 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index ea3f6e72a..de2ada6c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -262,6 +262,7 @@ check-cfg = [ 'cfg(core_intrinsics)', 'cfg(criterion)', 'cfg(document_experimental_runtime_api)', + 'cfg(error_in_core)', 'cfg(fix_y2038)', 'cfg(freebsdlike)', 'cfg(libc)', diff --git a/build.rs b/build.rs index 5b92f070b..68831c351 100644 --- a/build.rs +++ b/build.rs @@ -71,6 +71,7 @@ fn main() { use_feature_or_nothing("core_ffi_c"); use_feature_or_nothing("alloc_c_string"); use_feature_or_nothing("alloc_ffi"); + use_feature_or_nothing("error_in_core"); } // Feature needed for testing. diff --git a/src/io/errno.rs b/src/io/errno.rs index 2cfaef70d..7ff0a6abf 100644 --- a/src/io/errno.rs +++ b/src/io/errno.rs @@ -8,6 +8,8 @@ use crate::backend; use core::{fmt, result}; #[cfg(feature = "std")] use std::error; +#[cfg(all(not(feature = "std"), error_in_core))] +use core::error; /// A specialized [`Result`] type for `rustix` APIs. pub type Result = result::Result; @@ -49,7 +51,7 @@ impl fmt::Debug for Errno { } } -#[cfg(feature = "std")] +#[cfg(any(feature = "std", error_in_core))] impl error::Error for Errno {} #[cfg(feature = "std")] diff --git a/src/lib.rs b/src/lib.rs index aee4005a5..a6b7dcab2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -108,6 +108,7 @@ #![cfg_attr(all(wasi_ext, target_os = "wasi", feature = "std"), feature(wasi_ext))] #![cfg_attr(core_ffi_c, feature(core_ffi_c))] #![cfg_attr(core_c_str, feature(core_c_str))] +#![cfg_attr(error_in_core, feature(error_in_core))] #![cfg_attr(all(feature = "alloc", alloc_c_string), feature(alloc_c_string))] #![cfg_attr(all(feature = "alloc", alloc_ffi), feature(alloc_ffi))] #![cfg_attr(not(feature = "std"), no_std)]