This crate provides procedural macros to help you implement Rust-build-in traits quickly.
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 as default. Also, you can even format a struct to a tuple, and vice versa.
```rust
struct Struct { f1: u8 }
enum Enum { V1, V2 { f1: u8, }, V3(u8), } ```
A union will be formatted to a u8
slice, because we don't know it's field at runtime.
```rust
struct Union { f1: u8, f2: i32, } ```
The name
attribute can help you rename a type, a variant or a field.
```rust
struct Struct { #[educe(Debug(name = "f"))] f1: u8 }
enum Enum { #[educe(Debug(name = false))] V1, #[educe(Debug(name = "V"))] V2 { #[educe(Debug(name = "f"))] f1: u8, }, #[educe(Debug(name = false))] V3(u8), } ```
The ignore
attribute can ignore specific fields.
```rust
struct Struct { #[educe(Debug(ignore))] f1: u8 }
enum Enum { V1, V2 { #[educe(Debug(ignore))] f1: u8, }, V3( #[educe(Debug(ignore))] u8 ), } ```
With the named_field
attribute, structs can be formatted as tuples and tuples can be formatted as structs.
```rust
struct Struct { f1: u8 }
enum Enum { V1, #[educe(Debug(namedfield = false))] V2 { f1: u8, }, #[educe(Debug(namedfield = true))] V3( u8, #[educe(Debug(name = "value"))] i32 ), } ```
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 as default.
```rust
use std::fmt::{self, Formatter};
fn fmt(s: &u8, f: &mut Formatter) -> fmt::Result { f.writestr("Hi") }
trait A { fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.write_str("Hi") } }
impl A for i32 {}; impl A for u64 {};
enum Enum
Debug
trait or othersThe #[educe(Debug(bound))]
attribute can be used to add the Debug
trait bound to all generaic parameters for the Debug
implementation.
```rust
enum Enum
Or you can set the where predicates by yourself.
```rust
use std::fmt::{self, Formatter};
fn fmt(s: &u8, f: &mut Formatter) -> fmt::Result { f.writestr("Hi") }
trait A { fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.write_str("Hi") } }
impl A for i32 {}; impl A for u64 {};
enum Enum
There is a lot of work to be done. Unimplemented traits are listed below:
Default
Clone
Copy
Hash
PartialEq
Eq
PartialOrd
Ord
From
Into
FromStr
TryFrom
Deref
DerefMut
https://crates.io/crates/educe
https://docs.rs/educe