Function sample_std::sampler_choice

source ·
pub fn sampler_choice<C, II>(choices: II) -> SamplerChoice<C>where
    II: IntoIterator<Item = C>,
    C: Sample,
    <C as Sample>::Output: Clone,
Expand description

Sample values from a sampler randomly drawn from a list of choices.

shrink attempts to run the Sample::shrink method from each specified sampler in order. This allows sampler_choice to work with choices that generate enum variants (e.g. via Sample::try_convert):

use std::boxed::Box;
use sample_std::{Sample, sampler_choice};

#[derive(Clone)]
enum Widget {
    Bib(usize),
    Bob(usize)
}

type WidgetSampler = Box<dyn Sample<Output = Widget>>;

let bibs: WidgetSampler = Box::new((0..100).try_convert(Widget::Bib, |v| match v {
    Widget::Bib(u) => Some(u),
    _ => None,
}));

let bobs: WidgetSampler = Box::new((100..200).try_convert(Widget::Bob, |v| match v {
    Widget::Bob(u) => Some(u),
    _ => None,
}));

let widgets = sampler_choice([bibs, bobs]);

This may lead to unexpected shrinking behavior if every sampler in the SamplerChoice can shrink a given value.