Educe
This crate provides procedural macros to help you implement Rust-build-in traits quickly.
Debug
Use #[derive(Educe)]
and #[educe(Debug)]
to implement the Debug
trait for a struct, an enum, or a union. It supports to change the name of your types, variants and fields. You can also ignore some fields, or set a trait and/or a method to replace the Debug
trait used by default. Also, you can even format a struct to a tuple, and vice versa.
Basic Usage
extern crate educe;
Change the Name of a Type, a Variant or a Field
The name
attribute can help you rename a type, a variant or a field.
extern crate educe;
Ignore Fields
The ignore
attribute can ignore specific fields.
extern crate educe;
Fake Structs and Tuples
With the named_field
attribute, structs can be formatted as tuples and tuples can be formatted as structs.
extern crate educe;
Use Another Method or Trait to Do the Format Thing
The format
attribute has two parameters: trait
and method
. They can be used to replace the Debug
trait on fields. If you only set the trait
parameter, the method
will be set to fmt
automatically by default.
extern crate educe;
use ;
;
;
Generic Parameters Bound to the Debug
Trait or Others
The #[educe(Debug(bound))]
attribute can be used to add the Debug
trait bound to all generaic parameters for the Debug
implementation.
extern crate educe;
Or you can set the where predicates by yourself.
extern crate educe;
use ;
;
;
Union
A union will be formatted to a u8
slice, because we don't know it's field at runtime. The fileds of a union cannot be ignored, renamed or formated with other methods or traits.
extern crate educe;
Hash
Use #[derive(Educe)]
and #[educe(Hash)]
to implement the Hash
trait for a struct or an enum. It supports to ignore some fields, or set a trait and/or a method to replace the Hash
trait used by default.
Basic Usage
extern crate educe;
Ignore Fields
The ignore
attribute can ignore specific fields.
extern crate educe;
Use Another Method or Trait to Do Hashing
The hash
attribute has two parameters: trait
and method
. They can be used to replace the Hash
trait on fields. If you only set the trait
parameter, the method
will be set to hash
automatically by default.
extern crate educe;
use ;
;
;
Generic Parameters Bound to the Hash
Trait or Others
The #[educe(Hash(bound))]
attribute can be used to add the Hash
trait bound to all generaic parameters for the Hash
implementation.
extern crate educe;
Or you can set the where predicates by yourself.
extern crate educe;
use ;
;
;
TODO
There is a lot of work to be done. Unimplemented traits are listed below:
Default
Clone
Copy
PartialEq
Eq
PartialOrd
Ord
From
Into
FromStr
TryFrom
Deref
DerefMut
Crates.io
https://crates.io/crates/educe