pub struct Gen<A> { /* private fields */ }Expand description
Generator that generates values.
Implementations§
Source§impl<A: Clone + 'static> Gen<A>
impl<A: Clone + 'static> Gen<A>
Sourcepub fn run(self, rng: RNG) -> (A, RNG)
pub fn run(self, rng: RNG) -> (A, RNG)
Evaluates the Gen with a given RNG to produce a value.
§Arguments
rng- The random number generator to use
§Returns
- A tuple
(A, RNG)containing the generated value and the updated RNG state
§Examples
use prop_check_rs::gen::Gens;
use prop_check_rs::rng::RNG;
let gen = Gens::choose(1, 10);
let (value, new_rng) = gen.run(RNG::new());
assert!(value >= 1 && value <= 10);Sourcepub fn map<B, F>(self, f: F) -> Gen<B>
pub fn map<B, F>(self, f: F) -> Gen<B>
Transforms the output of a Gen using a function.
§Arguments
f- A function to apply to the generated value
§Returns
- A new
Gen<B>that applies the function to generated values
§Type Parameters
B- The result type after applying the functionF- The function type, must be Fn(A) -> B + ’static
§Examples
use prop_check_rs::gen::Gens;
let numbers = Gens::choose(1, 10);
let doubled = numbers.map(|x| x * 2); // Generates numbers from 2 to 20Sourcepub fn and_then<B, C, F>(self, g: Gen<B>, f: F) -> Gen<C>
pub fn and_then<B, C, F>(self, g: Gen<B>, f: F) -> Gen<C>
Combines two Gens using a function that takes both of their results.
§Arguments
g- Another Gen to combine with this onef- A function that combines the results of both Gens
§Returns
- A new
Gen<C>that combines the results of both Gens
§Type Parameters
B- The type of the second Gen’s outputC- The result type after combining both outputsF- The function type, must be Fn(A, B) -> C + ’static
§Examples
use prop_check_rs::gen::Gens;
let x = Gens::choose(1, 5);
let y = Gens::choose(6, 10);
let sum = x.and_then(y, |a, b| a + b); // Generates sums from 7 to 15Sourcepub fn flat_map<B, F>(self, f: F) -> Gen<B>
pub fn flat_map<B, F>(self, f: F) -> Gen<B>
Chains this Gen with a function that returns another Gen.
§Arguments
f- A function that takes the result of this Gen and returns a new Gen
§Returns
- A new
Gen<B>that represents the chained computation
§Type Parameters
B- The type of the resulting GenF- The function type, must be Fn(A) -> Gen + ’static
§Examples
use prop_check_rs::gen::Gens;
let numbers = Gens::choose(1, 3);
let repeated = numbers.flat_map(|n| Gens::list_of_n(n, Gens::pure(n)));Trait Implementations§
Auto Trait Implementations§
impl<A> Freeze for Gen<A>
impl<A> !RefUnwindSafe for Gen<A>
impl<A> !Send for Gen<A>
impl<A> !Sync for Gen<A>
impl<A> Unpin for Gen<A>
impl<A> !UnwindSafe for Gen<A>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more