Crates.io | visitor |
lib.rs | visitor |
version | 0.2.1 |
source | src |
created_at | 2016-04-12 23:47:49.981795 |
updated_at | 2016-04-13 00:42:32.464741 |
description | A generic library to easily visit elements of a structure and perform an action on each one |
homepage | https://github.com/fuchsnj/visitor |
repository | https://github.com/fuchsnj/visitor |
max_upload_size | |
id | 4737 |
size | 2,033 |
A generic library to easily visit elements of a structure and perform an action on each one
Add this to your Cargo.toml
:
[dependencies]
visitor = "*"
and this to your crate root:
extern crate visitor;
struct Data{
a: u32,
b: u32
}
impl Visit<u32> for Data{
fn visit<V: Visitor<u32>>(&self, v: &mut V) -> Result<(),V::Error>{
try!(v.visit(self.a));
try!(v.visit(self.b));
Ok(())
}
}
struct AddVisitor{
value: u32
}
impl Visitor<u32> for AddVisitor{
type Error = ();
fn visit(&mut self, data: u32) -> Result<(), Self::Error>{
self.value += data;
Ok(())
}
}
#[test]
fn it_works() {
let data = Data{
a: 3,
b: 4
};
let mut adder = AddVisitor{
value: 0
};
data.visit(&mut adder).unwrap();
assert_eq!(adder.value, 7);
}