visitor

Crates.iovisitor
lib.rsvisitor
version0.2.1
sourcesrc
created_at2016-04-12 23:47:49.981795
updated_at2016-04-13 00:42:32.464741
descriptionA generic library to easily visit elements of a structure and perform an action on each one
homepagehttps://github.com/fuchsnj/visitor
repositoryhttps://github.com/fuchsnj/visitor
max_upload_size
id4737
size2,033
Nathan (fuchsnj)

documentation

https://github.com/fuchsnj/visitor

README

Visitor

A generic library to easily visit elements of a structure and perform an action on each one

Usage

Add this to your Cargo.toml:

[dependencies]
visitor = "*"

and this to your crate root:

extern crate visitor;

Example

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);
}
Commit count: 10

cargo fmt