Trait sample_std::Sample

source ·
pub trait Sample {
    type Output;

    // Required method
    fn generate(&mut self, g: &mut Random) -> Self::Output;

    // Provided methods
    fn shrink(&self, _: Self::Output) -> Shrunk<'_, Self::Output> { ... }
    fn try_convert<T, I, F>(
        self,
        from: F,
        try_into: I
    ) -> TryConvert<Self, F, I>
       where Self: Sized,
             F: Fn(Self::Output) -> T,
             I: Fn(T) -> Option<Self::Output> { ... }
    fn zip<OS>(self, other: OS) -> Zip<Self, OS>
       where Self: Sized,
             OS: Sample { ... }
    fn chain_resample<F, RS>(
        self,
        transform: F,
        subsamples: usize
    ) -> ChainResample<Self, F>
       where Self: Sized,
             F: Fn(Self::Output) -> RS,
             RS: Sample { ... }
}
Expand description

User-defined strategies for generating and shrinking an Output type.

Required Associated Types§

Required Methods§

source

fn generate(&mut self, g: &mut Random) -> Self::Output

Randomly generate the requested type.

Provided Methods§

source

fn shrink(&self, _: Self::Output) -> Shrunk<'_, Self::Output>

Shrink the given value into a “smaller” value. Defaults to an empty iterator (which represents that the value cannot be shrunk).

source

fn try_convert<T, I, F>(self, from: F, try_into: I) -> TryConvert<Self, F, I>
where Self: Sized, F: Fn(Self::Output) -> T, I: Fn(T) -> Option<Self::Output>,

Convert this sampler into a new sampler with from and try_into functions:

use sample_std::{Sample, VecSampler};

struct Wrapper {
    vec: Vec<usize>
}

impl Wrapper {
    fn new(vec: Vec<usize>) -> Self {
        Self { vec }
    }
}

let sampler = VecSampler { length: 10..20, el: 1..5 }.try_convert(
    Wrapper::new,
    |w| Some(w.vec)
);

Sample::generate will use from to convert the inner sampled value to the desired type.

Sample::shrink will use try_into to convert the desired type back to the inner sampled type, if possible. The inner shrink method will be called on that type, and all values will be converted back to the target type again with into.

source

fn zip<OS>(self, other: OS) -> Zip<Self, OS>
where Self: Sized, OS: Sample,

“Zip” two samplers together. Functionally equivalent to (self, other).

source

fn chain_resample<F, RS>( self, transform: F, subsamples: usize ) -> ChainResample<Self, F>
where Self: Sized, F: Fn(Self::Output) -> RS, RS: Sample,

“Resampling” method for chaining samplers.

For sampling, use this sampler as a “supersampler” that creates a “seed” value. The provided function then converts this seed into an inner sampler that is used to generate a final value.

This value is returned within a Chained wrapper that also captures the seed. This allows us to use the “supersampler” in the shrinking process. This then shrinks the seed, and then “resamples” (generates new samples) with the shrunk inner sampler.

Note that the resulting sampler will only perform a very shallow search (subsamples) of the shrunk inner sampler space.

Trait Implementations§

source§

impl<T> Sample for Box<dyn Sample<Output = T>>

§

type Output = T

source§

fn generate(&mut self, g: &mut Random) -> Self::Output

Randomly generate the requested type.
source§

fn shrink(&self, v: Self::Output) -> Shrunk<'_, Self::Output>

Shrink the given value into a “smaller” value. Defaults to an empty iterator (which represents that the value cannot be shrunk).
source§

fn try_convert<T, I, F>(self, from: F, try_into: I) -> TryConvert<Self, F, I>
where Self: Sized, F: Fn(Self::Output) -> T, I: Fn(T) -> Option<Self::Output>,

Convert this sampler into a new sampler with from and try_into functions: Read more
source§

fn zip<OS>(self, other: OS) -> Zip<Self, OS>
where Self: Sized, OS: Sample,

“Zip” two samplers together. Functionally equivalent to (self, other).
source§

fn chain_resample<F, RS>( self, transform: F, subsamples: usize ) -> ChainResample<Self, F>
where Self: Sized, F: Fn(Self::Output) -> RS, RS: Sample,

