[−][src]Struct hetero_container::HeteroContainer
A hetero container implemented with hashmap.
The hetero container can store any type that implements Any
in one container.
use hetero_container::HeteroContainer; use std::any::TypeId; let mut container = HeteroContainer::new(); // Insert difference types. container.insert(10); container.insert("foo"); assert_eq!(container.get::<i32>(), Some(&10)); assert_eq!(container.get::<&str>(), Some(&"foo")); // Check for the specific one. if !container.contains::<String>() { println!("We got {} values, but String not found.", container.len()); } for (type_id, any) in &container { if any.is::<i32>() { println!("i32: {}", any.downcast_ref::<i32>().unwrap()) } }
HeteroContainer
also implements an Entry API
, which allows
for more complex methods of getting, setting, updating and removing keys and
their values:
use hetero_container::HeteroContainer; let mut container = HeteroContainer::new(); container.entry() .or_insert("tom".to_string()); container.entry() .or_insert_with(|| 128); container.entry::<String>() .and_modify(|mut x| x.push_str(" watson"));
If you need to insert multiple values of the same type, please use HashSet
with type.
use hetero_container::HeteroContainer; use std::collections::HashSet; let mut container = HeteroContainer::new(); // Insert multiple strings let mut strings = container.entry::<HashSet<String>>() .or_insert(HashSet::new()); strings.insert("a".to_string()); strings.insert("b".to_string()); assert_eq!(container.get::<HashSet<String>>().map(|x| x.len()), Some(2));
Implementations
impl HeteroContainer
[src]
pub fn new() -> Self
[src]
Creates an empty HeteroContainer
.
The hetero container is initially created with a capacity of 0, so it will not allocate until it is first inserted into.
Examples
use hetero_container::HeteroContainer; let mut container: HeteroContainer = HeteroContainer::new();
pub fn with_capacity(capacity: usize) -> Self
[src]
Creates an empty HeteroContainer
with the specified capacity.
The hetero container will be able to hold at least capacity
elements without
reallocating. If capacity
is 0, the hetero container will not be allocate.
Examples
use hetero_container::HeteroContainer; let mut container: HeteroContainer = HeteroContainer::with_capacity(10);
pub fn capacity(&self) -> usize
[src]
Returns the number of elements the container can hold without reallocating.
This number is a lower bound; the HeteroContainer
might be able to hold
more, but is guaranteed to be able to hold at least this many.
Examples
use hetero_container::HeteroContainer; let container: HeteroContainer = HeteroContainer::with_capacity(100); assert!(container.capacity() >= 100);
pub fn len(&self) -> usize
[src]
Returns the number of elements in the container.
Examples
use hetero_container::HeteroContainer; let mut container = HeteroContainer::new(); assert_eq!(container.len(), 0); container.insert("a"); assert_eq!(container.len(), 1);
pub fn insert<V: Any>(&mut self, v: V) -> Option<V>
[src]
Inserts a value with type into the container.
If the container did not have this value type present, None
is returned.
if the container did not have this value type presents, the value is updated, and the old value is returned.
Examples
use hetero_container::HeteroContainer; let mut container = HeteroContainer::new(); assert_eq!(container.insert(37), None); assert_eq!(container.is_empty(), false); container.insert(42); assert_eq!(container.insert(56), Some(42)); assert_eq!(container.get::<i32>(), Some(&56))
pub fn get<V: Any>(&self) -> Option<&V>
[src]
Returns a reference to the value corresponding to the type.
The type must implements Any
.
Examples
use hetero_container::HeteroContainer; let mut container = HeteroContainer::new(); container.insert(10); assert_eq!(container.get::<i32>(), Some(&10)); assert_eq!(container.get::<String>(), None);
pub fn get_mut<V: Any>(&mut self) -> Option<&mut V>
[src]
Returns a mutable reference to the value corresponding to the type.
The type must be implements Any
.
Examples
use hetero_container::HeteroContainer; let mut container = HeteroContainer::new(); container.insert("a"); if let Some(x) = container.get_mut::<&str>() { *x = "b" } assert_eq!(container.get::<&str>(), Some(&"b"));
pub fn remove<V: Any>(&mut self) -> Option<V>
[src]
Removes a value from the container, returning the value at the type if the type was previously in the container.
The type must implements Any
.
Examples
use hetero_container::HeteroContainer; let mut container = HeteroContainer::new(); container.insert(10); assert_eq!(container.remove::<i32>(), Some(10)); assert_eq!(container.remove::<i32>(), None);
pub fn entry<V: Any>(&mut self) -> Entry<'_, V>
[src]
Gets the given type entry in the container for in-place manipulation.
Examples
use hetero_container::HeteroContainer; let mut container = HeteroContainer::new(); let v = container.entry() .or_insert(0); *v *= 10;
pub fn contains<T: Any>(&self) -> bool
[src]
Returns true
if the container contains a value for the specified type.
pub fn is_empty(&self) -> bool
[src]
Returns true
if the container contains no elements.
Examples
use hetero_container::HeteroContainer; let mut a = HeteroContainer::new(); assert!(a.is_empty()); a.insert("a"); assert!(!a.is_empty());
pub fn iter(&self) -> Iter<'_>ⓘ
[src]
An iterator visiting all TypeId-Box(&'a TypeId, &'a Box<dyn Any>)
.
Examples
use hetero_container::HeteroContainer; let mut container = HeteroContainer::new(); container.insert(10); container.insert("foo"); container.insert(1.0); for (type_id, value) in container.iter() { println!("type_id: {:?} value: {:?}", type_id, value) }
pub fn iter_mut(&mut self) -> IterMut<'_>ⓘ
[src]
An iterator visiting all TypeId-Box(&'a TypeId, &'a mut Box<dyn Any>)
.
Examples
use hetero_container::HeteroContainer; let mut container = HeteroContainer::new(); container.insert("a"); container.insert(1); container.insert(2.0); for (_, val) in container.iter_mut() { // I cannot find a good example to do here :( }
pub fn shrink_to_fit(&mut self)
[src]
Shrinks the capacity of the container as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.
Examples
use hetero_container::HeteroContainer; let mut container: HeteroContainer = HeteroContainer::with_capacity(100); container.insert(1); container.insert("a"); assert!(container.capacity() >= 100); container.shrink_to_fit(); assert!(container.capacity() >= 2);
Trait Implementations
impl Debug for HeteroContainer
[src]
impl<'a> IntoIterator for &'a HeteroContainer
[src]
An owning iterator over
type Item = (&'a TypeId, &'a Box<dyn Any>)
The type of the elements being iterated over.
type IntoIter = Iter<'a>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
impl<'a> IntoIterator for &'a mut HeteroContainer
[src]
Auto Trait Implementations
impl !RefUnwindSafe for HeteroContainer
impl !Send for HeteroContainer
impl !Sync for HeteroContainer
impl Unpin for HeteroContainer
impl !UnwindSafe for HeteroContainer
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,