Skip to content

Commit

Permalink
Update documentation about Error in no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
JelteF committed Jul 4, 2024
1 parent 55b9bb1 commit 87fa2d5
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
14 changes: 6 additions & 8 deletions impl/doc/error.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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 __};
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
55 changes: 55 additions & 0 deletions tests/no_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 87fa2d5

Please sign in to comment.