#derive #automatically

macro derefable

Automatically derive Deref/DerefMut implementations

1 unstable release

0.1.0 Jan 23, 2019
Download history 176/week @ 2022-12-03 233/week @ 2022-12-10 203/week @ 2022-12-17 97/week @ 2022-12-24 124/week @ 2022-12-31 264/week @ 2023-01-07 102/week @ 2023-01-14 249/week @ 2023-01-21 260/week @ 2023-01-28 246/week @ 2023-02-04 265/week @ 2023-02-11 272/week @ 2023-02-18 343/week @ 2023-02-25 181/week @ 2023-03-04 148/week @ 2023-03-11 205/week @ 2023-03-18

905 downloads per month
Used in 7 crates (4 directly)

MIT/Apache

7KB
98 lines

A procedural macro that allows you to derive std::ops::Deref and std::ops::DerefMut for your structs. This macro can only be derived on structs with atleast one field. You can specify which field you want to be deref'ed to with the #[deref] and allow mutable dereferencing with #[deref(mutable)].

Deriving std::ops::Deref

use std::collections::HashMap;

use derefable::Derefable;

#[derive(Default, Derefable)]
struct Map {
    #[deref]
    inner: HashMap<&'static str, &'static str>
}

fn main() {
    let map = Map::default();

    assert!(map.is_empty());
}

Deriving std::ops::DerefMut

use std::collections::HashMap;

use derefable::Derefable;

#[derive(Default, Derefable)]
struct MutableMap {
    #[deref(mutable)]
    inner: HashMap<&'static str, &'static str>
}

fn main() {
    let mut map = MutableMap::default();

    map.insert("Hello", "World");

    assert_eq!(map.get("Hello"), Some("World"));
}

Dependencies

~1.5MB
~42K SLoC