Selector

Struct Selector 

Source
pub struct Selector<I, T>
where I: IntoIterator<Item = T>,
{ /* private fields */ }
Expand description

Wraps a variable amount of choices and provides methods that guarantee selection from those choices.

Implementations§

Source§

impl<I, T> Selector<I, T>
where I: IntoIterator<Item = T>,

Source

pub fn with<const K: usize, C>(self, chooser: C) -> [T; K]
where C: FnOnce(Vec<Choice<'_, T>>) -> [Choice<'_, T>; K],

The function chooser is used to choose from our provided choices by returning a K-selection of it. The values of these choices are then returned by the function.

use choose_from::select_from;
let choices = vec!["Hi", "how", "are ya?"];

let chosen = select_from(choices).with(|mut choices| {
    // the provided choices allow inspection of the values
    let third = choices.pop().unwrap();
    assert_eq!(*third, "are ya?");
     
    // ignore 2nd
    choices.pop();

    let first = choices.pop().unwrap();
     
    // this is our selection
    [first, third]
});

assert_eq!(chosen, ["Hi", "are ya?"]);
Source

pub fn any_with<C>(self, chooser: C) -> Vec<T>
where C: FnOnce(Vec<Choice<'_, T>>) -> Vec<Choice<'_, T>>,

Like with, but for returning any number of chosen values. Use this when you want to ensure some values come from the choices, but the amount of chosen values returned doesn’t matter.

use choose_from::select_from;

let choices = vec!["Hi", "how", "are ya?"];

let chosen = select_from(choices).any_with(|choices| {
    choices.into_iter().step_by(2).collect()
});

assert_eq!(chosen, ["Hi", "are ya?"]);

Trait Implementations§

Source§

impl<I, T: Debug> Debug for Selector<I, T>
where I: IntoIterator<Item = T> + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<I, T: Hash> Hash for Selector<I, T>
where I: IntoIterator<Item = T> + Hash,

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<I, T: PartialEq> PartialEq for Selector<I, T>
where I: IntoIterator<Item = T> + PartialEq,

Source§

fn eq(&self, other: &Selector<I, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<I, T: Eq> Eq for Selector<I, T>
where I: IntoIterator<Item = T> + Eq,

Source§

impl<I, T> StructuralPartialEq for Selector<I, T>
where I: IntoIterator<Item = T>,

Auto Trait Implementations§

§

impl<I, T> Freeze for Selector<I, T>
where I: Freeze,

§

impl<I, T> RefUnwindSafe for Selector<I, T>
where I: RefUnwindSafe,

§

impl<I, T> Send for Selector<I, T>
where I: Send,

§

impl<I, T> Sync for Selector<I, T>
where I: Sync,

§

impl<I, T> Unpin for Selector<I, T>
where I: Unpin,

§

impl<I, T> UnwindSafe for Selector<I, T>
where I: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.