“Resampling” method for chaining samplers. Read more
source§

impl<T> Sample for Box<dyn Sample<Output = T> + Send + Sync>

§

type Output = T

source§

fn generate(&mut self, g: &mut Random) -> Self::Output

Randomly generate the requested type.
source§

fn shrink(&self, v: Self::Output) -> Shrunk<'_, Self::Output>

Shrink the given value into a “smaller” value. Defaults to an empty iterator (which represents that the value cannot be shrunk).
source§

fn try_convert<T, I, F>(self, from: F, try_into: I) -> TryConvert<Self, F, I>
where Self: Sized, F: Fn(Self::Output) -> T, I: Fn(T) -> Option<Self::Output>,

Convert this sampler into a new sampler with from and try_into functions: Read more
source§

fn zip<OS>(self, other: OS) -> Zip<Self, OS>
where Self: Sized, OS: Sample,

“Zip” two samplers together. Functionally equivalent to (self, other).
source§

fn chain_resample<F, RS>( self, transform: F, subsamples: usize ) -> ChainResample<Self, F>
where Self: Sized, F: Fn(Self::Output) -> RS, RS: Sample,

“Resampling” method for chaining samplers. Read more

Implementations on Foreign Types§

source§

impl<A> Sample for (A,)
where A: Sample,

§

type Output = (<A as Sample>::Output,)

source§

fn generate(&mut self, g: &mut Random) -> Self::Output

source§

fn shrink(&self, v: Self::Output) -> Shrunk<'_, Self::Output>

source§

impl<A, B> Sample for (A, B)
where A: Sample, B: Sample, A::Output: Clone, B::Output: Clone,

§

type Output = (<A as Sample>::Output, <B as Sample>::Output)

source§

fn generate(&mut self, r: &mut Random) -> Self::Output

source§

fn shrink(&self, v: Self::Output) -> Shrunk<'_, Self::Output>

source§

impl<A, B, C> Sample for (A, B, C)
where A: Sample, B: Sample, C: Sample, A::Output: Clone, B::Output: Clone, C::Output: Clone,

§

type Output = (<A as Sample>::Output, <B as Sample>::Output, <C as Sample>::Output)

source§

fn generate(&mut self, r: &mut Random) -> Self::Output

source§

fn shrink(&self, v: Self::Output) -> Shrunk<'_, Self::Output>

source§

impl<A, B, C, D> Sample for (A, B, C, D)
where A: Sample, B: Sample, C: Sample, D: Sample, A::Output: Clone, B::Output: Clone, C::Output: Clone, D::Output: Clone,

§

type Output = (<A as Sample>::Output, <B as Sample>::Output, <C as Sample>::Output, <D as Sample>::Output)

source§

fn generate(&mut self, r: &mut Random) -> Self::Output

source§

fn shrink(&self, v: Self::Output) -> Shrunk<'_, Self::Output>

source§

impl<A, B, C, D, E> Sample for (A, B, C, D, E)
where A: Sample, B: Sample, C: Sample, D: Sample, E: Sample, A::Output: Clone, B::Output: Clone, C::Output: Clone, D::Output: Clone, E::Output: Clone,

§

type Output = (<A as Sample>::Output, <B as Sample>::Output, <C as Sample>::Output, <D as Sample>::Output, <E as Sample>::Output)

source§

fn generate(&mut self, r: &mut Random) -> Self::Output

source§

fn shrink(&self, v: Self::Output) -> Shrunk<'_, Self::Output>

source§

impl<A, B, C, D, E, F> Sample for (A, B, C, D, E, F)
where A: Sample, B: Sample, C: Sample, D: Sample, E: Sample, F: Sample, A::Output: Clone, B::Output: Clone, C::Output: Clone, D::Output: Clone, E::Output: Clone, F::Output: Clone,

§

type Output = (<A as Sample>::Output, <B as Sample>::Output, <C as Sample>::Output, <D as Sample>::Output, <E as Sample>::Output, <F as Sample>::Output)

source§

fn generate(&mut self, r: &mut Random) -> Self::Output

source§

fn shrink(&self, v: Self::Output) -> Shrunk<'_, Self::Output>

source§

