Struct lazy_transducer::Builder [] [src]

pub struct Builder<'a, Input, Output> where
    Input: 'a + Copy,
    Output: 'a, 
{ /* fields omitted */ }

A builder is useful for when the transducer needs to be constructed incrementally, i.e., certain information is present later on, or is optional, etc.

Example

use lazy_transducer::{LazyTransducer, Builder};
use std::mem::size_of;
use std::mem::transmute;

let bytes: Vec<u8> = vec![1u8, 0, 2, 0, 3, 0, 4, 0];
let mut builder = Builder::new(&bytes);
let maybe_number_of_elements = Some(4);
if let Some(count) = maybe_number_of_elements {
  builder = builder.count(count);
}
// if the count was None, we'd have 0 elements, but the transducer is still constructable,
// similar to an empty iterator
let lt: LazyTransducer<_, u16> = builder.transducer(|input, index| {
    let start = size_of::<u16>() * index;
    unsafe { *transmute::<_, &u16>(&input[start]) }
  })
  .finish()
  .unwrap();

 // note: the data will be 1, 2, 3, 4, for little-endian machines, but not for big-endian
for (i, n) in lt.into_iter().enumerate() {
  println!("{}: {}", i, n);
}

Methods

impl<'a, Input, Output> Builder<'a, Input, Output> where
    Input: 'a + Copy,
    Output: 'a, 
[src]

[src]

Creates an empty builder; you must set the input and transducer before calling finish otherwise this is a runtime error.

[src]

Create a new builder with the given input; you must set the transducer before calling finish otherwise this is a runtime error.

[src]

Set (or reset) the input.

[src]

Set the number of output elements in the input source.

[src]

Set the transducer from input source to output elements.

[src]

Finish building the lazy transducer, and return it; if the input source or the transducer is missing this is a runtime error.

impl<'a, Output> Builder<'a, &'a [u8], Output>
[src]

[src]

Create a scroll-based transducer with the given parsing ctx.