has

Crates.iohas
lib.rshas
version0.1.0
sourcesrc
created_at2016-04-09 23:22:35.718685
updated_at2016-04-09 23:22:35.718685
descriptionIntroducing 'has a' relationship for Rust's traits.
homepagehttps://github.com/Proksima/has
repositoryhttps://github.com/Proksima/has
max_upload_size
id4713
size4,370
Damien Levac (Proksima)

documentation

http://proksima.github.io/has-doc/has/index.html

README

'Has a' relationship for Rust's traits

This crate offers an alternative for a missing feature of the Rust Programming Language. That is, the possibility for traits to hold states.

[ License: MPL 2.0 Travis Build Status

Documentation

http://proksima.github.io/has-doc/has/index.html

Simple example

#[macro_use]
extern crate has;

use has::*;

struct Apple;

trait ApplesContainer: HasMut<Vec<Apple>> {
    fn take_apple(&mut self) -> Option<Apple> {
        self.get_mut().pop()
    }

    fn put_apple(&mut self, apple: Apple) {
        self.get_mut().push(apple);
    }
}

#[derive(Default)]
struct Basket {
    pub apples: Vec<Apple>,
}

impl ApplesContainer for Basket {}
impl_has!(Basket, Vec<Apple>, apples);

fn main() {
    let mut basket = Basket::default();

    basket.put_apple(Apple);
    basket.put_apple(Apple);
    basket.put_apple(Apple);

    basket.take_apple();

    assert_eq!(basket.apples.len(), 2);
}
Commit count: 4

cargo fmt