impl<A, B, C, D, E, F, G> Sample for (A, B, C, D, E, F, G)
where A: Sample, B: Sample, C: Sample, D: Sample, E: Sample, F: Sample, G: Sample, A::Output: Clone, B::Output: Clone, C::Output: Clone, D::Output: Clone, E::Output: Clone, F::Output: Clone, G::Output: Clone,

§

type Output = (<A as Sample>::Output, <B as Sample>::Output, <C as Sample>::Output, <D as Sample>::Output, <E as Sample>::Output, <F as Sample>::Output, <G as Sample>::Output)

source§

fn generate(&mut self, r: &mut Random) -> Self::Output

source§

fn shrink(&self, v: Self::Output) -> Shrunk<'_, Self::Output>

source§

impl<A, B, C, D, E, F, G, H> Sample for (A, B, C, D, E, F, G, H)
where A: Sample, B: Sample, C: Sample, D: Sample, E: Sample, F: Sample, G: Sample, H: Sample, A::Output: Clone, B::Output: Clone, C::Output: Clone, D::Output: Clone, E::Output: Clone, F::Output: Clone, G::Output: Clone, H::Output: Clone,

§

type Output = (<A as Sample>::Output, <B as Sample>::Output, <C as Sample>::Output, <D as Sample>::Output, <E as Sample>::Output, <F as Sample>::Output, <G as Sample>::Output, <H as Sample>::Output)

source§

fn generate(&mut self, r: &mut Random) -> Self::Output

source§

fn shrink(&self, v: Self::Output) -> Shrunk<'_, Self::Output>

source§

impl<T> Sample for Box<dyn Sample<Output = T> + Send + Sync>

§

type Output = T

source§

fn generate(&mut self, g: &mut Random) -> Self::Output

source§

fn shrink(&self, v: Self::Output) -> Shrunk<'_, Self::Output>

source§

impl<T> Sample for Box<dyn Sample<Output = T>>

§

type Output = T

source§

fn generate(&mut self, g: &mut Random) -> Self::Output

source§

fn shrink(&self, v: Self::Output) -> Shrunk<'_, Self::Output>

source§

impl<T, I> Sample for Range<T>
where T: SampleUniform + Clone + PartialOrd + 'static, Range<T>: IntoIterator<IntoIter = I>, I: DoubleEndedIterator<Item = T> + 'static,

§

type Output = T

source§

fn generate(&mut self, g: &mut Random) -> Self::Output

source§

fn shrink(&self, v: Self::Output) -> Shrunk<'_, Self::Output>

Implementors§

source§

impl Sample for Chance

§

type Output = bool

source§

impl Sample for Regex

source§

impl<A, B> Sample for Zip<A, B>
where A: Sample, B: Sample, A::Output: Clone, B::Output: Clone,

§

type Output = (<A as Sample>::Output, <B as Sample>::Output)

source§

impl<C, T> Sample for SamplerChoice<C>
where C: Sample<Output = T>, T: Clone + 'static,

§

type Output = T

source§

impl<G, N> Sample for RecursiveSampler<G>
where G: Recursion<Output = N> + Sample<Output = N> + Clone, N: 'static,

§

type Output = N

source§

impl<P, F, I, T> Sample for TryConvert<P, F, I>
where P: Sample, F: Fn(P::Output) -> T, I: Fn(T) -> Option<P::Output>,

§

type Output = T

source§

impl<S> Sample for SampleAll<S>
where S: Sample, S::Output: Clone,

§

type Output = Vec<<S as Sample>::Output>

source§

impl<S, F, SS> Sample for ChainResample<S, F>
where S: Sample, S::Output: Clone, SS: Sample + 'static, F: Fn(S::Output) -> SS,

§

type Output = Chained<<S as Sample>::Output, <SS as Sample>::Output>

source§

impl<S, I, T> Sample for VecSampler<S, I>
where S: Sample<Output = usize>, I: Sample<Output = T>, T: Clone + 'static,

§

type Output = Vec<T>

source§

impl<T> Sample for Choice<T>
where T: Clone + PartialEq,

§

type Output = T

source§

impl<T: Clone> Sample for Always<T>

§

type Output = T

source§

impl<T: Arbitrary> Sample for ArbitrarySampler<T>

§

type Output = T