1 unstable release

0.1.0 Jan 23, 2019

#12 in #deref-mut

Download history 222/week @ 2024-07-25 217/week @ 2024-08-01 272/week @ 2024-08-08 244/week @ 2024-08-15 223/week @ 2024-08-22 199/week @ 2024-08-29 156/week @ 2024-09-05 186/week @ 2024-09-12 175/week @ 2024-09-19 221/week @ 2024-09-26 343/week @ 2024-10-03 174/week @ 2024-10-10 282/week @ 2024-10-17 139/week @ 2024-10-24 267/week @ 2024-10-31 119/week @ 2024-11-07

833 downloads per month
Used in 7 crates (5 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
~48K SLoC