Struct anymap2::Map

source ·
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 of Any.
  • 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>

source

pub fn new() -> Map<A>

Create an empty collection.

source

pub fn with_capacity(capacity: usize) -> Map<A>

Creates an empty collection with the given initial capacity.

source

pub fn capacity(&self) -> usize

Returns the number of elements the collection can hold without reallocating.

source

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.

source

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

pub fn len(&self) -> usize

Returns the number of items in the collection.

source

pub fn is_empty(&self) -> bool

Returns true if there are no items in the collection.

source

pub fn clear(&mut self)

Removes all items from the collection. Keeps the allocated memory for reuse.

source§

impl<A: ?Sized + UncheckedAnyExt> Map<A>

source

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.

source

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.

source

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.

source

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.

source

pub fn contains<T: IntoBox<A>>(&self) -> bool

Returns true if the collection contains a value of type T.

source

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

Trait Implementations§

source§

impl<A: ?Sized + UncheckedAnyExt> AsMut<RawMap<A>> for Map<A>

source§

fn as_mut(&mut self) -> &mut RawMap<A>

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl<A: ?Sized + UncheckedAnyExt> AsRef<RawMap<A>> for Map<A>

source§

fn as_ref(&self) -> &RawMap<A>

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<A: ?Sized + UncheckedAnyExt> Clone for Map<A>
where Box<A>: Clone,

source§

fn clone(&self) -> Map<A>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<A: Debug + ?Sized + UncheckedAnyExt> Debug for Map<A>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<A: ?Sized + UncheckedAnyExt> Default for Map<A>

source§

fn default() -> Map<A>

Returns the “default value” for a type. Read more
source§

impl<A: ?Sized + UncheckedAnyExt> From<Map<A>> for RawMap<A>

source§

fn from(map: Map<A>) -> Self

Converts to this type from the input type.

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>
where A: Send + ?Sized,

§

impl<A> Sync for Map<A>
where A: Sync + ?Sized,

§

impl<A> Unpin for Map<A>
where A: ?Sized,

§

impl<A> UnwindSafe for Map<A>
where A: UnwindSafe + ?Sized,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> Any for T
where T: Any,

source§

impl<T> CloneAny for T
where T: Any + Clone,

source§

impl<T> CloneAnySend for T
where T: Any + Send + Clone,

source§

impl<T> CloneAnySendSync for T
where T: Any + Send + Sync + Clone,

source§

impl<T> CloneAnySync for T
where T: Any + Sync + Clone,