Skip to content

Commit 211165a

Browse files
authored
Use core::error::Error in codebase (#466)
## Synopsis We had [TODOs across the code](https://github.com/JelteF/derive_more/blob/v2.0.1/impl/src/error.rs#L43-L44): > ```rust > // TODO: Use `derive_more::core::error::Error` once `error_in_core` Rust feature is > // stabilized. > ``` The `feature(error_in_core)` was [stabilized in 1.81 Rust](rust-lang/rust#125951). It's long enough, so we can bump up our MSRV. ## Solution - Use `core::error::Error` in codebase and remove `feature = "std"` gating for it. - Bump up MSRV to 1.81. ## Checklist - [x] Documentation is updated - [x] Tests are added/updated - [x] [CHANGELOG entry](/CHANGELOG.md) is added
1 parent 96b92a7 commit 211165a

File tree

19 files changed

+56
-79
lines changed

19 files changed

+56
-79
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,26 @@ jobs:
5454
strategy:
5555
fail-fast: false
5656
matrix:
57-
msrv: ["1.75.0"]
5857
os:
5958
- ubuntu
6059
- macOS
6160
- windows
6261
runs-on: ${{ matrix.os }}-latest
6362
steps:
6463
- uses: actions/checkout@v4
64+
65+
- uses: SebRollen/[email protected]
66+
id: msrv
67+
with:
68+
file: Cargo.toml
69+
field: package.rust-version
70+
6571
- uses: dtolnay/rust-toolchain@v1
6672
with:
6773
toolchain: nightly
6874
- uses: dtolnay/rust-toolchain@v1
6975
with:
70-
toolchain: ${{ matrix.msrv }}
76+
toolchain: ${{ steps.msrv.outputs.value }}
7177

7278
- name: Install minimal dependencies versions
7379
run: cargo +nightly update -Z minimal-versions

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1515
- Support `Option` fields for `Error::source()` in `Error` derive.
1616
([#459](https://github.com/JelteF/derive_more/pull/459))
1717

18+
### Changed
19+
20+
- The minimum supported Rust version (MSRV) is now Rust 1.81.
21+
([#466](https://github.com/JelteF/derive_more/pull/466))
22+
1823
### Fixed
1924

2025
- Suppress deprecation warnings in generated code.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "derive_more"
33
version = "2.0.1"
44
edition = "2021"
5-
rust-version = "1.75.0"
5+
rust-version = "1.81.0"
66
description = "Adds #[derive(x)] macros for more traits"
77
authors = ["Jelte Fennema <[email protected]>"]
88
license = "MIT"

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Latest Version](https://img.shields.io/crates/v/derive_more.svg)](https://crates.io/crates/derive_more)
55
[![Rust Documentation](https://docs.rs/derive_more/badge.svg)](https://docs.rs/derive_more)
66
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/JelteF/derive_more/master/LICENSE)
7-
[![Rust 1.75+](https://img.shields.io/badge/rustc-1.75+-lightgray.svg)](https://blog.rust-lang.org/2023/12/28/Rust-1.75.0.html)
7+
[![Rust 1.81+](https://img.shields.io/badge/rustc-1.81+-lightgray.svg)](https://blog.rust-lang.org/2024/09/05/Rust-1.81.0)
88
[![Unsafe Forbidden](https://img.shields.io/badge/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance)
99

1010
Rust has lots of builtin traits that are implemented for its basic types, such
@@ -216,7 +216,7 @@ extern crate derive_more;
216216

217217
## [MSRV] policy
218218

219-
This library requires Rust 1.75 or higher.
219+
This library requires Rust 1.81 or higher.
220220

221221
Changing [MSRV] (minimum supported Rust version) of this crate is treated as a **minor version change** in terms of [Semantic Versioning].
222222
- So, if [MSRV] changes are **NOT concerning** for your project, just use the default [caret requirement]:

impl/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "derive_more-impl"
33
version = "2.0.1"
44
edition = "2021"
5-
rust-version = "1.75.0"
5+
rust-version = "1.81.0"
66
description = "Internal implementation of `derive_more` crate"
77
authors = ["Jelte Fennema <[email protected]>"]
88
license = "MIT"

impl/doc/error.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Deriving `Error` will generate an `Error` implementation, that contains
44
(depending on the type) a `source()` and a `provide()` method. Please note,
5-
at the time of writing `provide()` is only supported on nightly rust. So you
5+
at the time of writing `provide()` is only supported on nightly Rust. So you
66
have to use that to make use of it.
77

88
For a struct, these methods always do the same. For an `enum` they have separate
@@ -44,12 +44,8 @@ ignored for one of these methods by using `#[error(not(backtrace))]` or
4444

4545
### What works in `no_std`?
4646

47-
If you want to use the `Error` derive on `no_std` environments, then
48-
you need to compile with nightly, or wait until Rust 1.81 when `Error`
49-
in `core` is expected to be stabilized.
50-
51-
Backtraces don't work though, because the `Backtrace` type is only available in
52-
`std`.
47+
`Error` derive fully works on `no_std` environments, except the `provide()`
48+
method usage, because the `Backtrace` type is only available in `std`.
5349

5450

5551
### `Option`al fields

impl/src/error.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ pub fn expand(
4040
// Not using `#[inline]` here on purpose, since this is almost never part
4141
// of a hot codepath.
4242
quote! {
43-
// TODO: Use `derive_more::core::error::Error` once `error_in_core` Rust feature is
44-
// stabilized.
45-
fn source(&self) -> Option<&(dyn derive_more::with_trait::Error + 'static)> {
43+
fn source(&self) -> Option<&(dyn derive_more::core::error::Error + 'static)> {
4644
use derive_more::__private::AsDynError as _;
4745
#source
4846
}
@@ -84,9 +82,7 @@ pub fn expand(
8482
where #(
8583
#bounds: derive_more::core::fmt::Debug
8684
+ derive_more::core::fmt::Display
87-
// TODO: Use `derive_more::core::error::Error` once `error_in_core`
88-
// Rust feature is stabilized.
89-
+ derive_more::with_trait::Error
85+
+ derive_more::core::error::Error
9086
+ 'static
9187
),*
9288
},
@@ -97,9 +93,7 @@ pub fn expand(
9793

9894
let render = quote! {
9995
#[automatically_derived]
100-
// TODO: Use `derive_more::core::error::Error` once `error_in_core` Rust feature is
101-
// stabilized.
102-
impl #impl_generics derive_more::with_trait::Error for #ident #ty_generics #where_clause {
96+
impl #impl_generics derive_more::core::error::Error for #ident #ty_generics #where_clause {
10397
#source
10498
#provide
10599
}
@@ -229,9 +223,7 @@ impl ParsedFields<'_, '_> {
229223
let source_provider = self.source.map(|source| {
230224
let source_expr = &self.data.members[source];
231225
quote! {
232-
// TODO: Use `derive_more::core::error::Error` once `error_in_core` Rust feature is
233-
// stabilized.
234-
derive_more::with_trait::Error::provide(&#source_expr, request);
226+
derive_more::core::error::Error::provide(&#source_expr, request);
235227
}
236228
});
237229
let backtrace_provider = self
@@ -261,9 +253,7 @@ impl ParsedFields<'_, '_> {
261253
let pattern = self.data.matcher(&[source], &[quote! { source }]);
262254
Some(quote! {
263255
#pattern => {
264-
// TODO: Use `derive_more::core::error::Error` once `error_in_core` Rust
265-
// feature is stabilized.
266-
derive_more::with_trait::Error::provide(source, request);
256+
derive_more::core::error::Error::provide(source, request);
267257
}
268258
})
269259
}
@@ -275,9 +265,7 @@ impl ParsedFields<'_, '_> {
275265
Some(quote! {
276266
#pattern => {
277267
request.provide_ref::<::std::backtrace::Backtrace>(backtrace);
278-
// TODO: Use `derive_more::core::error::Error` once `error_in_core` Rust
279-
// feature is stabilized.
280-
derive_more::with_trait::Error::provide(source, request);
268+
derive_more::core::error::Error::provide(source, request);
281269
}
282270
})
283271
}

src/add.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Definitions used in derived implementations of [`core::ops::Add`]-like traits.
22
3-
use core::fmt;
3+
use core::{error::Error, fmt};
44

55
use crate::UnitError;
66

@@ -30,8 +30,7 @@ impl fmt::Display for WrongVariantError {
3030
}
3131
}
3232

33-
#[cfg(feature = "std")]
34-
impl std::error::Error for WrongVariantError {}
33+
impl Error for WrongVariantError {}
3534

3635
/// Possible errors returned by the derived implementations of binary
3736
/// arithmetic or logic operations.
@@ -53,9 +52,8 @@ impl fmt::Display for BinaryError {
5352
}
5453
}
5554

56-
#[cfg(feature = "std")]
57-
impl std::error::Error for BinaryError {
58-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
55+
impl Error for BinaryError {
56+
fn source(&self) -> Option<&(dyn Error + 'static)> {
5957
match self {
6058
Self::Mismatch(e) => e.source(),
6159
Self::Unit(e) => e.source(),

src/convert.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub use self::try_into::TryIntoError;
77

88
#[cfg(feature = "try_from")]
99
mod try_from {
10-
use core::fmt;
10+
use core::{error::Error, fmt};
1111

1212
/// Error returned by the derived [`TryFrom`] implementation on enums to
1313
/// convert from their repr.
@@ -42,14 +42,13 @@ mod try_from {
4242
}
4343
}
4444

45-
#[cfg(feature = "std")]
46-
// `T` should only be an integer type and therefore be debug
47-
impl<T: fmt::Debug> std::error::Error for TryFromReprError<T> {}
45+
// `T` should only be an integer type and therefore be `Debug`.
46+
impl<T: fmt::Debug> Error for TryFromReprError<T> {}
4847
}
4948

5049
#[cfg(feature = "try_into")]
5150
mod try_into {
52-
use core::fmt;
51+
use core::{error::Error, fmt};
5352

5453
/// Error returned by the derived [`TryInto`] implementation.
5554
///
@@ -92,6 +91,5 @@ mod try_into {
9291
}
9392
}
9493

95-
#[cfg(feature = "std")]
96-
impl<T: fmt::Debug> std::error::Error for TryIntoError<T> {}
94+
impl<T: fmt::Debug> Error for TryIntoError<T> {}
9795
}

src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,7 @@ pub mod with_trait {
192192
UpperHex,
193193
);
194194

195-
#[cfg(not(feature = "std"))]
196195
re_export_traits!("error", error_traits, core::error, Error);
197-
#[cfg(feature = "std")]
198-
re_export_traits!("error", error_traits, std::error, Error);
199196

200197
re_export_traits!("from", from_traits, core::convert, From);
201198

src/ops.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Definitions used in derived implementations of [`core::ops`] traits.
22
3-
use core::fmt;
3+
use core::{error::Error, fmt};
44

55
/// Error returned by the derived implementations when an arithmetic or logic
66
/// operation is invoked on a unit-like variant of an enum.
@@ -24,5 +24,4 @@ impl fmt::Display for UnitError {
2424
}
2525
}
2626

27-
#[cfg(feature = "std")]
28-
impl std::error::Error for UnitError {}
27+
impl Error for UnitError {}

src/str.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::fmt;
1+
use core::{error::Error, fmt};
22

33
/// Error of parsing an enum value its string representation.
44
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
@@ -21,5 +21,4 @@ impl fmt::Display for FromStrError {
2121
}
2222
}
2323

24-
#[cfg(feature = "std")]
25-
impl std::error::Error for FromStrError {}
24+
impl Error for FromStrError {}

src/try_unwrap.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::{error::Error, fmt};
2+
13
/// Error returned by the derived [`TryUnwrap`] implementation.
24
///
35
/// [`TryUnwrap`]: macro@crate::TryUnwrap
@@ -32,8 +34,8 @@ impl<T> TryUnwrapError<T> {
3234
}
3335
}
3436

35-
impl<T> core::fmt::Display for TryUnwrapError<T> {
36-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
37+
impl<T> fmt::Display for TryUnwrapError<T> {
38+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3739
write!(
3840
f,
3941
"Attempt to call `{enum_name}::{func_name}()` on a `{enum_name}::{variant_name}` value",
@@ -44,5 +46,4 @@ impl<T> core::fmt::Display for TryUnwrapError<T> {
4446
}
4547
}
4648

47-
#[cfg(feature = "std")]
48-
impl<T: core::fmt::Debug> std::error::Error for TryUnwrapError<T> {}
49+
impl<T: fmt::Debug> Error for TryUnwrapError<T> {}

src/vendor/thiserror/aserror.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
#[cfg(not(feature = "std"))]
2-
use core::error::Error;
3-
#[cfg(feature = "std")]
4-
use std::error::Error;
5-
6-
use core::panic::UnwindSafe;
1+
use core::{error::Error, panic::UnwindSafe};
72

83
#[doc(hidden)]
94
pub trait AsDynError<'a>: Sealed {

tests/error/derives_for_enums_with_source.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#![allow(dead_code)] // some code is tested for type checking only
22

3+
#[cfg(not(feature = "std"))]
4+
use alloc::boxed::Box;
5+
36
use super::*;
47

58
type RenamedOption<T> = Option<T>;
@@ -15,7 +18,6 @@ enum TestErr {
1518
source: SimpleErr,
1619
field: i32,
1720
},
18-
#[cfg(feature = "std")]
1921
NamedImplicitBoxedSource {
2022
source: Box<dyn Error + Send + 'static>,
2123
field: i32,
@@ -24,7 +26,6 @@ enum TestErr {
2426
source: Option<SimpleErr>,
2527
field: i32,
2628
},
27-
#[cfg(feature = "std")]
2829
NamedImplicitOptionalBoxedSource {
2930
source: Option<Box<dyn Error + Send + 'static>>,
3031
field: i32,
@@ -49,7 +50,6 @@ enum TestErr {
4950
explicit_source: RenamedOption<SimpleErr>,
5051
field: i32,
5152
},
52-
#[cfg(feature = "std")]
5353
NamedExplicitRenamedOptionalBoxedSource {
5454
#[error(source(optional))]
5555
explicit_source: RenamedOption<Box<dyn Error + Send + 'static>>,
@@ -84,7 +84,6 @@ enum TestErr {
8484
#[error(source(optional))] RenamedOption<SimpleErr>,
8585
i32,
8686
),
87-
#[cfg(feature = "std")]
8887
UnnamedExplicitRenamedOptionalBoxedSource(
8988
#[error(source(optional))] RenamedOption<Box<dyn Error + Send + 'static>>,
9089
i32,
@@ -154,7 +153,6 @@ fn named_implicit_optional_source() {
154153
assert!(err.source().unwrap().is::<SimpleErr>());
155154
}
156155

157-
#[cfg(feature = "std")]
158156
#[test]
159157
fn named_implicit_boxed_source() {
160158
let err = TestErr::NamedImplicitBoxedSource {
@@ -166,7 +164,6 @@ fn named_implicit_boxed_source() {
166164
assert!(err.source().unwrap().is::<SimpleErr>());
167165
}
168166

169-
#[cfg(feature = "std")]
170167
#[test]
171168
fn named_implicit_optional_boxed_source() {
172169
let err = TestErr::NamedImplicitOptionalBoxedSource {
@@ -221,7 +218,6 @@ fn named_explicit_renamed_optional_source() {
221218
assert!(err.source().unwrap().is::<SimpleErr>());
222219
}
223220

224-
#[cfg(feature = "std")]
225221
#[test]
226222
fn named_explicit_renamed_optional_boxed_source() {
227223
let err = TestErr::NamedExplicitRenamedOptionalBoxedSource {
@@ -325,7 +321,6 @@ fn unnamed_explicit_renamed_optional_source() {
325321
assert!(err.source().unwrap().is::<SimpleErr>());
326322
}
327323

328-
#[cfg(feature = "std")]
329324
#[test]
330325
fn unnamed_explicit_renamed_optional_boxed_source() {
331326
let err = TestErr::UnnamedExplicitRenamedOptionalBoxedSource(

0 commit comments

Comments
 (0)