Educe
This crate offers procedural macros designed to facilitate the swift implementation of Rust's built-in traits.
Features
By default, every trait this crate supports will be enabled. You can disable all of them by turning off the default features and enable only the traits that you want to use by adding them to the features
explicitly.
For example,
[]
= "*"
= ["Debug", "Clone", "Copy", "Hash", "Default"]
= false
Traits
Debug
Use #[derive(Educe)]
and #[educe(Debug)]
to implement the Debug
trait for a struct, enum, or union. This allows you to modify the names of your types, variants, and fields. You can also choose to ignore specific fields or set a method to replace the Debug
trait. Additionally, you have the option to format a struct as a tuple and vice versa.
Basic Usage
use Educe;
Change the Name of a Type, a Variant or a Field
The name
parameter can rename a type, a variant or a field. If you set it to false
, the name can be ignored or forced to show otherwise.
use Educe;
Ignore Fields
The ignore
parameter can ignore a specific field.
use Educe;
Fake Structs and Tuples
With the named_field
parameter, structs can be formatted as tuples and tuples can be formatted as structs.
use Educe;
Use Another Method to Handle the Formatting
The method
parameter can be utilized to replace the implementation of the Debug
trait for a field, eliminating the need to implement the Debug
trait for the type of that field.
use Educe;
use ;
Generic Parameters Bound to the Debug
Trait or Others
Generic parameters will be automatically bound to the Debug
trait if necessary.
use Educe;
Or you can set the where predicates by yourself.
use Educe;
use ;
In the above case, T
is bound to the Debug
trait, but K
is not.
Or, you can have educe
replicate the behaviour of std
's derive
's, where a bound is produced for every generic parameter, without regard to how it's used in the structure:
use Educe;
This can be useful if you don't want to make the trait implementation part of your permanent public API. In this example, Struct<T>
doesn't implement Debug
unless T
does. I.e., it has a T: Debug
bound even though that's not needed right now. Later we might want to display f
; we wouldn't then need to make a breaking API change by adding the bound.
This was the behaviour of Trait(bound)
in educe 0.4.x and earlier.
Union
A union will be formatted as a u8
slice because we don't know its fields at runtime. The fields of a union cannot be ignored, renamed, or formatted with other methods. The implementation is unsafe because it may expose uninitialized memory.
use Educe;
union Union
Clone
Use #[derive(Educe)]
and #[educe(Clone)]
to implement the Clone
trait for a struct, an enum, or a union. You can set a method to replace the Clone
trait.
Basic Usage
use Educe;
Use Another Method to Perform Cloning
The method
parameter can be utilized to replace the implementation of the Clone
trait for a field, eliminating the need to implement the Clone
trait for the type of that field.
use Educe;
Generic Parameters Bound to the Clone
Trait or Others
Generic parameters will be automatically bound to the Clone
trait if necessary. If the #[educe(Copy)]
attribute exists, they will be bound to the Copy
trait.
use Educe;
Or you can set the where predicates by yourself.
use Educe;
In the above case, T
is bound to the Clone
trait, but K
is not.
Or, you can have educe
replicate the behaviour of std
's derive
's by using bound(*)
. See the Debug
section for more information.
use Educe;
Union
Refer to the introduction of the #[educe(Copy)]
attribute.
Copy
Use #[derive(Educe)]
and #[educe(Copy)]
to implement the Copy
trait for a struct, an enum, or a union.
Basic Usage
use Educe;
Generic Parameters Bound to the Copy
Trait or Others
All generic parameters will be automatically bound to the Copy
trait.
use Educe;
Or you can set the where predicates by yourself.
use Educe;
Note that utilizing custom cloning methods for a type that implements the Copy
and Clone
traits may not be entirely appropriate.
Union
The #[educe(Copy, Clone)]
attribute can be used for a union. The fields of a union cannot be cloned with other methods.
use Educe;
union Union
PartialEq
Use #[derive(Educe)]
and #[educe(PartialEq)]
to implement the PartialEq
trait for a struct, enum, or union. You can also choose to ignore specific fields or set a method to replace the PartialEq
trait.
Basic Usage
use Educe;
Ignore Fields
The ignore
parameter can ignore a specific field.
use Educe;
Use Another Method to Perform Comparison
The method
parameter can be utilized to replace the implementation of the PartialEq
trait for a field, eliminating the need to implement the PartialEq
trait for the type of that field.
use Educe;
Generic Parameters Bound to the PartialEq
Trait or Others
Generic parameters will be automatically bound to the PartialEq
trait if necessary.
use Educe;
Or you can set the where predicates by yourself.
use Educe;
In the above case, T
is bound to the PartialEq
trait, but K
is not.
You can have educe
replicate the behaviour of std
's derive
's by using bound(*)
. See the Debug
section for more information.
use Educe;
Union
The #[educe(PartialEq(unsafe))]
attribute can be used for a union. The fields of a union cannot be compared with other methods. The implementation is unsafe because it disregards the specific fields it utilizes.
use Educe;
union Union
Eq
Use #[derive(Educe)]
and #[educe(Eq)]
to implement the Eq
trait for a struct, enum, or union. You can also choose to ignore specific fields or set a method to replace the PartialEq
trait.
Basic Usage
use Educe;
Ignore Fields
The ignore
parameter can ignore a specific field.
use Educe;
Use Another Method to Perform Comparison
The method
parameter can be utilized to replace the implementation of the Eq
trait for a field, eliminating the need to implement the PartialEq
trait for the type of that field.
use Educe;
Generic Parameters Bound to the PartialEq
Trait or Others
Generic parameters will be automatically bound to the PartialEq
trait if necessary.
use Educe;
Or you can set the where predicates by yourself.
use Educe;
Union
The #[educe(PartialEq(unsafe), Eq)]
attribute can be used for a union. The fields of a union cannot be compared with other methods. The implementation is unsafe because it disregards the specific fields it utilizes.
use Educe;
union Union
PartialOrd
Use #[derive(Educe)]
and #[educe(PartialOrd)]
to implement the PartialOrd
trait for a struct or enum. You can also choose to ignore specific fields or set a method to replace the PartialOrd
trait.
Basic Usage
use Educe;
Ignore Fields
The ignore
parameter can ignore a specific field.
use Educe;
Use Another Method to Perform Comparison
The method
parameter can be utilized to replace the implementation of the PartialOrd
trait for a field, eliminating the need to implement the PartialOrd
trait for the type of that field.
use Educe;
use Ordering;
Ranking
Each field can add a #[educe(PartialOrd(rank = priority_value))]
attribute, where priority_value
is an integer value indicating its comparison precedence (lower values indicate higher priority). The default priority_value
for a field depends on its ordinal position (lower towards the front) and starts with isize::MIN
.
use Educe;
For variants, the discriminant can be explicitly set for comparison.
use Educe;
Generic Parameters Bound to the PartialOrd
Trait or Others
Generic parameters will be automatically bound to the PartialOrd
trait if necessary.
use Educe;
Or you can set the where predicates by yourself.
use Educe;
use Ordering;
In the above case, T
is bound to the PartialOrd
trait, but K
is not.
You can have educe
replicate the behaviour of std
's derive
's by using bound(*)
. See the Debug
section for more information.
use Educe;
Ord
Use #[derive(Educe)]
and #[educe(Ord)]
to implement the Ord
trait for a struct or enum. You can also choose to ignore specific fields or set a method to replace the Ord
trait.
Basic Usage
use Educe;
Ignore Fields
The ignore
parameter can ignore a specific field.
use Educe;
Use Another Method to Perform Comparison
The method
parameter can be utilized to replace the implementation of the Ord
trait for a field, eliminating the need to implement the Ord
trait for the type of that field.
use Educe;
use Ordering;
Ranking
Each field can add a #[educe(Ord(rank = priority_value))]
attribute, where priority_value
is an integer value indicating its comparison precedence (lower values indicate higher priority). The default priority_value
for a field depends on its ordinal position (lower towards the front) and starts with isize::MIN
.
use Educe;
For variants, the discriminant can be explicitly set for comparison.
use Educe;
Generic Parameters Bound to the Ord
Trait or Others
Generic parameters will be automatically bound to the Ord
trait if necessary.
use Educe;
Or you can set the where predicates by yourself.
use Educe;
use Ordering;
Hash
Use #[derive(Educe)]
and #[educe(Hash)]
to implement the Hash
trait for a struct, enum, or union. You can also choose to ignore specific fields or set a method to replace the Hash
trait.
Basic Usage
use Educe;
Ignore Fields
The ignore
parameter can ignore a specific field.
use Educe;
Use Another Method for Hashing
The method
parameter can be utilized to replace the implementation of the Hash
trait for a field, eliminating the need to implement the Hash
trait for the type of that field.
use Educe;
use ;
Generic Parameters Bound to the Hash
Trait or Others
Generic parameters will be automatically bound to the Hash
trait if necessary.
use Educe;
Or you can set the where predicates by yourself.
use Educe;
use ;
In the above case, T
is bound to the Hash
trait, but K
is not.
You can have educe
replicate the behaviour of std
's derive
's by using bound(*)
. See the Debug
section for more information.
use Educe;
Union
The #[educe(PartialEq(unsafe), Eq, Hash(unsafe))]
attribute can be used for a union. The fields of a union cannot be hashed with other methods. The implementation is unsafe because it disregards the specific fields it utilizes.
use Educe;
union Union
Default
Use #[derive(Educe)]
and #[educe(Default)]
to implement the Default
trait for a struct, enum, or union. You can also choose to ignore specific fields or set a method to replace the Hash
trait.
Basic Usage
For enums and unions, it is necessary to designate a default variant (for enums) and a default field (for unions) unless the enum has only one variant or the union has only one field.
use Educe;
union Union
The Default Value for the Entire Type
use Educe;
union Union
You may need to activate the full
feature to enable support for advanced expressions.
The Default Values for Specific Fields
use Educe;
union Union
Generic Parameters Bound to the Default
Trait or Others
Generic parameters will be automatically bound to the Default
trait if necessary.
use Educe;
Or you can set the where predicates by yourself.
use Educe;
The new
Associated Function
With the #[educe(Default(new))]
attribute, your type will include an additional associated function called new
. This function can be utilized to invoke the default
method of the Default
trait.
use Educe;
Deref
Use #[derive(Educe)]
and #[educe(Deref)]
to implement the Deref
trait for a struct or enum.
Basic Usage
You must designate a field as the default for obtaining an immutable reference unless the number of fields is exactly one.
use Educe;
DerefMut
Use #[derive(Educe)]
and #[educe(DerefMut)]
to implement the DerefMut
trait for a struct or enum.
Basic Usage
You must designate a field as the default for obtaining an mutable reference unless the number of fields is exactly one.
use Educe;
The mutable dereferencing fields do not need to be the same as the immutable dereferencing fields, but their types must be consistent.
Into
Use #[derive(Educe)]
and #[educe(Into(type))]
to implement the Into<type>
trait for a struct or enum.
Basic Usage
You need to designate a field as the default for Into<type>
conversion unless the number of fields is exactly one. If you don't, educe will automatically try to find a proper one.
use Educe;
Use Another Method to Perform Into Conversion
The method
parameter can be utilized to replace the implementation of the Into
trait for a field, eliminating the need to implement the Into
trait for the type of that field.
use Educe;
Generic Parameters Bound to the Into
Trait or Others
Generic parameters will be automatically bound to the Into<type>
trait if necessary.
use Educe;
Or you can set the where predicates by yourself.
use Educe;
Crates.io
https://crates.io/crates/educe