Trait sample_test::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
) -> Box<dyn Iterator<Item = 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§
Provided Methods§
sourcefn shrink(&self, _: Self::Output) -> Box<dyn Iterator<Item = Self::Output> + '_>
fn shrink(&self, _: Self::Output) -> Box<dyn Iterator<Item = 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>
fn try_convert<T, I, F>(self, from: F, try_into: I) -> TryConvert<Self, F, I>
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>
fn zip<OS>(self, other: OS) -> Zip<Self, OS>
“Zip” two samplers together. Functionally equivalent to (self, other)
.
sourcefn chain_resample<F, RS>(
self,
transform: F,
subsamples: usize
) -> ChainResample<Self, F>
fn chain_resample<F, RS>( self, transform: F, subsamples: usize ) -> ChainResample<Self, F>
“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 generate(
&mut self,
g: &mut Random
) -> <Box<dyn Sample<Output = T>> as Sample>::Output
fn generate( &mut self, g: &mut Random ) -> <Box<dyn Sample<Output = T>> as Sample>::Output
source§fn shrink(
&self,
v: <Box<dyn Sample<Output = T>> as Sample>::Output
) -> Box<dyn Iterator<Item = <Box<dyn Sample<Output = T>> as Sample>::Output> + '_>
fn shrink( &self, v: <Box<dyn Sample<Output = T>> as Sample>::Output ) -> Box<dyn Iterator<Item = <Box<dyn Sample<Output = T>> as Sample>::Output> + '_>
source§fn try_convert<T, I, F>(self, from: F, try_into: I) -> TryConvert<Self, F, I>
fn try_convert<T, I, F>(self, from: F, try_into: I) -> TryConvert<Self, F, I>
source§fn zip<OS>(self, other: OS) -> Zip<Self, OS>
fn zip<OS>(self, other: OS) -> Zip<Self, OS>
(self, other)
.source§fn chain_resample<F, RS>(
self,
transform: F,
subsamples: usize
) -> ChainResample<Self, F>
fn chain_resample<F, RS>( self, transform: F, subsamples: usize ) -> ChainResample<Self, F>
source§impl<T> Sample for Box<dyn Sample<Output = T> + Sync + Send>
impl<T> Sample for Box<dyn Sample<Output = T> + Sync + Send>
type Output = T
source§fn generate(
&mut self,
g: &mut Random
) -> <Box<dyn Sample<Output = T> + Sync + Send> as Sample>::Output
fn generate( &mut self, g: &mut Random ) -> <Box<dyn Sample<Output = T> + Sync + Send> as Sample>::Output
source§fn shrink(
&self,
v: <Box<dyn Sample<Output = T> + Sync + Send> as Sample>::Output
) -> Box<dyn Iterator<Item = <Box<dyn Sample<Output = T> + Sync + Send> as Sample>::Output> + '_>
fn shrink( &self, v: <Box<dyn Sample<Output = T> + Sync + Send> as Sample>::Output ) -> Box<dyn Iterator<Item = <Box<dyn Sample<Output = T> + Sync + Send> as Sample>::Output> + '_>
source§fn try_convert<T, I, F>(self, from: F, try_into: I) -> TryConvert<Self, F, I>
fn try_convert<T, I, F>(self, from: F, try_into: I) -> TryConvert<Self, F, I>
source§fn zip<OS>(self, other: OS) -> Zip<Self, OS>
fn zip<OS>(self, other: OS) -> Zip<Self, OS>
(self, other)
.