Struct coatcheck::CoatCheck
[-]
[src]
pub struct CoatCheck<V> { // some fields omitted }
A data structure storing values indexed by tickets.
Methods
impl<V> CoatCheck<V>
fn new() -> Self
Constructs a new, empty CoatCheck<T>
.
The coat check will not allocate until elements are pushed onto it.
Examples
use coatcheck::CoatCheck; let mut cc: CoatCheck<i32> = CoatCheck::new();
fn with_capacity(capacity: usize) -> Self
Constructs a new, empty CoatCheck<T>
with the specified capacity.
The coat check will be able to hold exactly capacity
elements without reallocating. If
capacity
is 0, the coat check will not allocate.
It is important to note that this function does not specify the length of the returned
coat check, but only the capacity. (For an explanation of the difference between length and
capacity, see the main Vec<T>
docs in the std::vec
module, 'Capacity and reallocation'.)
Examples
use coatcheck::CoatCheck; let mut cc: CoatCheck<i32> = CoatCheck::with_capacity(10); // The coat check contains no items, even though it has capacity for more assert_eq!(cc.len(), 0); // These are all done without reallocating... for i in 0i32..10 { let _ = cc.check(i); } // ...but this may make the coat check reallocate cc.check(11);
fn capacity(&self) -> usize
Returns the number of elements the coat check can hold without reallocating.
Examples
use coatcheck::CoatCheck; let cc: CoatCheck<i32> = CoatCheck::with_capacity(10); assert_eq!(cc.capacity(), 10);
fn len(&self) -> usize
The number of checked items.
Examples
use coatcheck::CoatCheck; let mut cc = CoatCheck::new(); assert_eq!(cc.len(), 0); cc.check("a"); assert_eq!(cc.len(), 1);
fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more elements to be checked into the given
CoatCheck<T>
. The collection may reserve more space to avoid frequent reallocations.
Panics
Panics if the new capacity overflows usize
.
Examples
use coatcheck::CoatCheck; let mut cc: CoatCheck<i32> = CoatCheck::new(); let t1 = cc.check(1); cc.reserve(10); assert!(cc.capacity() >= 11);
fn reserve_exact(&mut self, additional: usize)
Reserves the minimum capacity for exactly additional
more elements to be check into the
given CoatCheck<T>
. Does nothing if the capacity is already sufficient.
Note that the allocator may give the collection more space than it requests. Therefore
capacity can not be relied upon to be precisely minimal. Prefer reserve
if future
check-ins are expected.
Panics
Panics if the new capacity overflows usize
.
Examples
use coatcheck::CoatCheck; let mut cc: CoatCheck<i32> = CoatCheck::new(); let t1 = cc.check(1); cc.reserve_exact(10); assert!(cc.capacity() >= 11);
fn check(&mut self, value: V) -> Ticket
Check a value in and get a Ticket
in exchange.
This ticket cannot be copied, cloned, or forged but can be used to reference (get*
) or
claim values from this CoatCheck.
Panics if the size of the CoatCheck<V>
would overflow usize::MAX
.
fn check_all<I>(&mut self, iter: I) -> Tickets<I> where I: Iterator<Item=V>
Check all the items in an iterator and get tickets back.
Warning: If you don't take your tickets (collect them from the iterator), you're items won't be checked.
fn iter<'a>(&'a self) -> Iter<'a, V>
Iterate over the items in this CoatCheck<V>
.
fn iter_mut<'a>(&'a mut self) -> IterMut<'a, V>
Mutably iterate over the items in this CoatCheck<V>
.
fn contains_ticket(&self, ticket: &Ticket) -> bool
Check if a ticket belongs to this CoatCheck<V>
.
Returns true if the ticket belongs to this CoatCheck<V>
.
fn is_empty(&self) -> bool
Check if this CoatCheck<V>
is empty.
Returns true
if this CoatCheck<V>
is empty.
fn claim(&mut self, ticket: Ticket) -> Result<V, ClaimError>
Claim an item.
Returns Ok(value)
if the ticket belongs to this CoatCheck<V>
(eating the ticket).
Returns Err(ClaimError)
if the ticket belongs to another CoatCheck<V>
(returning the
ticket inside of the ClaimError).
fn get(&self, ticket: &Ticket) -> Result<&V, AccessError>
Get a reference to the value matching this ticket.
Returns Ok(&value)
if the ticket belongs to this CoatCheck<V>
.
Returns Err(AccessError)
if the ticket belongs to another CoatCheck<V>
.
fn get_mut(&mut self, ticket: &Ticket) -> Result<&mut V, AccessError>
Get a mutable reference to the value matching this ticket.
Returns Ok(&mut value)
if the ticket belongs to this CoatCheck<V>
.
Returns Err(AccessError)
if the ticket belongs to another CoatCheck<V>
.