Struct anymap2::Map [−][src]
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
impl<A: ?Sized + UncheckedAnyExt> Map<A>
[src]
pub fn new() -> Map<A>
[src]
Create an empty collection.
pub fn with_capacity(capacity: usize) -> Map<A>
[src]
Creates an empty collection with the given initial capacity.
pub fn capacity(&self) -> usize
[src]
Returns the number of elements the collection can hold without reallocating.
pub fn reserve(&mut self, additional: usize)
[src]
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
.
pub fn shrink_to_fit(&mut self)
[src]
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.
pub fn len(&self) -> usize
[src]
Returns the number of items in the collection.
pub fn is_empty(&self) -> bool
[src]
Returns true if there are no items in the collection.
pub fn clear(&mut self)
[src]
Removes all items from the collection. Keeps the allocated memory for reuse.
impl<A: ?Sized + UncheckedAnyExt> Map<A>
[src]
pub fn get<T: IntoBox<A>>(&self) -> Option<&T>
[src]
Returns a reference to the value stored in the collection for the type
T
, if it exists.
pub fn get_mut<T: IntoBox<A>>(&mut self) -> Option<&mut T>
[src]
Returns a mutable reference to the value stored in the collection for
the type T
, if it exists.
pub fn insert<T: IntoBox<A>>(&mut self, value: T) -> Option<T>
[src]
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.
pub fn remove<T: IntoBox<A>>(&mut self) -> Option<T>
[src]
Removes the T
value from the collection,
returning it if there was one or None
if there was not.
pub fn contains<T: IntoBox<A>>(&self) -> bool
[src]
Returns true if the collection contains a value of type T
.
pub fn entry<T: IntoBox<A>>(&mut self) -> Entry<'_, A, T>
[src]
Gets the entry for the given type in the collection for in-place manipulation
Trait Implementations
impl<A: ?Sized + UncheckedAnyExt> AsMut<RawMap<A>> for Map<A>
[src]
impl<A: ?Sized + UncheckedAnyExt> AsRef<RawMap<A>> for Map<A>
[src]
impl<A: ?Sized + UncheckedAnyExt> Clone for Map<A> where
Box<A>: Clone,
[src]
Box<A>: Clone,
impl<A: Debug + ?Sized + UncheckedAnyExt> Debug for Map<A>
[src]
impl<A: ?Sized + UncheckedAnyExt> Default for Map<A>
[src]
impl<A: ?Sized + UncheckedAnyExt> From<Map<A>> for RawMap<A>
[src]
Auto Trait Implementations
impl<A: ?Sized> RefUnwindSafe for Map<A> where
A: RefUnwindSafe,
A: RefUnwindSafe,
impl<A: ?Sized> Send for Map<A> where
A: Send,
A: Send,
impl<A: ?Sized> Sync for Map<A> where
A: Sync,
A: Sync,
impl<A: ?Sized> Unpin for Map<A>
impl<A: ?Sized> UnwindSafe for Map<A> where
A: UnwindSafe,
A: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: Any,
[src]
T: Any,
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,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> CloneAny for T where
T: Any + Clone,
[src]
T: Any + Clone,
impl<T> CloneAnySend for T where
T: Any + Send + Clone,
[src]
T: Any + Send + Clone,
impl<T> CloneAnySendSync for T where
T: Any + Send + Sync + Clone,
[src]
T: Any + Send + Sync + Clone,
impl<T> CloneAnySync for T where
T: Any + Sync + Clone,
[src]
T: Any + Sync + Clone,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
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.
pub 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>,