[][src]Struct handy::typed::TypedHandleMap

pub struct TypedHandleMap<T>(_);

A TypedHandleMap is a wrapper around a HandleMap which gives you some additional type safety, should you desire.

It accepts and returns TypedHandles, and you can only pass a TypedHandle<T> to a TypedHandleMap<T> -- attempting to pass it to a TypedHandleMap<U> will be statically detected.

Beyond this, it still can detect use of a handle that came from another map.

You use it with TypedHandles, which only will accept handles of the correct type. This could be useful if you have several handle maps in your program, and find

TypedHandle<T> is Copy + Send + Sync (and several others) regardless of T, which is not true for a naïve implementation of this, so it's provided even though I don't think it's that helpful for most usage (handle maps already detect this at runtime).

Methods

impl<T> TypedHandleMap<T>[src]

pub fn new() -> Self[src]

Create a new typed handle map.

Example

let m: TypedHandleMap<u32> = TypedHandleMap::new();
// No allocation is performed by default.
assert_eq!(m.capacity(), 0);

pub fn from_untyped(h: HandleMap<T>) -> Self[src]

Create a typed handle map from one which accepts untyped handles.

Example

let mut m: HandleMap<u32> = HandleMap::new();
let h = m.insert(10u32);
let tm = TypedHandleMap::from_untyped(m);
assert_eq!(tm[TypedHandle::from_handle(h)], 10u32);

pub fn into_untyped(self) -> HandleMap<T>[src]

Convert this map into it's wrapped HandleMap. See also TypedHandleMap::as_untyped_map and TypedHandleMap::as_mut_untyped_map.

Example

let mut tm: TypedHandleMap<u32> = TypedHandleMap::new();
let th = tm.insert(10u32);
let m = tm.into_untyped();
assert_eq!(m[th.handle()], 10);

pub fn with_capacity(c: usize) -> Self[src]

Create a new typed handle map with the specified capacity.

Example

let m: TypedHandleMap<u32> = TypedHandleMap::with_capacity(10);
// Note that we don't guarantee the capacity will be exact.
// (though in practice it will so long as the requested
// capacity is >= 8)
assert!(m.capacity() >= 10);

pub fn capacity(&self) -> usize[src]

Get the number of entries we can hold before reallocation.

This just calls HandleMap::capacity on our wrapped map.

Example

let mut m: TypedHandleMap<u32> = TypedHandleMap::new();
m.insert(10);
assert!(m.capacity() >= 1);

pub fn len(&self) -> usize[src]

Get the number of occupied entries.

This just calls HandleMap::len on our wrapped map.

Example

let mut m: TypedHandleMap<u32> = TypedHandleMap::new();
assert_eq!(m.len(), 0);
m.insert(10u32);
assert_eq!(m.len(), 1);

pub fn is_empty(&self) -> bool[src]

Returns true if our length is zero.

This just calls HandleMap::is_empty on our wrapped map.

Example

let mut m: TypedHandleMap<u32> = TypedHandleMap::new();
assert!(m.is_empty());

pub fn as_untyped_map(&self) -> &HandleMap<T>[src]

Get a reference to this map as a HandleMap.

This shouldn't be necessary except in advanced use, but may allow access to APIs that aren't mirrored, like most of the raw APIs.

Example

let mut tm: TypedHandleMap<u32> = TypedHandleMap::new();
let th = tm.insert(10u32);
assert_eq!(tm.as_untyped_map()[th.handle()], 10);

pub fn as_mut_untyped_map(&mut self) -> &mut HandleMap<T>[src]

Get a mutable reference to this map as a HandleMap.

This shouldn't be necessary except in advanced use, but may allow access to APIs that aren't mirrored, like most of the raw APIs.

Example

let mut tm = TypedHandleMap::new();
let th = tm.insert(10u32);
tm.as_mut_untyped_map()[th.handle()] = 5;
assert_eq!(tm[th], 5);

pub fn insert(&mut self, value: T) -> TypedHandle<T>[src]

Add a new item, returning a handle to it.

This just calls HandleMap::insert on our wrapped map.

Example

let mut m = TypedHandleMap::new();
assert_eq!(m.len(), 0);
m.insert(10u32);
assert_eq!(m.len(), 1);

pub fn remove(&mut self, handle: TypedHandle<T>) -> Option<T>[src]

Remove the value referenced by this handle from the map, returning it.

If the handle doesn't point to an entry in the map we return None. This will happen if:

  • The handle comes from a different map.
  • The item it referenced has been removed already.
  • It appears corrupt in some other way.

This just calls HandleMap::remove on our wrapped map.

Example

let mut m: TypedHandleMap<u32> = TypedHandleMap::new();
let h = m.insert(10u32);
// Present:
assert_eq!(m.remove(h), Some(10));
// Not present:
assert_eq!(m.remove(h), None);

pub fn clear(&mut self)[src]

Remove all entries in this handle map.

This just calls HandleMap::clear on our wrapped map.

Example

let mut m: TypedHandleMap<u32> = TypedHandleMap::new();
let h = m.insert(10u32);
m.clear();
assert_eq!(m.len(), 0);
assert_eq!(m.get(h), None);

pub fn get(&self, handle: TypedHandle<T>) -> Option<&T>[src]

Try and get a reference to the item backed by the handle.

If the handle doesn't point to an entry in the map we return None. This will happen if:

  • The handle comes from a different map.
  • The item it referenced has been removed already.
  • It appears corrupt in some other way.

This just calls HandleMap::get on our wrapped map.

Example

