Trait sample_std::Sample
source · pub trait Sample {
type Output;
// Required method
fn generate(&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 + 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§
Provided Methods§
sourcefn shrink(&self, _: Self::Output) -> Shrunk<'_, Self::Output>
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).
sourcefn 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 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
.
sourcefn zip<OS>(self, other: OS) -> Zip<Self, OS>where
Self: Sized,
OS: Sample,
fn zip<OS>(self, other: OS) -> Zip<Self, OS>where Self: Sized, OS: Sample,
“Zip” two samplers together. Functionally equivalent to (self, other)
.
sourcefn chain_resample<F, RS>(
self,
transform: F,
subsamples: usize
) -> ChainResample<Self, F>where
Self: Sized,
F: Fn(Self::Output) -> RS,
RS: Sample,
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>>
impl<T> Sample for Box<dyn Sample<Output = T>>
type Output = T
source§fn shrink(&self, v: Self::Output) -> Shrunk<'_, Self::Output>
fn shrink(&self, v: Self::Output) -> Shrunk<'_, Self::Output>
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>,
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>,
source§fn zip<OS>(self, other: OS) -> Zip<Self, OS>where
Self: Sized,
OS: Sample,
fn zip<OS>(self, other: OS) -> Zip<Self, OS>where Self: Sized, OS: Sample,
(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,
fn chain_resample<F, RS>( self, transform: F, subsamples: usize ) -> ChainResample<Self, F>where Self: Sized, F: Fn(Self::Output) -> RS, RS: Sample,
source§impl<T> Sample for Box<dyn Sample<Output = T> + Send + Sync>
impl<T> Sample for Box<dyn Sample<Output = T> + Send + Sync>
type Output = T
source§fn shrink(&self, v: Self::Output) -> Shrunk<'_, Self::Output>
fn shrink(&self, v: Self::Output) -> Shrunk<'_, Self::Output>
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>,
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>,
source§fn zip<OS>(self, other: OS) -> Zip<Self, OS>where
Self: Sized,
OS: Sample,
fn zip<OS>(self, other: OS) -> Zip<Self, OS>where Self: Sized, OS: Sample,
(self, other)
.