Skip to main content

Hand

Struct Hand 

Source
pub struct Hand<'h, T>(/* private fields */);
Expand description

A lock on a slice of choices, with a slice of elements to match them.

Implementations§

Source§

impl<'h, T> Hand<'h, T>

Source

pub fn new(choices: Box<[usize]>, elements: &'h [T]) -> Hand<'h, T>

Creates a new hand from a slice of choices, and a slice of elements.

Slice length equivalence isn’t checked.

Source

pub fn choose(&self, idx: usize) -> Option<&T>

Pick from a series of choices by index, which then picks a corresponding element from the list.

This function uses the internal (frozen) slice of choices to choose an element out of the slice of elements.

With a choice slice of [2,3,1,0,4], and an element slice of [A,B,C,D,E], picking choice 1 (as an index) would use choice 4 to pick D.

1 -> [..., 4, ...] -> [..., D, ...]

use hidden::dispenser::Hand;

// Note, this is not how you'd go about getting a Hand
// Please use a Dispenser instead
let choices = Box::from([2,3,1,0,4]);
let elements = &['a','b','c','d','e'];
let hand = Hand::new(choices, elements);

assert_eq!(hand.choose(1).unwrap(), &'d'); // idx 1 -> choice 3 -> element d
assert_eq!(hand.choose(2).unwrap(), &'b'); // idx 2 -> choice 1 -> element b

Returns None if idx exceeds amount of choices, or if choice can’t correspond to an element in the list.

Returns Some with a reference to an element if the choosing succeeds.

Source

pub fn len(&self) -> usize

Returns the amount of choices that this hand has.

Trait Implementations§

Source§

impl<'h, T: Debug> Debug for Hand<'h, T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'h, T> Freeze for Hand<'h, T>

§

impl<'h, T> RefUnwindSafe for Hand<'h, T>
where T: RefUnwindSafe,

§

impl<'h, T> Send for Hand<'h, T>
where T: Sync,

§

impl<'h, T> Sync for Hand<'h, T>
where T: Sync,

§

impl<'h, T> Unpin for Hand<'h, T>

§

impl<'h, T> UnsafeUnpin for Hand<'h, T>

§

impl<'h, T> UnwindSafe for Hand<'h, T>
where T: RefUnwindSafe,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V