From 87fa2d561a035e963afa681a9360f475354efac6 Mon Sep 17 00:00:00 2001 From: Jelte Fennema-Nio Date: Thu, 4 Jul 2024 10:16:40 +0200 Subject: [PATCH] Update documentation about Error in no_std --- CHANGELOG.md | 3 +-- impl/Cargo.toml | 2 +- impl/doc/error.md | 14 ++++++------ src/lib.rs | 1 - tests/no_std.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e10b5014..d4b0f832 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -85,8 +85,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). should prevent code style linters from attempting to modify the generated code. - Upgrade to `syn` 2.0. -- The `Error` derive now works in nightly `no_std` environments when enabling - `#![feature(error_in_core)]`. +- The `Error` derive now works in nightly `no_std` environments ### Fixed diff --git a/impl/Cargo.toml b/impl/Cargo.toml index 8252fdfb..cdce885d 100644 --- a/impl/Cargo.toml +++ b/impl/Cargo.toml @@ -44,7 +44,7 @@ features = ["full"] rustdoc-args = ["--cfg", "docsrs"] [lints.rust] -unexpected_cfgs = { level = "warn", check-cfg = ["cfg(ci)", "cfg(nighthly)"] } +unexpected_cfgs = { level = "warn", check-cfg = ["cfg(ci)", "cfg(nightly)"] } [features] default = [] diff --git a/impl/doc/error.md b/impl/doc/error.md index cc755cb2..2aace519 100644 --- a/impl/doc/error.md +++ b/impl/doc/error.md @@ -44,11 +44,9 @@ ignored for one of these methods by using `#[error(not(backtrace))]` or ### What works in `no_std`? -If you want to use the `Error` derive on `no_std` environments, then you need to -compile with nightly and enable this feature: -```ignore -#![feature(error_in_core)] -``` +If you want to use the `Error` derive on `no_std` environments, then +you need to compile with nightly, or wait until Rust 1.81 when `Error` +in `core` is expected to be stabilized. Backtraces don't work though, because the `Backtrace` type is only available in `std`. @@ -59,9 +57,9 @@ Backtraces don't work though, because the `Backtrace` type is only available in ## Example usage ```rust -# #![cfg_attr(nightly, feature(error_generic_member_access, error_in_core))] -// Nightly requires enabling these features: -// #![feature(error_generic_member_access, error_in_core)] +# #![cfg_attr(nightly, feature(error_generic_member_access))] +// Nightly requires enabling this feature: +// #![feature(error_generic_member_access)] # #[cfg(not(nightly))] fn main() {} # #[cfg(nightly)] fn main() { # use core::error::{request_ref, request_value, Error as __}; diff --git a/src/lib.rs b/src/lib.rs index 879de304..68c3aa03 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,7 +42,6 @@ doc = include_str!("../README.md") )] #![cfg_attr(not(feature = "std"), no_std)] -#![cfg_attr(all(not(feature = "std"), feature = "error"), feature(error_in_core))] #![cfg_attr(docsrs, feature(doc_auto_cfg))] #![cfg_attr(any(not(docsrs), ci), deny(rustdoc::all))] #![forbid(non_ascii_idents, unsafe_code)] diff --git a/tests/no_std.rs b/tests/no_std.rs index 7d2adf14..566fe13a 100644 --- a/tests/no_std.rs +++ b/tests/no_std.rs @@ -74,3 +74,58 @@ enum EnumWithUnit { SmallInt(i32), Unit, } + +#[rustversion::nightly] +mod error { + use derive_more::{Display, Error, From}; + #[derive(Default, Debug, Display, Error)] + struct Simple; + + #[derive(Default, Debug, Display, Error)] + struct WithSource { + source: Simple, + } + #[derive(Default, Debug, Display, Error)] + struct WithExplicitSource { + #[error(source)] + explicit_source: Simple, + } + + #[derive(Default, Debug, Display, Error)] + struct Tuple(Simple); + + #[derive(Default, Debug, Display, Error)] + struct WithoutSource(#[error(not(source))] i32); + #[derive(Debug, Display, Error, From)] + enum CompoundError { + Simple, + WithSource { + source: Simple, + }, + WithExplicitSource { + #[error(source)] + explicit_source: WithSource, + }, + Tuple(WithExplicitSource), + WithoutSource(#[error(not(source))] Tuple), + } + + #[test] + fn assert() { + assert!(Simple.source().is_none()); + assert!(WithSource::default().source().is_some()); + assert!(WithExplicitSource::default().source().is_some()); + assert!(Tuple::default().source().is_some()); + assert!(Tuple::default().source().is_some()); + assert!(WithoutSource::default().source().is_none()); + assert!(CompoundError::Simple.source().is_none()); + assert!(CompoundError::from(Simple).source().is_some()); + assert!(CompoundError::from(WithSource::default()) + .source() + .is_some()); + assert!(CompoundError::from(WithExplicitSource::default()) + .source() + .is_some()); + assert!(CompoundError::from(Tuple::default()).source().is_none()); + } +}