Struct Gens

Source
pub struct Gens;
Expand description

Factory responsibility for generating Gens.

Implementations§

Source§

impl Gens

Source

pub fn unit() -> Gen<()>

Generates a Gen that returns ().

§Returns
  • A Gen<()> that generates the unit value
Source

pub fn pure<B>(value: B) -> Gen<B>
where B: Clone + 'static,

Generates a Gen that returns a constant value.

§Arguments
  • value - The value to be wrapped in a Gen
§Returns
  • A Gen<B> that always generates the provided value
Source

pub fn pure_lazy<B, F>(f: F) -> Gen<B>
where F: Fn() -> B + 'static, B: Clone + 'static,

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);
Source

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)
Source

pub fn option<B>(gen: Gen<B>) -> Gen<Option<B>>
where B: Debug + Clone + 'static,

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
Source

pub fn either<T, E>(gt: Gen<T>, ge: Gen<E>) -> Gen<Result<T, E>>
where T: Choose + Clone + 'static, E: Clone + 'static,

Generates a Gen that produces a Result type by combining two Gens.

§Arguments
  • gt - The Gen that produces the Ok variant values
  • ge - 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 lifetime
  • E - 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);
Source

pub fn frequency_values<B>(values: impl IntoIterator<Item = (u32, B)>) -> Gen<B>
where B: Debug + Clone + 'static,

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
]);
Source

pub fn frequency<B>(values: impl IntoIterator<Item = (u32, Gen<B>)>) -> Gen<B>
where B: Debug + Clone + 'static,

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
Source

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 generate
  • gen - 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_chunked or list_of_n_chunked_optimal which 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]
Source

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.

Source

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 generate
  • chunk_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 is
  • gen - 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_n for large datasets
  • Consider using list_of_n_chunked_optimal for 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);
Source

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 generate
  • gen - 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);
Source

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 implementation
Source

pub fn one_i64() -> Gen<i64>

Generates a Gen that produces random i64 values.

§Returns
  • A Gen<i64> that generates random 64-bit signed integers
§Examples
use prop_check_rs::gen::Gens;
let gen = Gens::one_i64();  // Generates random i64 values
Source

pub fn one_u64() -> Gen<u64>

Generates a Gen that produces random u64 values.

§Returns
  • A Gen<u64> that generates random 64-bit unsigned integers
§Examples
use prop_check_rs::gen::Gens;
let gen = Gens::one_u64();  // Generates random u64 values
Source

pub fn one_i32() -> Gen<i32>

Generates a Gen that produces random i32 values.

§Returns
  • A Gen<i32> that generates random 32-bit signed integers
§Examples
use prop_check_rs::gen::Gens;
let gen = Gens::one_i32();  // Generates random i32 values
Source

pub fn one_u32() -> Gen<u32>

Generates a Gen that produces random u32 values.

§Returns
  • A Gen<u32> that generates random 32-bit unsigned integers
§Examples
use prop_check_rs::gen::Gens;
let gen = Gens::one_u32();  // Generates random u32 values
Source

pub fn one_i16() -> Gen<i16>

Generates a Gen that produces random i16 values.

§Returns
  • A Gen<i16> that generates random 16-bit signed integers
§Examples
use prop_check_rs::gen::Gens;
let gen = Gens::one_i16();  // Generates random i16 values
Source

pub fn one_u16() -> Gen<u16>

Generates a Gen that produces random u16 values.

§Returns
  • A Gen<u16> that generates random 16-bit unsigned integers
§Examples
use prop_check_rs::gen::Gens;
let gen = Gens::one_u16();  // Generates random u16 values
Source

pub fn one_i8() -> Gen<i8>

Generates a Gen that produces random i8 values.

§Returns
  • A Gen<i8> that generates random 8-bit signed integers
§Examples
use prop_check_rs::gen::Gens;
let gen = Gens::one_i8();  // Generates random i8 values
Source

pub fn one_u8() -> Gen<u8>

Generates a Gen that produces random u8 values.

§Returns
  • A Gen<u8> that generates random 8-bit unsigned integers
§Examples
use prop_check_rs::gen::Gens;
let gen = Gens::one_u8();  // Generates random u8 values
Source

pub fn one_char() -> Gen<char>

Generates a Gen that produces random char values.

§Returns
  • A Gen<char> that generates random characters
§Examples
use prop_check_rs::gen::Gens;
let gen = Gens::one_char();  // Generates random characters
Source

pub fn one_bool() -> Gen<bool>

Generates a Gen that produces random boolean values.

§Returns
  • A Gen<bool> that generates random true/false values
§Examples
use prop_check_rs::gen::Gens;
let gen = Gens::one_bool();  // Generates random booleans
Source

pub fn one_f64() -> Gen<f64>

Generates a Gen that produces random f64 values.

§Returns
  • A Gen<f64> that generates random 64-bit floating point numbers
§Examples
use prop_check_rs::gen::Gens;
let gen = Gens::one_f64();  // Generates random f64 values
Source

pub fn one_f32() -> Gen<f32>

Generates a Gen that produces random f32 values.

§Returns
  • A Gen<f32> that generates random 32-bit floating point numbers
§Examples
use prop_check_rs::gen::Gens;
let gen = Gens::one_f32();  // Generates random f32 values
Source

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 range
Source

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 number
Source

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.

Source

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.

Source

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 range
  • max - 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)
Source

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.

Source

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 range
  • max - 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)
Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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 range
  • max - 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
Source

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 range
  • max - 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
Source

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 range
  • stop_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}
Source

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 range
  • stop_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}

Auto Trait Implementations§

§

impl Freeze for Gens

§

impl RefUnwindSafe for Gens

§

impl Send for Gens

§

impl Sync for Gens

§

impl Unpin for Gens

§

impl UnwindSafe for Gens

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V