derive-where
Description
Derive macro to simplify deriving standard and other traits with custom generic type bounds.
Usage
The derive_where macro can be used just like std's #[derive(...)]
statements, with the only caveat that it requires to derive DeriveWhere
(#27):
;
This will generate trait implementations for Example for any T,
as opposed to std's derives, which would only implement these traits with
T: Trait bound to the corresponding trait.
In addition, the following convenience options are available:
Generic type bounds
Separated from the list of traits with a semi-colon, types to bind to can be
specified. This example will restrict the implementation for Example to
T: Clone:
;
It is also possible to specify the bounds to be applied. This will
bind implementation for Example to T: Super:
;
But more complex trait bounds are possible as well.
The example below will restrict the implementation for Example to
T::Type: Clone:
;
;
Any combination of options listed here can be used to satisfy a specific constrain. It is also possible to use multiple separate constrain specifications when required:
;
Enum default
Deriving Default on an enum is not possible in Rust at the moment.
Derive-where allows this with a default attribute:
Skipping fields
With a skip or skip_inner attribute fields can be skipped for traits
that allow it, which are: Debug, Hash, Ord, PartialOrd,
PartialEq and Zeroize.
] T);
assert_eq!;
assert_eq!;
It is also possible to skip all fields in an item or variant if desired:
;
assert_eq!;
assert_eq!;
Selective skipping of fields for certain traits is also an option, both in
skip and skip_inner:
;
assert_eq!;
assert_ne!;
Zeroize options
Zeroize has three options:
crate: an item-level option which specifies a path to thezeroizecrate in case of a re-export or rename.drop: an item-level option which implementsDropand usesZeroizeto erase all data from memory.fqs: a field -level option which will use fully-qualified-syntax instead of calling thezeroizemethod onselfdirectly. This is to avoid ambiguity between another method also calledzeroize.
)] i32);
let mut test = Example;
// Will call the struct method.
test.zeroize;
assert_eq!;
// WIll call the `Zeroize::zeroize` method.
zeroize;
assert_eq!;
Supported traits
The following traits can be derived with derive-where:
CloneCopyDebugDefaultEqHashOrdPartialEqPartialOrdZeroize: Only available with thezeroizecrate feature.
Supported items
Structs, tuple structs, unions and enums are supported. Derive-where tries
it's best to discourage usage that could be covered by std's derive. For
example unit structs and enums only containing unit variants aren't
supported.
Unions only support Clone and Copy.
no_std support
no_std support is provided by default.
Crate features
nightly: ImplementsOrdandPartialOrdwith the help ofcore::intrinsics::discriminant_value, which is what Rust does by default too. Without this featuretransmuteis used to convertDiscriminantto ai32, which is the underlying type.safe: ImplementsOrdandPartialOrdmanually. This is much slower, but might be preferred if you don't trust derive-where. It also replaces all cases ofcore::hint::unreachable_uncheckedinOrd,PartialEqandPartialOrd, which is what std uses, withunreachable.zeroize: Allows derivingZeroize.
MSRV
The current MSRV is 1.34 and is being checked by the CI. A change will be
accompanied by a minor version bump. If MSRV is important to you, use
derive-where = "~1.x" to pin a specific minor version to your crate.
Alternatives
derivative
()
is a great alternative with many options. Notably it has no
no_std
support.
Changelog
See the CHANGELOG file for details.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.