anymap2

Struct 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§

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

🔬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,

Source§

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>,

Source§

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>,

Source§

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,