Skip to content

Commit

Permalink
Rust 1.81: Stub out cfg_should_impl_error and use `core::error::Err…
Browse files Browse the repository at this point in the history
…or`.

This doesn't remove `cfg_should_impl_error` usage because that will be
a more systematic and more merge-conflict-ful change.
  • Loading branch information
kpreid committed Aug 27, 2024
1 parent f20f94e commit cd7ad84
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 83 deletions.
88 changes: 12 additions & 76 deletions all-is-cubes-base/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Tools that we could imagine being in the Rust standard library, but aren't.
#![allow(clippy::std_instead_of_core)] // TODO: remove this when core::error::Error is stable

use alloc::sync::Arc;
use core::fmt;
use core::marker::PhantomData;
Expand Down Expand Up @@ -79,13 +77,11 @@ impl Executor for () {
}
}

#[cfg(feature = "std")]
#[doc(hidden)]
pub use error_chain::ErrorChain;
#[cfg(feature = "std")]
mod error_chain {
use core::error::Error;
use core::fmt;
use std::error::Error;

/// Formatting wrapper which prints an [`Error`] together with its
/// `source()` chain, with at least one newline between each.
Expand Down Expand Up @@ -121,75 +117,16 @@ mod error_chain {
}
}

cfg_if::cfg_if! {
if #[cfg(feature = "std")] {
/// Alias for [`std::error::Error`] that is a substitute when not on `std`.
/// Used to conditionally disable `Error` trait bounds, and as a path to `Error`
/// to suppress future `std_instead_of_core` lint.
/// TODO: When Rust 1.81 is released and `core::error::Error` exists, we can throw out
/// this mechanism entirely.
#[doc(hidden)]
pub use std::error::Error as ErrorIfStd;

/// Macro that causes conditional compilation on *this* crate's `std` feature,
/// which should be used around `impl std::error::Error`s.
///
/// This macro can be gotten rid of once `core::error::Error` is stable.
#[macro_export]
#[doc(hidden)]
macro_rules! cfg_should_impl_error {
($($body:tt)*) => {
$($body)*
}
}

} else {
use alloc::boxed::Box;
use alloc::string::String;

/// Substitute for [`std::error::Error`] with the same supertraits but no methods.
///
#[doc(hidden)]
pub trait ErrorIfStd: fmt::Debug + fmt::Display {}
impl<T: ?Sized> ErrorIfStd for T where T: fmt::Debug + fmt::Display {}

impl From<&str> for Box<dyn ErrorIfStd + Send + Sync> {
#[allow(clippy::missing_inline_in_public_items)]
fn from(s: &str) -> Self {
Box::new(String::from(s))
}
}
impl From<&str> for Box<dyn ErrorIfStd> {
#[allow(clippy::missing_inline_in_public_items)]
fn from(s: &str) -> Self {
Box::new(String::from(s))
}
}
impl From<String> for Box<dyn ErrorIfStd + Send + Sync> {
#[allow(clippy::missing_inline_in_public_items)]
fn from(s: String) -> Self {
Box::new(s)
}
}
impl From<String> for Box<dyn ErrorIfStd> {
#[allow(clippy::missing_inline_in_public_items)]
fn from(s: String) -> Self {
Box::new(s)
}
}

/// Macro that causes conditional compilation on *this* crate's `std` feature,
/// which should be used around `impl std::error::Error`s.
///
/// This macro can be gotten rid of once `core::error::Error` is stable.
#[macro_export]
#[doc(hidden)]
macro_rules! cfg_should_impl_error {
($($body:tt)*) => {
// ignored
}
}
/// TODO: Remove this no-longer-needed alias
#[doc(hidden)]
pub use core::error::Error as ErrorIfStd;

/// TODO: Remove this no-longer-needed macro
#[macro_export]
#[doc(hidden)]
macro_rules! cfg_should_impl_error {
($($body:tt)*) => {
$($body)*
}
}
pub(crate) use cfg_should_impl_error;
Expand Down Expand Up @@ -369,10 +306,9 @@ mod tests {
fn _assert_executor_trait_is_object_safe(_: &dyn Executor) {}

#[test]
#[cfg(feature = "std")]
fn error_chain() {
use std::error::Error;
use std::fmt;
use core::error::Error;
use core::fmt;

#[derive(Debug)]
struct TestError1;
Expand Down
1 change: 0 additions & 1 deletion all-is-cubes/src/linking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,6 @@ mod tests {
}

#[test]
#[cfg(feature = "std")] // Error::source only exists on std
fn gen_error_message() {
use alloc::string::ToString;

Expand Down
3 changes: 1 addition & 2 deletions all-is-cubes/src/space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,8 +764,7 @@ impl Space {
if let Err(e) = first_pass_txn.execute(self, &mut transaction::no_outputs) {
// This really shouldn't happen, because we already check()ed every part of
// first_pass_txn, but we don't want it to be fatal.
// TODO: this logging should use util::ErrorChain, but that's only available
// with the std feature.
// TODO: this logging should use util::ErrorChain
log::error!("cube tick transaction could not be executed: {e:#?}");
}
first_pass_cubes.len()
Expand Down
1 change: 0 additions & 1 deletion all-is-cubes/src/universe/universe_txn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,6 @@ pub enum MemberMismatch {
Modify(ModifyMemberMismatch),
}

#[cfg(feature = "std")]
impl crate::util::ErrorIfStd for MemberMismatch {
fn source(&self) -> Option<&(dyn crate::util::ErrorIfStd + 'static)> {
match self {
Expand Down
1 change: 0 additions & 1 deletion all-is-cubes/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub use manyfmt::{refmt, Fmt, Refmt};
// Unfortunately, we can't use a glob re-export here or `ErrorIfStd` ends up visible when it
// shouldn't be, mysteriously. So, explicit everything instead, with their various visibilities
// and cfgs.
#[cfg(feature = "std")]
#[doc(hidden)]
pub use all_is_cubes_base::util::ErrorChain;
#[doc(hidden)]
Expand Down
2 changes: 0 additions & 2 deletions all-is-cubes/src/util/maybe_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ impl<G> LockError<G> {
// }
}

#[cfg(feature = "std")]
impl<G> crate::util::ErrorIfStd for LockError<G> {}

impl<G> fmt::Display for LockError<G> {
Expand All @@ -236,7 +235,6 @@ pub(crate) enum TryLockError<G> {
WouldBlock,
}

#[cfg(feature = "std")]
impl<G> crate::util::ErrorIfStd for TryLockError<G> {}

impl<G> fmt::Display for TryLockError<G> {
Expand Down

0 comments on commit cd7ad84

Please sign in to comment.