Skip to content

Commit

Permalink
util: Support impl Error for Arc<T: Error> in no-std at Rust 1.81+
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jun 11, 2024
1 parent 985f071 commit 30b9f90
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 2 additions & 0 deletions portable-atomic-util/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com

## [Unreleased]

- Support `impl Error for Arc<T: Error>` in no-std at Rust 1.81+.

## [0.2.0] - 2024-05-07

- Rewrite `Arc` based on `std::sync::Arc`'s implementation. ([#142](https://github.com/taiki-e/portable-atomic/pull/142))
Expand Down
6 changes: 5 additions & 1 deletion portable-atomic-util/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn main() {
// Custom cfgs set by build script. Not public API.
// grep -E 'cargo:rustc-cfg=' build.rs | grep -v '=//' | sed -E 's/^.*cargo:rustc-cfg=//; s/(=\\)?".*$//' | LC_ALL=C sort -u | tr '\n' ','
println!(
"cargo:rustc-check-cfg=cfg(portable_atomic_no_alloc,portable_atomic_no_alloc_layout_extras,portable_atomic_no_core_unwind_safe,portable_atomic_no_futures_api,portable_atomic_no_io_safety,portable_atomic_no_io_vec,portable_atomic_no_min_const_generics,portable_atomic_no_unsafe_op_in_unsafe_fn,portable_atomic_sanitize_thread)"
"cargo:rustc-check-cfg=cfg(portable_atomic_no_alloc,portable_atomic_no_alloc_layout_extras,portable_atomic_no_core_unwind_safe,portable_atomic_no_error_in_core,portable_atomic_no_futures_api,portable_atomic_no_io_safety,portable_atomic_no_io_vec,portable_atomic_no_min_const_generics,portable_atomic_no_unsafe_op_in_unsafe_fn,portable_atomic_sanitize_thread)"
);
}

Expand Down Expand Up @@ -72,6 +72,10 @@ fn main() {
if !version.probe(63, 2022, 6, 15) {
println!("cargo:rustc-cfg=portable_atomic_no_io_safety");
}
// error_in_core stabilized in Rust 1.81 (nightly-2024-06-09): https://github.com/rust-lang/rust/pull/125951
if !version.probe(81, 2024, 6, 8) {
println!("cargo:rustc-cfg=portable_atomic_no_error_in_core");
}

if version.nightly {
// `cfg(sanitize = "..")` is not stabilized.
Expand Down
18 changes: 11 additions & 7 deletions portable-atomic-util/src/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2405,18 +2405,22 @@ fn data_offset_align(align: usize) -> usize {
layout.size() + padding_needed_for(layout, align)
}

#[cfg(feature = "std")]
impl<T: ?Sized + std::error::Error> std::error::Error for Arc<T> {
#[cfg(not(portable_atomic_no_error_in_core))]
use core::error;
#[cfg(all(portable_atomic_no_error_in_core, feature = "std"))]
use std::error;
#[cfg(any(not(portable_atomic_no_error_in_core), feature = "std"))]
impl<T: ?Sized + error::Error> error::Error for Arc<T> {
#[allow(deprecated)]
fn description(&self) -> &str {
std::error::Error::description(&**self)
error::Error::description(&**self)
}
#[allow(deprecated)]
fn cause(&self) -> Option<&dyn std::error::Error> {
std::error::Error::cause(&**self)
fn cause(&self) -> Option<&dyn error::Error> {
error::Error::cause(&**self)
}
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
std::error::Error::source(&**self)
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
error::Error::source(&**self)
}
}

Expand Down

0 comments on commit 30b9f90

Please sign in to comment.