Expand description
A collection containing zero or one values for any given type and allowing convenient, type-safe access to those values.
The type parameter A
allows you to use a different value type; normally you will want
it to be core::any::Any
(also known as std::any::Any
), but there are other choices:
- If you want the entire map to be cloneable, use
CloneAny
instead ofAny
; with that, you can only add types that implementClone
to the map. - You can add on
+ Send
or+ Send + Sync
(e.g.Map<dyn Any + Send>
) to add those auto traits.
Cumulatively, there are thus six forms of map:
Map<dyn core::any::Any>
, also spelledAnyMap
for convenience.Map<dyn core::any::Any + Send>
Map<dyn core::any::Any + Send + Sync>
Map<dyn CloneAny>
Map<dyn CloneAny + Send>
Map<dyn CloneAny + Send + Sync>
Example
(Here using the AnyMap
convenience alias; the first line could use
anymap::Map::<core::any::Any>::new()
instead if desired.)
let mut data = anymap::AnyMap::new();
assert_eq!(data.get(), None::<&i32>);
data.insert(42i32);
assert_eq!(data.get(), Some(&42i32));
data.remove::<i32>();
assert_eq!(data.get::<i32>(), None);
#[derive(Clone, PartialEq, Debug)]
struct Foo {
str: String,
}
assert_eq!(data.get::<Foo>(), None);
data.insert(Foo { str: format!("foo") });
assert_eq!(data.get(), Some(&Foo { str: format!("foo") }));
data.get_mut::<Foo>().map(|foo| foo.str.push('t'));
assert_eq!(&*data.get::<Foo>().unwrap().str, "foot");
Values containing non-static references are not permitted.
Implementations
sourceimpl<A: ?Sized + Downcast> Map<A>
impl<A: ?Sized + Downcast> Map<A>
sourcepub fn with_capacity(capacity: usize) -> Map<A>
pub fn with_capacity(capacity: usize) -> Map<A>
Creates an empty collection with the given initial capacity.
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the collection can hold without reallocating.
sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more elements to be inserted
in the collection. The collection may reserve more space to avoid
frequent reallocations.
Panics
Panics if the new allocation size overflows usize
.
sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the collection 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.
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes all items from the collection. Keeps the allocated memory for reuse.
sourcepub fn get<T: IntoBox<A>>(&self) -> Option<&T>
pub fn get<T: IntoBox<A>>(&self) -> Option<&T>
Returns a reference to the value stored in the collection for the type T
,
if it exists.
sourcepub fn get_mut<T: IntoBox<A>>(&mut self) -> Option<&mut T>
pub fn get_mut<T: IntoBox<A>>(&mut self) -> Option<&mut T>
Returns a mutable reference to the value stored in the collection for the type T
,
if it exists.
sourcepub fn insert<T: IntoBox<A>>(&mut self, value: T) -> Option<T>
pub fn insert<T: IntoBox<A>>(&mut self, value: T) -> Option<T>
Sets the value stored in the collection for the type T
.
If the collection already had a value of type T
, that value is returned.
Otherwise, None
is returned.
sourcepub fn remove<T: IntoBox<A>>(&mut self) -> Option<T>
pub fn remove<T: IntoBox<A>>(&mut self) -> Option<T>
Removes the T
value from the collection,
returning it if there was one or None
if there was not.
sourcepub fn contains<T: IntoBox<A>>(&self) -> bool
pub fn contains<T: IntoBox<A>>(&self) -> bool
Returns true if the collection contains a value of type T
.
sourcepub fn entry<T: IntoBox<A>>(&mut self) -> Entry<'_, A, T>
pub fn entry<T: IntoBox<A>>(&mut self) -> Entry<'_, A, T>
Gets the entry for the given type in the collection for in-place manipulation
sourcepub fn as_raw(&self) -> &RawMap<A>
pub fn as_raw(&self) -> &RawMap<A>
Get access to the raw hash map that backs this.
This will seldom be useful, but it’s conceivable that you could wish to iterate over all the items in the collection, and this lets you do that.
sourcepub unsafe fn as_raw_mut(&mut self) -> &mut RawMap<A>
pub unsafe fn as_raw_mut(&mut self) -> &mut RawMap<A>
Get mutable access to the raw hash map that backs this.
This will seldom be useful, but it’s conceivable that you could wish to iterate over all the items in the collection mutably, or drain or something, or possibly even batch insert, and this lets you do that.
Safety
If you insert any values to the raw map, the key (a TypeId
) must match the
value’s type, or undefined behaviour will occur when you access those values.
(Removing entries is perfectly safe.)
sourcepub fn into_raw(self) -> RawMap<A>
pub fn into_raw(self) -> RawMap<A>
Convert this into the raw hash map that backs this.
This will seldom be useful, but it’s conceivable that you could wish to consume all
the items in the collection and do something with some or all of them, and this
lets you do that, without the unsafe
that .as_raw_mut().drain()
would require.
sourcepub unsafe fn from_raw(raw: RawMap<A>) -> Map<A>
pub unsafe fn from_raw(raw: RawMap<A>) -> Map<A>
Construct a map from a collection of raw values.
You know what? I can’t immediately think of any legitimate use for this, especially
because of the requirement of the BuildHasherDefault<TypeIdHasher>
generic in the
map.
Perhaps this will be most practical as unsafe { Map::from_raw(iter.collect()) }
,
iter
being an iterator over (TypeId, Box<A>)
pairs. Eh, this method provides
symmetry with into_raw
, so I don’t care if literally no one ever uses it. I’m not
even going to write a test for it, it’s so trivial.
Safety
For all entries in the raw map, the key (a TypeId
) must match the value’s type,
or undefined behaviour will occur when you access that entry.
Trait Implementations
sourceimpl<A: ?Sized + Downcast> Extend<Box<A, Global>> for Map<A>
impl<A: ?Sized + Downcast> Extend<Box<A, Global>> for Map<A>
sourcefn extend<T: IntoIterator<Item = Box<A>>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = Box<A>>>(&mut self, iter: T)
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
Auto Trait Implementations
impl<A: ?Sized> RefUnwindSafe for Map<A> where
A: RefUnwindSafe,
impl<A: ?Sized> Send for Map<A> where
A: Send,
impl<A: ?Sized> Sync for Map<A> where
A: Sync,
impl<A: ?Sized> Unpin for Map<A>
impl<A: ?Sized> UnwindSafe for Map<A> where
A: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more