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 5617564
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 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
11 changes: 3 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,6 @@ 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(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
54 changes: 54 additions & 0 deletions tests/no_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,57 @@ enum EnumWithUnit {
SmallInt(i32),
Unit,
}

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 5617564

Please sign in to comment.