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>
impl<'h, T> Hand<'h, T>
Sourcepub fn new(choices: Box<[usize]>, elements: &'h [T]) -> Hand<'h, T>
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.
Sourcepub fn choose(&self, idx: usize) -> Option<&T>
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 bReturns 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.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more