Trait sample_test::Sample

source ·
pub trait Sample {
    type Output;

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

    // Provided methods
    fn shrink(
        &self,
        _: Self::Output
    ) -> Box<dyn Iterator<Item = Self::Output> + '_, Global> { ... }
    fn try_convert<T, I, F>(
        self,
        from: F,
        try_into: I
    ) -> TryConvert<Self, F, I>
       where Self: Sized,
             F: Fn(Self::Output) -> T + Copy,
             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(&self, g: &mut Random) -> Self::Output

Randomly generate the requested type.

Provided Methods§

source

fn shrink( &self, _: Self::Output ) -> Box<dyn Iterator<Item = Self::Output> + '_, Global>

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 + Copy, 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> + Send + Sync, Global>

§

type Output = T

source§

fn generate( &self, g: &mut Random ) -> <Box<dyn Sample<Output = T> + Send + Sync, Global> as Sample>::Output

Randomly generate the requested type.
source§

fn shrink( &self, v: <Box<dyn Sample<Output = T> + Send + Sync, Global> as Sample>::Output ) -> Box<dyn Iterator<Item = <Box<dyn Sample<Output = T> + Send + Sync, Global> as Sample>::Output> + '_, Global>

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 + Copy, 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>, Global>

§

type Output = T

source§

fn generate( &self, g: &mut Random ) -> <Box<dyn Sample<Output = T>, Global> as Sample>::Output

Randomly generate the requested type.
source§

fn shrink( &self, v: <Box<dyn Sample<Output = T>, Global> as Sample>::Output ) -> Box<dyn Iterator<Item = <Box<dyn Sample<Output = T>, Global> as Sample>::Output> + '_, Global>

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 + Copy, 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, 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 as Sample>::Output: Clone, <B as Sample>::Output: Clone, <C as Sample>::Output: Clone, <D as Sample>::Output: Clone, <E as Sample>::Output: Clone, <F as Sample>::Output: Clone, <G as Sample>::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(&self, r: &mut Random) -> <(A, B, C, D, E, F, G) as Sample>::Output

source§

fn shrink( &self, v: <(A, B, C, D, E, F, G) as Sample>::Output ) -> Box<dyn Iterator<Item = <(A, B, C, D, E, F, G) as Sample>::Output> + '_, Global>

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 as Sample>::Output: Clone, <B as Sample>::Output: Clone, <C as Sample>::Output: Clone, <D as Sample>::Output: Clone, <E as Sample>::Output: Clone, <F as Sample>::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(&self, r: &mut Random) -> <(A, B, C, D, E, F) as Sample>::Output

source§

fn shrink( &self, v: <(A, B, C, D, E, F) as Sample>::Output ) -> Box<dyn Iterator<Item = <(A, B, C, D, E, F) as Sample>::Output> + '_, Global>

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 as Sample>::Output: Clone, <B as Sample>::Output: Clone, <C as Sample>::Output: Clone, <D as Sample>::Output: Clone, <E as Sample>::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(&self, r: &mut Random) -> <(A, B, C, D, E) as Sample>::Output

source§

fn shrink( &self, v: <(A, B, C, D, E) as Sample>::Output ) -> Box<dyn Iterator<Item = <(A, B, C, D, E) as Sample>::Output> + '_, Global>

source§

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

§

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

source§

fn generate(&self, r: &mut Random) -> <(A, B, C) as Sample>::Output

source§

fn shrink( &self, v: <(A, B, C) as Sample>::Output ) -> Box<dyn Iterator<Item = <(A, B, C) as Sample>::Output> + '_, Global>

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 as Sample>::Output: Clone, <B as Sample>::Output: Clone, <C as Sample>::Output: Clone, <D as Sample>::Output: Clone, <E as Sample>::Output: Clone, <F as Sample>::Output: Clone, <G as Sample>::Output: Clone, <H as Sample>::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( &self, r: &mut Random ) -> <(A, B, C, D, E, F, G, H) as Sample>::Output

source§

fn shrink( &self, v: <(A, B, C, D, E, F, G, H) as Sample>::Output ) -> Box<dyn Iterator<Item = <(A, B, C, D, E, F, G, H) as Sample>::Output> + '_, Global>

source§

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

§

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

source§

fn generate(&self, g: &mut Random) -> <(A,) as Sample>::Output

source§

fn shrink( &self, v: <(A,) as Sample>::Output ) -> Box<dyn Iterator<Item = <(A,) as Sample>::Output> + '_, Global>

source§

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

§

type Output = T

source§

fn generate( &self, g: &mut Random ) -> <Box<dyn Sample<Output = T> + Send + Sync, Global> as Sample>::Output

source§

fn shrink( &self, v: <Box<dyn Sample<Output = T> + Send + Sync, Global> as Sample>::Output ) -> Box<dyn Iterator<Item = <Box<dyn Sample<Output = T> + Send + Sync, Global> as Sample>::Output> + '_, Global>

source§

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

§

type Output = T

source§

fn generate( &self, g: &mut Random ) -> <Box<dyn Sample<Output = T>, Global> as Sample>::Output

source§

fn shrink( &self, v: <Box<dyn Sample<Output = T>, Global> as Sample>::Output ) -> Box<dyn Iterator<Item = <Box<dyn Sample<Output = T>, Global> as Sample>::Output> + '_, Global>

source§

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

§

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

source§

fn generate(&self, r: &mut Random) -> <(A, B) as Sample>::Output

source§

fn shrink( &self, v: <(A, B) as Sample>::Output ) -> Box<dyn Iterator<Item = <(A, B) as Sample>::Output> + '_, Global>

source§

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

§

type Output = T

source§

fn generate(&self, g: &mut Random) -> <Range<T> as Sample>::Output

source§

fn shrink( &self, v: <Range<T> as Sample>::Output ) -> Box<dyn Iterator<Item = <Range<T> as Sample>::Output> + '_, Global>

source§

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

§

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

source§

fn generate(&self, r: &mut Random) -> <(A, B, C, D) as Sample>::Output

source§

fn shrink( &self, v: <(A, B, C, D) as Sample>::Output ) -> Box<dyn Iterator<Item = <(A, B, C, D) as Sample>::Output> + '_, Global>

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 as Sample>::Output: Clone, <B as Sample>::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 as Sample>::Output) -> T + Copy, I: Fn(T) -> Option<<P as Sample>::Output>,

§

type Output = T

source§

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

§

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

source§

impl<S, F, SS> Sample for ChainResample<S, F>where S: Sample, <S as Sample>::Output: Clone, SS: Sample + 'static, F: Fn(<S as Sample>::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, Global>

source§

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

§

type Output = T

source§

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

§

type Output = T

source§

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

§

type Output = T