Crate read_transform

Source
Expand description

ReadTransformer is a struct for functional Read objects processing.

It takes Read object, map function and acts as medium Read object.

§Example

let mut data = Cursor::new(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
let mut transformed = ReadTransformer::new(
	&mut data,
	5,
	Box::new(|buffer: &mut [u8], _position, _last_attempt| -> Option<(Vec<u8>, usize)> {
		return Some((
			buffer
				.iter()
				.map(|x| {
					if x % 2 == 0 {
						return 0;
					};
					return *x;
				})
				.collect::<Vec<_>>(),
			buffer.len(),
		));
	}),
);
let mut out = vec![0; 10];
transformed.read_exact(&mut out).unwrap();
assert_eq!(out, [1, 0, 3, 0, 5, 0, 7, 0, 9, 0]);

Structs§

ReadTransformer
Transforms Read object with function

Traits§

TransformableRead
Convenience trait which implemented by all Read objects. Allows chaining of Read objects.

Type Aliases§

TransformFn
transform function which takes buffer and returns Vec<u8> and length of processed bytes