#macro-derive #traits #deref #defined #field #drerf-mut

macro derive-deref-rs

derive-deref-rs is a derive macro library used to easily defined the Derive and DrerfMut trait of the core library

2 releases

0.1.1 Apr 10, 2022
0.1.0 Apr 10, 2022

#868 in Procedural macros

Download history 99/week @ 2023-12-13 48/week @ 2023-12-20 18/week @ 2023-12-27 184/week @ 2024-01-03 290/week @ 2024-01-10 154/week @ 2024-01-17 264/week @ 2024-01-24 374/week @ 2024-01-31 543/week @ 2024-02-07 435/week @ 2024-02-14 476/week @ 2024-02-21 387/week @ 2024-02-28 341/week @ 2024-03-06 452/week @ 2024-03-13 351/week @ 2024-03-20 1207/week @ 2024-03-27

2,396 downloads per month
Used in 8 crates (6 directly)

MIT license

12KB
283 lines

Derive Deref Macro

Crate derive-deref-rs is a macro library used to easily defined the Derive and DrerfMut trait of the core library.

How to use #[derive(Deref)]

  • Structure with only one field:
#[derive(Deref)]
struct MyStruct {
    a: String
}

this will implement the Deref and DerefMut trait with the field a.

#[derive(Deref)]
struct MyStruct(String);

This will also work since it's have only one field.

  • Structure with multiple fields:
struct MyStruct {
    #[deref]
    a: String,
    b: String
}

When a structure has multiple field, we need to tell which field will be used to dereference the structure. To do that, simply add #[deref] on the field you want to use. You can only use this attribute once, otherwise a compile error will occur.

Limitation of the macro.

  • Does not support Enum and Union
  • Only support tuple struct with only one field.
  • Does not work on struct without any field.
  • Can't dereference a structure with 2 fields with #[deref] attribute.

Dependencies

~1.5MB
~33K SLoC