#borrow #higher #duplicates #prevent #type #argument #kinded

nightly ref_clone

An implementation of borrows as higher kinded types that can be used to prevent code duplication

8 releases (breaking)

0.8.0 Nov 26, 2020
0.7.0 Oct 16, 2020
0.6.0 Oct 9, 2020
0.5.0 Sep 23, 2020
0.1.0 Sep 9, 2020

#11 in #prevent

25 downloads per month

MIT license

13KB
313 lines

This crate provides an implementation of a borrow as a higher kinded type.

This can be used to abstract over the type of borrow by passing the type of the borrow as a type argument.

Example:

#[RefAccessors]
struct Example {
    pub value: u8,
}
fn get_example_value<'a, T: RefType>(x: Ref<'a, Example, T>) -> Ref<'a, u8, T> {
    let x = x.to_wrapped();
    x.value
}
fn main() {
    let mut ex = Example {
        value: 8
    };
    {
        let ex_ref = Shared::new(&ex);
        println!("{}", get_example_value(ex_ref)); // = 8
    }
    {
        let ex_mut = Unique::new(&mut ex);
        *get_example_value(ex_mut).as_mut() = 1;
    }
    println!("{}", ex.value); // = 1
    {
        let ex_ref = Shared::new(&ex);
        println!("{}", get_example_value(ex_ref)); // = 1
    }
}

Dependencies

~1.5MB
~35K SLoC