1 unstable release

0.1.0 Jan 23, 2019

#11 in #deref-mut

Download history 106/week @ 2024-01-01 137/week @ 2024-01-08 251/week @ 2024-01-15 165/week @ 2024-01-22 346/week @ 2024-01-29 173/week @ 2024-02-05 193/week @ 2024-02-12 180/week @ 2024-02-19 279/week @ 2024-02-26 169/week @ 2024-03-04 220/week @ 2024-03-11 183/week @ 2024-03-18 198/week @ 2024-03-25 224/week @ 2024-04-01 133/week @ 2024-04-08 175/week @ 2024-04-15

742 downloads per month
Used in 6 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

~2MB
~45K SLoC