pub struct Gens;Expand description
Factory responsibility for generating Gens.
Implementations§
Source§impl Gens
impl Gens
Sourcepub fn pure_lazy<B, F>(f: F) -> Gen<B>
pub fn pure_lazy<B, F>(f: F) -> Gen<B>
Generates a Gen that returns a value from a lazily evaluated function.
§Arguments
f- A closure that produces the value when called
§Returns
- A
Gen<B>that generates the value by calling the provided function
§Examples
use prop_check_rs::gen::Gens;
let expensive_computation = || {
// some expensive computation
42
};
let gen = Gens::pure_lazy(expensive_computation);Sourcepub fn some<B>(gen: Gen<B>) -> Gen<Option<B>>where
B: Clone + 'static,
pub fn some<B>(gen: Gen<B>) -> Gen<Option<B>>where
B: Clone + 'static,
Generates a Gen that wraps the value of another Gen in Some.
§Arguments
gen- The Gen whose values will be wrapped in Some
§Returns
- A
Gen<Option<B>>that always generates Some containing the value from the input Gen
§Type Parameters
B- The type of value to be wrapped in Some, must implement Clone and have a ’static lifetime
§Examples
use prop_check_rs::gen::Gens;
let number_gen = Gens::choose(1, 10);
let some_number_gen = Gens::some(number_gen); // Generates Some(1)..Some(10)Sourcepub fn option<B>(gen: Gen<B>) -> Gen<Option<B>>
pub fn option<B>(gen: Gen<B>) -> Gen<Option<B>>
Generates a Gen that returns Some or None based on the value of Gen. The probability distribution is 90% for Some and 10% for None.
§Arguments
gen- The Gen to be wrapped in an Option
§Returns
- A
Gen<Option<B>>that generates Some(value) 90% of the time and None 10% of the time
§Type Parameters
B- The type of value to be generated, must implement Debug, Clone and have a ’static lifetime
Sourcepub fn either<T, E>(gt: Gen<T>, ge: Gen<E>) -> Gen<Result<T, E>>
pub fn either<T, E>(gt: Gen<T>, ge: Gen<E>) -> Gen<Result<T, E>>
Generates a Gen that produces a Result type by combining two Gens.
§Arguments
gt- The Gen that produces the Ok variant valuesge- The Gen that produces the Err variant values
§Returns
- A
Gen<Result<T, E>>that randomly generates either Ok(T) or Err(E)
§Type Parameters
T- The success type, must implement Choose, Clone and have a ’static lifetimeE- The error type, must implement Clone and have a ’static lifetime
§Examples
use prop_check_rs::gen::Gens;
let success_gen = Gens::choose(1, 10);
let error_gen = Gens::pure("error");
let result_gen = Gens::either(success_gen, error_gen);Sourcepub fn frequency_values<B>(values: impl IntoIterator<Item = (u32, B)>) -> Gen<B>
pub fn frequency_values<B>(values: impl IntoIterator<Item = (u32, B)>) -> Gen<B>
Generates a Gen that produces values according to specified weights.
§Arguments
values- An iterator of tuples where the first element is the weight (u32) and the second element is the value to be generated. The probability of each value being generated is proportional to its weight.
§Returns
- A
Gen<B>that generates values with probabilities determined by their weights
§Panics
- Panics if all weights are 0
- Panics if no values are provided
§Examples
use prop_check_rs::gen::Gens;
let weighted_gen = Gens::frequency_values([
(2, "common"), // 2/3 probability
(1, "rare"), // 1/3 probability
]);Sourcepub fn frequency<B>(values: impl IntoIterator<Item = (u32, Gen<B>)>) -> Gen<B>
pub fn frequency<B>(values: impl IntoIterator<Item = (u32, Gen<B>)>) -> Gen<B>
Generates a Gen that produces values from other Gens according to specified weights.
§Arguments
values- An iterator of tuples where the first element is the weight (u32) and the second element is another Gen. The probability of each Gen being chosen is proportional to its weight.
§Returns
- A
Gen<B>that generates values by selecting and running other Gens based on their weights
§Panics
- Panics if all weights are 0
- Panics if no values are provided
§Implementation Notes
- Uses a BTreeMap for efficient weighted selection
- Filters out entries with zero weight
- Maintains cumulative weights for probability calculations
Sourcepub fn list_of_n<B>(n: usize, gen: Gen<B>) -> Gen<Vec<B>>where
B: Clone + 'static,
pub fn list_of_n<B>(n: usize, gen: Gen<B>) -> Gen<Vec<B>>where
B: Clone + 'static,
Generates a Gen that produces a vector of n values generated by another Gen.
§Arguments
n- The number of values to generategen- The Gen used to generate each value
§Returns
- A
Gen<Vec<B>>that generates a vector containing n values
§Performance
- Uses lazy evaluation internally for better memory efficiency
- For large n (>= 1000), consider using
list_of_n_chunkedorlist_of_n_chunked_optimalwhich may provide better performance through chunk-based processing
§Examples
use prop_check_rs::gen::Gens;
let number_gen = Gens::choose(1, 100);
let numbers_gen = Gens::list_of_n(5, number_gen);
// Could generate vec![42, 17, 89, 3, 71]Sourcepub fn list_of_n_chunked_optimal<B>(n: usize, gen: Gen<B>) -> Gen<Vec<B>>where
B: Clone + 'static,
pub fn list_of_n_chunked_optimal<B>(n: usize, gen: Gen<B>) -> Gen<Vec<B>>where
B: Clone + 'static,
Generates a Gen whose elements are the values generated by the specified number of Gen, using the optimal chunk size based on benchmarks.
Sourcepub fn list_of_n_chunked<B>(
n: usize,
chunk_size: usize,
gen: Gen<B>,
) -> Gen<Vec<B>>where
B: Clone + 'static,
pub fn list_of_n_chunked<B>(
n: usize,
chunk_size: usize,
gen: Gen<B>,
) -> Gen<Vec<B>>where
B: Clone + 'static,
Generates a Gen that produces a vector of values using chunk-based processing for better performance.
§Arguments
n- The number of values to generatechunk_size- The size of chunks for batch processing. For optimal performance: - Use 1000 for large n (>= 1000) - For smaller n, the provided chunk_size is used as isgen- The Gen used to generate each value
§Returns
- A
Gen<Vec<B>>that generates a vector containing n values
§Panics
- Panics if chunk_size is 0
§Performance Notes
- Processes values in chunks to reduce memory allocation overhead
- Automatically adjusts chunk size based on total number of elements
- More efficient than
list_of_nfor large datasets - Consider using
list_of_n_chunked_optimalfor automatic chunk size optimization
§Examples
use prop_check_rs::gen::Gens;
let number_gen = Gens::choose(1, 100);
let numbers_gen = Gens::list_of_n_chunked(1000, 100, number_gen);Sourcepub fn list_of_n_lazy<B>(n: usize, gen: Gen<B>) -> Gen<Vec<B>>where
B: Clone + 'static,
pub fn list_of_n_lazy<B>(n: usize, gen: Gen<B>) -> Gen<Vec<B>>where
B: Clone + 'static,
Generates a Gen that produces a vector of values using lazy evaluation.
§Arguments
n- The number of values to generategen- The Gen used to generate each value
§Returns
- A
Gen<Vec<B>>that generates a vector containing n values
§Performance Notes
- Uses lazy evaluation to generate values one at a time
- Minimizes memory usage by not pre-allocating all states
- More memory efficient than eager evaluation for large n
- May be slower than chunk-based processing for very large datasets
- Maintains consistent memory usage regardless of n
§Examples
use prop_check_rs::gen::Gens;
let number_gen = Gens::choose(1, 100);
let numbers_gen = Gens::list_of_n_lazy(5, number_gen);Sourcepub fn one<T: One>() -> Gen<T>
pub fn one<T: One>() -> Gen<T>
Generates a Gen that returns a single value using the One trait.
§Type Parameters
T- The type that implements the One trait
§Returns
- A
Gen<T>that generates values using the One trait implementation
§Implementation Notes
- Uses the
one()method from the One trait to generate values - Useful for types that have a natural “one” or “unit” value
§Examples
use prop_check_rs::gen::Gens;
let number_gen = Gens::one::<i32>(); // Generates using i32's One implementation
let bool_gen = Gens::one::<bool>(); // Generates using bool's One implementationSourcepub fn one_of<T: Choose + Clone + 'static>(
values: impl IntoIterator<Item = Gen<T>>,
) -> Gen<T>
pub fn one_of<T: Choose + Clone + 'static>( values: impl IntoIterator<Item = Gen<T>>, ) -> Gen<T>
Generates a Gen that produces values by randomly selecting from other Gens.
§Arguments
values- An iterator of Gens to choose from, with equal probability
§Returns
- A
Gen<T>that generates values by randomly selecting and running one of the input Gens
§Type Parameters
T- The type of value to generate, must implement Choose, Clone and have a ’static lifetime
§Panics
- Panics if the input iterator is empty
§Examples
use prop_check_rs::gen::Gens;
let small = Gens::choose(1, 10);
let large = Gens::choose(100, 200);
let combined = Gens::one_of([small, large]); // 50% chance of each rangeSourcepub fn one_of_values<T: Choose + Clone + 'static>(
values: impl IntoIterator<Item = T>,
) -> Gen<T>
pub fn one_of_values<T: Choose + Clone + 'static>( values: impl IntoIterator<Item = T>, ) -> Gen<T>
Generates a Gen that randomly selects from a set of fixed values.
§Arguments
values- An iterator of values to choose from, with equal probability
§Returns
- A
Gen<T>that generates values by randomly selecting one from the input set
§Type Parameters
T- The type of value to generate, must implement Choose, Clone and have a ’static lifetime
§Panics
- Panics if the input iterator is empty
§Examples
use prop_check_rs::gen::Gens;
// Note: &str doesn't implement Choose trait
// let colors = Gens::one_of_values(["red", "green", "blue"]);
let numbers = Gens::one_of_values([1, 10, 100]); // Equal probability for each numberSourcepub fn choose<T: Choose>(min: T, max: T) -> Gen<T>
pub fn choose<T: Choose>(min: T, max: T) -> Gen<T>
Generates a Gen that returns one randomly selected value from the specified maximum and minimum ranges of generic type.
Sourcepub fn choose_char(min: char, max: char) -> Gen<char>
pub fn choose_char(min: char, max: char) -> Gen<char>
Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type char.
Sourcepub fn choose_i64(min: i64, max: i64) -> Gen<i64>
pub fn choose_i64(min: i64, max: i64) -> Gen<i64>
Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type i64.
§Arguments
min- The minimum value (inclusive) of the rangemax- The maximum value (inclusive) of the range
§Returns
- A
Gen<i64>that generates random i64 values in the range [min, max]
§Panics
- Panics if
min > max(invalid range)
Sourcepub fn choose_u64(min: u64, max: u64) -> Gen<u64>
pub fn choose_u64(min: u64, max: u64) -> Gen<u64>
Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type u64.
Sourcepub fn choose_i32(min: i32, max: i32) -> Gen<i32>
pub fn choose_i32(min: i32, max: i32) -> Gen<i32>
Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type i32.
§Arguments
min- The minimum value (inclusive) of the rangemax- The maximum value (inclusive) of the range
§Returns
- A
Gen<i32>that generates random i32 values in the range [min, max]
§Panics
- Panics if
min > max(invalid range)
Sourcepub fn choose_u32(min: u32, max: u32) -> Gen<u32>
pub fn choose_u32(min: u32, max: u32) -> Gen<u32>
Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type u32.
Sourcepub fn choose_i16(min: i16, max: i16) -> Gen<i16>
pub fn choose_i16(min: i16, max: i16) -> Gen<i16>
Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type i16.
Sourcepub fn choose_u16(min: u16, max: u16) -> Gen<u16>
pub fn choose_u16(min: u16, max: u16) -> Gen<u16>
Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type u16.
Sourcepub fn choose_i8(min: i8, max: i8) -> Gen<i8>
pub fn choose_i8(min: i8, max: i8) -> Gen<i8>
Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type i8.
Sourcepub fn choose_u8(min: u8, max: u8) -> Gen<u8>
pub fn choose_u8(min: u8, max: u8) -> Gen<u8>
Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type u8.
Sourcepub fn choose_f64(min: f64, max: f64) -> Gen<f64>
pub fn choose_f64(min: f64, max: f64) -> Gen<f64>
Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type f64.
§Arguments
min- The minimum value (inclusive) of the rangemax- The maximum value (inclusive) of the range
§Returns
- A
Gen<f64>that generates random f64 values in the range [min, max]
§Note
- The distribution is uniform across the range
Sourcepub fn choose_f32(min: f32, max: f32) -> Gen<f32>
pub fn choose_f32(min: f32, max: f32) -> Gen<f32>
Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type f32.
§Arguments
min- The minimum value (inclusive) of the rangemax- The maximum value (inclusive) of the range
§Returns
- A
Gen<f32>that generates random f32 values in the range [min, max]
§Note
- The distribution is uniform across the range
Sourcepub fn even<T: Choose + Num + Copy + 'static>(
start: T,
stop_exclusive: T,
) -> Gen<T>
pub fn even<T: Choose + Num + Copy + 'static>( start: T, stop_exclusive: T, ) -> Gen<T>
Generates a Gen that returns randomly selected even numbers from a specified range.
§Arguments
start- The inclusive start of the rangestop_exclusive- The exclusive end of the range
§Type Parameters
T- A numeric type that implements Choose, Num, Copy, and ’static
§Returns
- A
Gen<T>that generates even numbers in the range [start, stop_exclusive)
§Implementation Notes
- If the start value is odd, the first even number after it will be used
- If stop_exclusive is odd, stop_exclusive - 1 will be used as the end of the range
- Maintains uniform distribution over even numbers in the range
§Examples
use prop_check_rs::gen::Gens;
let even_gen = Gens::even(1, 10); // Generates from {2, 4, 6, 8}Sourcepub fn odd<T: Choose + Num + Copy + 'static>(
start: T,
stop_exclusive: T,
) -> Gen<T>
pub fn odd<T: Choose + Num + Copy + 'static>( start: T, stop_exclusive: T, ) -> Gen<T>
Generates a Gen that returns randomly selected odd numbers from a specified range.
§Arguments
start- The inclusive start of the rangestop_exclusive- The exclusive end of the range
§Type Parameters
T- A numeric type that implements Choose, Num, Copy, and ’static
§Returns
- A
Gen<T>that generates odd numbers in the range [start, stop_exclusive)
§Implementation Notes
- If the start value is even, the first odd number after it will be used
- If stop_exclusive is even, stop_exclusive - 1 will be used as the end of the range
- Maintains uniform distribution over odd numbers in the range
§Examples
use prop_check_rs::gen::Gens;
let odd_gen = Gens::odd(1, 10); // Generates from {1, 3, 5, 7, 9}