Automatically derive Deref/DerefMut implementations in Rust.
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
derefable fix: Added missing cargo metadata Jan 23, 2019
tests fix: Fix import in tests Jan 23, 2019
.gitignore feat: Initial commit Jan 23, 2019
.travis.yml chore: Added travis and README Jan 23, 2019
Cargo.toml feat: Initial commit Jan 23, 2019
README.md chore: fix code fences in README Jan 23, 2019

README.md

Derefable

Linux build status Documentation

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"));
}