let mut m: TypedHandleMap<u32> = TypedHandleMap::new();
let h = m.insert(10u32);
assert_eq!(m.get(h), Some(&10));
m.remove(h);
assert_eq!(m.get(h), None);

pub fn get_mut(&mut self, handle: TypedHandle<T>) -> Option<&mut T>[src]

Try and get mutable a reference to the item backed by the handle.

If the handle doesn't point to an entry in the map we return None. This will happen if:

  • The handle comes from a different map.
  • The item it referenced has been removed already.
  • It appears corrupt in some other way.

This just calls HandleMap::get_mut on our wrapped map.

Example

let mut m: TypedHandleMap<u32> = TypedHandleMap::new();
let h = m.insert(10u32);
*m.get_mut(h).unwrap() += 1;
assert_eq!(m[h], 11);
// Note: The following is equivalent if you're going to `unwrap` the result of get_mut:
m[h] += 1;
assert_eq!(m[h], 12);

pub fn contains(&self, h: TypedHandle<T>) -> bool[src]

Returns true if the handle refers to an item present in this map.

This just calls HandleMap::contains on our wrapped map.

Example

let mut m: TypedHandleMap<u32> = TypedHandleMap::new();
let h = m.insert(10u32);
assert!(m.contains(h));
m.remove(h);
assert!(!m.contains(h));

pub fn contains_key(&self, h: TypedHandle<T>) -> bool[src]

Returns true if the handle refers to an item present in this map.

This just calls HandleMap::contains_key on our wrapped map.

Example

let mut m: TypedHandleMap<u32> = TypedHandleMap::new();
let h = m.insert(10u32);
assert!(m.contains_key(h));
m.remove(h);
assert!(!m.contains_key(h));

pub fn find_handle(&self, item: &T) -> Option<TypedHandle<T>> where
    T: PartialEq
[src]

Search the map for item, and if it's found, return a handle to it.

If more than one value compare as equal to item, it's not specified which we will return.

Note that this is a naive O(n) search, so if you want this often, you might want to store the handle as a field on the value.

This just calls HandleMap::find_handle on our wrapped map and wraps the resulting handle.

Example

let mut m: TypedHandleMap<u32> = TypedHandleMap::new();
let h = m.insert(10u32);
assert_eq!(m.find_handle(&10), Some(h));
assert_eq!(m.find_handle(&11), None);

pub fn reserve(&mut self, sz: usize)[src]

Reserve space for sz additional items.

This just calls HandleMap::reserve on our wrapped map.

Example

let mut m: TypedHandleMap<u32> = TypedHandleMap::new();
assert_eq!(m.capacity(), 0);
m.reserve(10);
assert!(m.capacity() >= 10);

pub fn iter<'a>(&'a self) -> impl Iterator<Item = &'a T> + 'a[src]

Get an iterator over every occupied slot of this map.

See also iter_with_handles if you want the handles during iteration.

This just calls HandleMap::iter on our wrapped map.

Example

let mut m: TypedHandleMap<u32> = TypedHandleMap::new();
m.insert(10u32);
assert_eq!(*m.iter().next().unwrap(), 10);

pub fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut T> + 'a[src]

Get a mut iterator over every occupied slot of this map.

See also iter_mut_with_handles if you want the handles during iteration.

This just calls HandleMap::iter_mut on our wrapped map.

Example

let mut m: TypedHandleMap<u32> = TypedHandleMap::new();
let h = m.insert(10u32);
for v in m.iter_mut() {
    *v += 1;
}
assert_eq!(m[h], 11);

pub fn iter_with_handles<'a>(
    &'a self
) -> impl Iterator<Item = (TypedHandle<T>, &'a T)> + 'a
[src]

Get an iterator over every occupied slot of this map, as well as a handle which can be used to fetch them later.

This just calls HandleMap::iter_with_handles on our wrapped map and wraps the resulting handles.

Example

for (h, v) in m.iter_with_handles() {
    println!("{:?} => {}", h, v);
}

pub fn iter_mut_with_handles<'a>(
    &'a mut self
) -> impl Iterator<Item = (TypedHandle<T>, &'a mut T)> + 'a
[src]

Get a mut iterator over every occupied slot of this map, as well as a handle which can be used to fetch them later.

This just calls HandleMap::iter_mut_with_handles on our wrapped map and wraps the resulting handles

Example

for (h, v) in m.iter_mut_with_handles() {
    *v += 1;
    println!("{:?}", h);
}

pub fn handle_for_index(&self, index: usize) -> Option<TypedHandle<T>>[src]

If index refers to an occupied entry, return a Handle to it. Otherwise, return None.

This just calls HandleMap::handle_for_index on our wrapped map and wraps the resulting handle.

Example

let mut m: TypedHandleMap<u32> = TypedHandleMap::new();
let h = m.insert(10u32);
assert_eq!(m.handle_for_index(h.index()), Some(h));

Trait Implementations

impl<T: Debug> Debug for TypedHandleMap<T>[src]

impl<T> Index<TypedHandle<T>> for TypedHandleMap<T>[src]

type Output = T

The returned type after indexing.

impl<T> IndexMut<TypedHandle<T>> for TypedHandleMap<T>[src]

impl<T> IntoIterator for TypedHandleMap<T>[src]

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?

type Item = T

The type of the elements being iterated over.

impl<T: Clone> Clone for TypedHandleMap<T>[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<T: Default> Default for TypedHandleMap<T>[src]

Auto Trait Implementations

impl<T> Unpin for TypedHandleMap<T> where
    T: Unpin

impl<T> Sync for TypedHandleMap<T> where
    T: Sync

impl<T> Send for TypedHandleMap<T> where
    T: Send

Blanket Implementations

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.