pub struct Map<A: ?Sized + UncheckedAnyExt = dyn Any> { /* private fields */ }
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 anymap::any::Any
, but there are other choices:
- If you want the entire map to be cloneable, use
CloneAny
instead ofAny
. - You can add on
+ Send
and/or+ Sync
(e.g.Map<Any + Send>
) to add those bounds.
let mut data = 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§
source§impl<A: ?Sized + UncheckedAnyExt> Map<A>
impl<A: ?Sized + UncheckedAnyExt> 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.
source§impl<A: ?Sized + UncheckedAnyExt> Map<A>
impl<A: ?Sized + UncheckedAnyExt> Map<A>
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.
Trait Implementations§
Auto Trait Implementations§
impl<A> Freeze for Map<A>where
A: ?Sized,
impl<A> RefUnwindSafe for Map<A>where
A: RefUnwindSafe + ?Sized,
impl<A> Send for Map<A>
impl<A> Sync for Map<A>
impl<A> Unpin for Map<A>where
A: ?Sized,
impl<A> UnwindSafe for Map<A>where
A: UnwindSafe + ?Sized,
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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)