Crate has [] [src]

Introduce 'has a' relationship as a trait to Rust.

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

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

Macros

impl_has!

Macro to consisely implement HasMut for a struct. The macro takes as argument the struct name, the type of the contained object and the identifier, within the struct, of the contained object; in that order.

Traits

Has

Trait to model a "has a" relationship between implementing structs and the generic parameter provided. This trait provides only a function to retrieve a non-mutable reference to the contained object. If a mutable reference is desired instead, use HasMut.

HasMut

Trait to model a "has a" relationship between implementing structs and the generic parameter provided. This trait provides methods to retrieve either a mutable or immutable reference to the contained object.