Struct lazy_transducer::LazyTransducer
[−]
[src]
pub struct LazyTransducer<'a, Input, Output> where
Input: 'a + Copy,
Output: 'a, { /* fields omitted */ }
A lazy transducer transforms n
elements from a source type into an output type.
The transformer is called the transducer, which receives the original source input, and an index
corresponding to the i
th element, and returns the corresponding i
th element out of that source.
Importantly, lazy transducers are:
- Lazy - it never parses any elements unless you request it to do so
- Iterable - one can iterate over every element
- Indexable - accessing an element is O(1)
- Parallel - one can iterate in parallel over every element
Basic Example
We need to provide a data source, the number of elements in the data source, and the means of extracting the elements out of the data source (the transducer).
For a simple case, we can consider a backing array of u32
s, which we cast to u64
s.
extern crate lazy_transducer; use lazy_transducer::LazyTransducer; let data = [0xdeadbeefu32, 0xcafed00d]; let lt: LazyTransducer<&[u32], u64> = LazyTransducer::new(&data, 2, |input, idx| input[idx] as u64); let cafedood = lt.get(1).expect("has 2 elements"); assert_eq!(cafedood, 0xcafed00d); for (i, elem) in lt.into_iter().enumerate() { println!("{}: {}", i, elem); }
Advanced Example
This example uses the bincode binary serializer as its transducer.
extern crate lazy_transducer; #[macro_use] extern crate serde_derive; extern crate serde; extern crate bincode; extern crate rayon; use lazy_transducer::LazyTransducer; use bincode::{serialize, deserialize, Infinite, Error}; use rayon::prelude::*; #[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct Foo { x: u64, y: f32, z: bool, } fn run() -> Result<(), Error> { let foo1 = Foo { x: 0xcafed00d, y: 0.75, z: false }; let foo2 = Foo { x: 0xdeadbeef, y: 0.50, z: true }; // we need to serialize the data, which we do by extending a byte vector with the individually // serialized components let mut data = serialize(&foo1, Infinite)?; let sizeof_serialized_element = data.len(); data.extend_from_slice(&serialize(&foo2, Infinite)?); // we construct our transducer by providing the serialized bytes _and_ the size of a serialized // element as input; our transducer just reads at the appropriate byte offset, and deserializes! let lt: LazyTransducer<_, Result<Foo, Error>> = LazyTransducer::new((data.as_slice(), sizeof_serialized_element), 2, |(input, size), idx| { deserialize(&input[(idx * size)..]) }); let foo2_ = lt.get(1).expect("has 2 elements")?; assert_eq!(foo2, foo2_); // and now with the help of rayon, we iterate over the items in parallel lt.into_par_iter().for_each(|elem| { println!("{:?}", elem); }); Ok(()) }
Methods
impl<'a, Input, Output> LazyTransducer<'a, Input, Output> where
Input: 'a + Copy,
Output: 'a,
[src]
Input: 'a + Copy,
Output: 'a,
fn len(&self) -> usize
[src]
How many elements are contained in this lazy transducer
fn new(
contents: Input,
count: usize,
transducer: fn(_: Input, _: usize) -> Output
) -> Self
[src]
contents: Input,
count: usize,
transducer: fn(_: Input, _: usize) -> Output
) -> Self
Create a new LazyTransducer with count
elements in contents
, using transducer
to extract
them.
Example
use lazy_transducer::LazyTransducer; let data = [0xdeadbeefu32, 0xcafed00d]; let lt: LazyTransducer<&[u32], u64> = LazyTransducer::new(&data, 2, |input, idx| input[idx] as u64);
fn get(&self, idx: usize) -> Option<Output>
[src]
Get an element out of the lazy transducer, returning None
if the index is greater than
the number of elements in this lazy transducer.
Example
extern crate lazy_transducer; use lazy_transducer::LazyTransducer; let data = [0xdeadbeefu64, 0xcafed00d]; let lt: LazyTransducer<&[u64], &u64> = LazyTransducer::new(&data, 2, |input, idx| &input[idx]); let cafedood = lt.get(1).expect("has 2 elements"); assert_eq!(*cafedood, 0xcafed00d); assert!(lt.get(2).is_none());
Trait Implementations
impl<'a, Input: Debug, Output: Debug> Debug for LazyTransducer<'a, Input, Output> where
Input: 'a + Copy,
Output: 'a,
[src]
Input: 'a + Copy,
Output: 'a,
impl<'a, Input: Copy, Output> Clone for LazyTransducer<'a, Input, Output>
[src]
fn clone(&self) -> Self
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<'a, Input: Copy, Output> IntoIterator for LazyTransducer<'a, Input, Output>
[src]
type Item = Output
The type of the elements being iterated over.
type IntoIter = IntoIter<'a, Input, Output>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
Creates an iterator from a value. Read more
impl<'a, 'b, Input: Copy, Output> IntoIterator for &'b LazyTransducer<'a, Input, Output>
[src]
type Item = Output
The type of the elements being iterated over.
type IntoIter = IntoIter<'a, Input, Output>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
Creates an iterator from a value. Read more