Struct proptest::sample::Selector

source ·
pub struct Selector { /* private fields */ }
Expand description

A value for picking random values out of iterators.

This is, in a sense, a more flexible variant of Index in that it can operate on arbitrary IntoIterator values.

Initially, the selection is roughly uniform, with a very slight bias towards items earlier in the iterator.

Shrinking causes the selection to move toward items earlier in the iterator, ultimately settling on the very first, but this currently happens in a very haphazard way that may fail to find the earliest failing input.

§Example

Generate a non-indexable collection and a value to pick out of it.

use proptest::prelude::*;

proptest! {
    #[test]
    fn my_test(
        names in prop::collection::hash_set("[a-z]+", 10..20),
        selector in any::<prop::sample::Selector>()
    ) {
        println!("Selected name: {}", selector.select(&names));
        // Test stuff...
    }
}

Implementations§

source§

impl Selector

source

pub fn select<T: IntoIterator>(&self, it: T) -> T::Item

Pick a random element from iterable it.

The selection is unaffected by the elements themselves, and is dependent only on the actual length of it.

it is always iterated completely.

§Panics

Panics if it has no elements.

source

pub fn try_select<T: IntoIterator>(&self, it: T) -> Option<T::Item>

Pick a random element from iterable it.

Returns None if it is empty.

The selection is unaffected by the elements themselves, and is dependent only on the actual length of it.

it is always iterated completely.

Trait Implementations§

source§

impl Arbitrary for Selector

§

type Parameters = ()

The type of parameters that arbitrary_with accepts for configuration of the generated Strategy. Parameters must implement Default.
§

type Strategy = SelectorStrategy

The type of Strategy used to generate values of type Self.
source§

fn arbitrary_with(_: ()) -> SelectorStrategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). The strategy is passed the arguments given in args. Read more
source§

fn arbitrary() -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). Read more
source§

impl Clone for Selector

source§

fn clone(&self) -> Selector

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Selector

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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