Trait smoke::Generator [−][src]
pub trait Generator {
type Item;
fn gen(&self, r: &mut R) -> Self::Item;
fn map<O, F>(self, f: F) -> Map<Self, F>
where
Self: Sized,
F: Fn(Self::Item) -> O,
{ ... }
fn such_that<F>(self, f: F) -> SuchThat<Self, F>
where
Self: Sized,
F: Fn(Self::Item) -> bool + Clone,
{ ... }
fn and<G>(self, other: G) -> And<Self, G>
where
Self: Sized,
{ ... }
fn or<G>(self, other: G) -> Or<Self, G>
where
Self: Sized,
G: Generator<Item = Self::Item>,
{ ... }
fn into_boxed(self) -> BoxGenerator<Self::Item>
where
Self: Sized + 'static,
{ ... }
}
Expand description
Generator for an Item
The interface is very similar to an Iterator, except next
is gen
Associated Types
Required methods
Provided methods
Map the output of a generator through a function
use smoke::{Generator, generator::num};
let generator = num::<u32>().map(|n| n + 1);
Filter the generated items such that only the item that matches the predicate ‘f’ are returned.
Due to being an user controlled callback, it’s not recommended to do heavy filtering with this function and instead use a generator that generate data that is closer to the end result. A general rule of thumb, is that if the callback accept less than half the generated value, then it should probably be refined at the source generator.
use smoke::{Generator, generator::range};
// u32 number between 1 and 1000 that are odd only
let odd_gen = range(1u32..1000).such_that(|n| (n & 0x1) == 1);
Combine two arbitrary generators into one that generate tuple item of both generators, transforming generator for A and generator for B into one generator of (A,B)
use smoke::{Generator, generator::{Num, num}};
let generator_a : Num<u32> = num();
let generator_b : Num<u64> = num();
let generator = generator_a.and(generator_b);
This generator or another one.
It’s not recommended to use this combinator to chain more than one generator, as the generator on the “left” will have a 1/2 handicapped at each iteration.
Prefered choose()
to do a unbiased choice or frequency()
to
control the distribution between generator.
fn into_boxed(self) -> BoxGenerator<Self::Item> where
Self: Sized + 'static,
fn into_boxed(self) -> BoxGenerator<Self::Item> where
Self: Sized + 'static,
Box a generator into a monomorphic fixed-sized type, that is easier to handle