Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
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
Trait Bounds
When a trait is derived with Educe and no explicit bound is set, the where predicates of the generated impl are determined automatically. Every field type that the generated code touches (ignored fields and fields handled by a custom method are excluded) is processed with the following rules, in order:
- A type that is known to implement the trait unconditionally produces no predicate at all. This covers
PhantomData, raw pointers, and function pointers for every trait, shared references forCloneandCopy, plus the types in table A. - A type that does not use any generic type parameter produces no predicate, because such a predicate would be constant.
- A std type that implements the trait whenever its type arguments do (table B) produces the predicates of its type arguments instead, with these rules applied recursively: a field of type
Option<T>producesT: Trait, and one of typeVec<Box<T>>produces justT: CloneforClone. - A type that mentions the derived type itself (e.g.
Box<List<T>>insideList<T>) producesParam: Traitbounds for the type parameters it uses, because a self-referencing predicate would overflow the trait solver (E0275). - Any other type produces the precise predicate
FieldType: Trait, so the compiler verifies the real requirement: a field of typeWrapper<T>whereWrapperhas its own conditionalCloneimpl producesWrapper<T>: Clone, which works for exactly the type arguments thatWrappersupports.
Table A — types whose type arguments never need a bound:
| Trait | Types |
|---|---|
Clone, Copy |
Arc, Rc, Weak, NonNull, Cow, Discriminant |
Debug |
Weak, NonNull, AtomicPtr, Discriminant |
PartialEq, Eq, Hash |
NonNull, Discriminant |
PartialOrd, Ord |
NonNull |
Default |
Option, Vec, VecDeque, LinkedList, HashMap, HashSet, BTreeMap, BTreeSet, Weak |
Table B — types that forward the trait to their type arguments:
| Trait | Types |
|---|---|
Clone |
Option, Result, Box, Vec, VecDeque, LinkedList, BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, RefCell, Wrapping, Reverse, Saturating |
Copy |
Option, Result, Wrapping, Reverse, Saturating |
Debug |
Option, Result, Box, Vec, VecDeque, LinkedList, BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, Arc, Rc, RefCell, Mutex, RwLock, Wrapping, Reverse, Saturating |
PartialEq, Eq, PartialOrd, Ord |
Option, Result, Box, Vec, VecDeque, LinkedList, BTreeMap, BTreeSet, Arc, Rc, RefCell, Wrapping, Reverse, Saturating |
Hash |
Option, Result, Box, Vec, VecDeque, LinkedList, BTreeMap, BTreeSet, Arc, Rc, Wrapping, Reverse, Saturating |
Default |
Box, Arc, Rc, Cell, RefCell, Mutex, RwLock, Wrapping, Reverse, Saturating |
HashMap and HashSet are not in the comparison rows of table B because their comparison impls additionally require K: Eq + Hash; such fields get the precise whole-type predicate from rule 5 instead.
Both tables match type names syntactically (by the last path segment), so a user-defined type that happens to share a name with one of these std types is treated the same way; if the resulting bounds do not fit such a type, set them explicitly with bound(...).
Bound Inheritance
When related traits are derived together with automatic bounds, a trait inherits the final predicates of its prerequisite traits: Eq and PartialOrd inherit from PartialEq, Ord inherits from Eq and PartialOrd, and Copy inherits from Clone. This way, a custom bound like #[educe(PartialEq(bound(T: MyTrait)), Eq)] automatically carries T: MyTrait into the Eq impl.
Educe cannot see the traits derived by other derive macros, including the built-in ones, so inheritance only applies between traits listed in the same #[educe(...)] attribute; a prerequisite trait implemented elsewhere contributes nothing.
Controlling the Bounds
bound(where_predicates)orbound = "where_predicates"uses exactly the given predicates, without inheritance.bound(*)addsParam: Traitfor every generic type parameter, like the built-in derives.bound(false)adds no predicates at all.
An explicit bound is used verbatim; if a prerequisite impl carries predicates that the explicit bound does not imply, the compiler reports an unsatisfied supertrait and the missing predicates have to be added by hand.
Limitations
- Mutually recursive generic types (an
A<T>containingVec<B<T>>whileB<T>containsA<T>) cannot be detected from a single type definition, so automatic bounds make the trait solver overflow (E0275) on them; usebound(*)or a custom bound for such types. - The precise predicates appear in the public where clause of the impl, so private field types become visible in documentation and error messages, and changing a private field type can change the public bounds of the impl.
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
The where predicates of the generated impl are determined from the field types automatically; see the "Trait Bounds" section above for the exact rules.
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.
Union
A union is formatted as a u8 slice, because its active field cannot be known at runtime. The fields of a union cannot be ignored, renamed, or formatted with other methods. The implementation is unsafe because it deliberately reads the whole memory of the union, including any padding bytes, which are not required to be initialized; the output may therefore 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
The where predicates of the generated impl are determined from the field types automatically; see the "Trait Bounds" section above for the exact rules.
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.
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
The where predicates of the generated impl are determined from the field types automatically; see the "Trait Bounds" section above for the exact rules. With automatic bounds, the Copy impl additionally inherits the predicates of the Clone impl generated by Educe, because Copy requires Clone.
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
The where predicates of the generated impl are determined from the field types automatically; see the "Trait Bounds" section above for the exact rules.
use Educe;
Or you can set the where predicates by yourself.
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. Eq is a marker trait, so it has no field attributes of its own; field-level equality settings such as ignore and method belong to the PartialEq attribute.
Basic Usage
use Educe;
Generic Parameters Bound to the Eq Trait or Others
The where predicates of the generated impl are determined from the field types automatically; see the "Trait Bounds" section above for the exact rules. With automatic bounds, the Eq impl also inherits the predicates of the PartialEq impl generated by Educe.
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 deliberately compares the whole memory of the two unions byte by byte, including any padding bytes, while disregarding 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.
When Ord is derived together, a field without its own PartialOrd attribute follows the ignore, rank, and method settings of its Ord attribute, so partial_cmp stays consistent with cmp; the result of an Ord comparison method is wrapped in Some automatically.
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
The where predicates of the generated impl are determined from the field types automatically; see the "Trait Bounds" section above for the exact rules. With automatic bounds, the PartialOrd impl also inherits the predicates of the PartialEq impl generated by Educe.
use Educe;
Or you can set the where predicates by yourself.
use Educe;
use Ordering;
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.
When PartialOrd is derived together, a field without its own Ord attribute follows the ignore and rank settings of its PartialOrd attribute; a PartialOrd comparison method returns an Option<Ordering> and cannot be used by cmp, so such a field is compared with the built-in comparison.
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
The where predicates of the generated impl are determined from the field types automatically; see the "Trait Bounds" section above for the exact rules. With automatic bounds, the Ord impl also inherits the predicates of the Eq and PartialOrd impls generated by Educe.
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
The where predicates of the generated impl are determined from the field types automatically; see the "Trait Bounds" section above for the exact rules.
use Educe;
Or you can set the where predicates by yourself.
use Educe;
use ;
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 deliberately hashes the whole memory of the union byte by byte, including any padding bytes, while disregarding 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.
Note that the expression is pasted into the generated default method verbatim, so for a generic type it has to be valid for every possible instantiation; an expression producing a concrete type does not work for a generic field.
The Default Values for Specific Fields
use Educe;
union Union
Generic Parameters Bound to the Default Trait or Others
The where predicates of the generated impl are determined from the field types automatically; see the "Trait Bounds" section above for the exact rules.
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 make a struct or enum convertible into another type.
Educe generates an impl From<YourType> for type, which automatically provides the corresponding Into through the standard library's blanket implementation. Use the bare into flag — #[educe(Into(type, into))] — to generate a direct impl Into<type> instead.
The into Flag
A From impl also lets callers write Target::from(value), whereas a direct Into impl only supports value.into(). Use the into flag when you deliberately want the conversion to be one-directional, exposed only as value.into().
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
A generic type parameter is automatically bound to Into<type> only when it is itself the type of a field, because a nested parameter cannot meaningfully receive an Into bound.
use Educe;
Or you can set the where predicates by yourself.
use Educe;
Crates.io
https://crates.io/crates/educe