Releases: JelteF/derive_more
v1.0.0 - Finally a stable release
derive_more
is a library that adds derives for many of the standard library traits. By using this library the following code just works:
use derive_more::{Add, Display, From, Into};
#[derive(PartialEq, From, Add)]
struct MyInt(i32);
#[derive(PartialEq, From, Into)]
struct Point2D {
x: i32,
y: i32,
}
#[derive(PartialEq, From, Add, Display)]
enum MyEnum {
#[display("int: {_0}")]
Int(i32),
Uint(u32),
#[display("nothing")]
Nothing,
}
assert!(MyInt(11) == MyInt(5) + 6.into());
assert!((5, 6) == Point2D { x: 5, y: 6 }.into());
assert!(MyEnum::Int(15) == (MyEnum::Int(8) + 7.into()).unwrap());
assert!(MyEnum::Int(15).to_string() == "int: 15");
assert!(MyEnum::Uint(42).to_string() == "42");
assert!(MyEnum::Nothing.to_string() == "nothing");
Now, more than 8 years after the first commit and almost 5 years after the 0.99.0 release, derive_more
has finally reached its 1.0.0 release. This release contains a lot of changes (including some breaking ones) to make it easier to use the derives and make it possible to extend them without having to break backwards compatibility again. There are five major changes that I would like to call out, but there are many more changes that are documented below:
- There is a new
Debug
derive that can be used to easily customizeDebug
formatting. - A greatly improved
Display
derive, which allows you to do anything thatthiserror
provides, but it works for any type not just errors. And by combining theDisplay
derive with theError
andFrom
derives, there shouldn't really be any need to usethiserror
anymore (if you are missing a feature/behaviour fromthiserror
please report an issue). - Traits that can return errors now return a type that implements
Error
when an error occurs instead of a&'static str
. - When using
use derive_more::SomeTrait
the actual trait is also imported not just the derive macro. This is especially useful forError
and
Display
- The docs are now rendered on docs.rs and are much better overall.
Breaking changes
- The minimum supported Rust version (MSRV) is now Rust 1.75.
- Add the
std
feature which should be disabled inno_std
environments. - All Cargo features, except
std
, are now disabled by default. Thefull
feature can be used to get the old behavior of supporting all possible derives. - The
TryFrom
,Add
,Sub
,BitAnd
,BitOr
,BitXor
,Not
andNeg
derives now return a dedicated error type instead of a&'static str
on error. - The
FromStr
derive now uses a dedicatedFromStrError
error type instead of generating unique one each time. - The
Display
derive (and otherfmt
-like ones) now uses#[display("...", (<expr>),*)]
syntax instead of#[display(fmt = "...", ("<expr>"),*)]
, and#[display(bound(<bound>))]
instead of#[display(bound = "<bound>")]
. So without the double quotes around the expressions and bounds. - The
Debug
andDisplay
derives (and otherfmt
-like ones) now transparently delegate to the inner type when#[display("...", (<expr>),*)]
attribute is trivially substitutable with a transparent call. (#322) - The
DebugCustom
derive is renamed to justDebug
(gated now under a separatedebug
feature), and its semantics were changed to be a superset ofstd
variant ofDebug
. - The
From
derive doesn't deriveFrom<()>
for enum variants without any fields anymore. This feature was removed because it was considered useless in practice. - The
From
derive now uses#[from(<types>)]
instead of#[from(types(<types>))]
and ignores field type itself. - The
Into
derive now uses#[into(<types>)]
instead of#[into(types(<types>))]
and ignores field type itself. - The
Into
derive now generates separate impls for each field whenever the#[into(...)]
attribute is applied to it. (#291) - Importing a derive macro now also imports its corresponding trait.
- The
Error
derive is updated with changes to theerror_generic_member_access
unstable feature for nightly users. (#200, #294) - The
as_mut
feature is removed, and theAsMut
derive is now gated by theas_ref
feature. (#295) - A top level
#[display("...")]
attribute on an enum now requires the usage of{_variant}
to include the variant instead of including it at{}
. The reason is that{}
now references the first argument to the format string, just like in all other format strings. (#377)
Added
- Add support captured identifiers in
Display
derives. So now you can use:#[display(fmt = "Prefix: {field}")]
instead of needing to use#[display(fmt = "Prefix: {}", field)]
- Add
FromStr
derive support for enums that contain variants without fields. If you pass the name of the variant tofrom_str
it will create the matching variant. - Add
#[unwrap(owned, ref, ref_mut)]
attribute for theUnwrap
derive. By using them, it is possible to derive implementations for the reference types as well. (#206) - Add
TryUnwrap
derive similar to theUnwrap
derive. This one returns aResult
and does not panic. (#206) - Add support for container format in
Debug
derive with the same syntax asDisplay
derives. (#279) derive_more::derive
module exporting only macros, without traits. (#290)- Add support for specifying concrete types to
AsRef
/AsMut
derives. (#298) - Add
TryFrom
derive for enums to convert from their discriminant. (#300) #[inline]
attributes toIsVariant
andDebug
implementations. (#334- Add
#[track_caller]
toAdd
,Mul
,AddAssign
andMulAssign
derives (#378
Changed
- The
Constructor
andIsVariant
derives now generateconst fn
functions. - Static methods derived by
IsVariant
are now marked#[must_use]
. (#350) - The
Unwrap
andIsVariant
derives now generate doc comments. #[automatically_derived]
is now emitted from all macro expansions. This should prevent code style linters from attempting to modify the generated code.- Upgrade to
syn
2.0. - The
Error
derive now works in nightlyno_std
environments
Fixed
- Use a deterministic
HashSet
in all derives, this is needed for rust analyzer to work correctly. - Use
Provider
API for backtraces inError
derive. - Fix
Error
derive not working withconst
generics. - Support trait objects for source in Error, e.g.
Box<dyn Error + Send + 'static>
- Fix bounds on derived
IntoIterator
impls for generic structs. (#284) - Fix documentation of generated bounds in
Display
derive. (#297) - Hygiene of macro expansions in presence of custom
core
crate. (#327) - Fix documentation of generated methods in
IsVariant
derive. - Make
{field:p}
do the expected thing in format strings forDisplay
andDebug
. Also document weirdness aroundPointer
formatting when using expressions, due to field variables being references. (#381)
New Contributors
- @Fuuzetsu made their first contribution in #178
- @Robrisi made their first contribution in #183
- @Technohacker made their first contribution in #179
- @aj-bagwell made their first contribution in #176
- @momirza made their first contribution in #187
- @phip1611 made their first contribution in #192
- @tvercruyssen made their first contribution in #196
- @fmiguelgarcia made their first contribution in #218
- @zohnannor made their first contribution in #238
- @rassvet2 made their first contribution in #206
- @dependabot made their first contribution in #251
- @pymongo made their first contribution in #264
- @ModProg made their first contribution in #279
- @MegaBluejay made their first contribution in #284
- @fritzrehde made their first contribution in #310
- @DaniPopes made their first contribution in #313
- @TheLostLambda made their first contribution in #350
- @alpha-tango-kilo made their first contribution in #360
- @wangcundashang made their first contribution in #372
- @DenisGorbachev made their first contribution in #374
- @sornas made their first contribution in #371
Full Changelog: https://github.com/JelteF/derive_mo...
1.0.0-beta.7
1.0.0-beta.6
1.0.0-beta.3
1.0.0-beta.2
1.0.0-beta.1
There is still a few issues/PRs that we plan to address for the final 1.0 release: https://github.com/JelteF/derive_more/milestone/3
So if you use this be prepared for some more slight breaking changes.