vec_box

Crates.iovec_box
lib.rsvec_box
version1.0.0
sourcesrc
created_at2017-01-15 14:32:58.083179
updated_at2017-01-15 14:32:58.083179
descriptionA single macro to create a vec of boxed elements, for trait objects
homepagehttps://github.com/derekdreery/vec_box
repositoryhttps://github.com/derekdreery/vec_box
max_upload_size
id8087
size3,117
Richard Dodd (derekdreery)

documentation

https://docs.rs/vec_box/1.0.0/vec_box/

README

vec_box!

A small macro to box up elements in an array for use with trait objects.

Example

#[macro_use]
extern crate vec_box;

struct A {
    x: u32
}

impl A {
    fn new(x: u32) -> A {
        A { x: x }
    }
}

struct B {
    x: u32,
    y: u32
}

impl B {
    fn new(x: u32) -> B {
        B { x: x, y: x }
    }
}

trait C {
    fn get(&self) -> u32;
}

impl C for A {
    fn get(&self) -> u32 {
        self.x
    }
}

impl C for B {
    fn get(&self) -> u32 {
        self.x + self.y
    }
}

fn main() {
    let v: Vec<Box<C>> = vec_box![
        A::new(1),
        B::new(1),
        A::new(2)
    ];

    assert_eq!(
        v.iter().fold(0, |acc, ref x| acc + x.get()),
        5
    );
}
Commit count: 6

cargo fmt