Struct SyncTypeMap

Source
pub struct SyncTypeMap(/* private fields */);
Expand description

Like TypeMap but with a Send + Sync bound.

Implementations§

Source§

impl SyncTypeMap

Source

pub fn new() -> Self

Creates an empty SyncTypeMap.

The map is initially created with a capacity of 0, so it will not allocate until it is first inserted into.

§Examples
use static_type_map::SyncTypeMap;

let type_map = SyncTypeMap::new();
Source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty SyncTypeMap with the specified capacity.

The map will be able to hold at least capacity types without reallocating. If capacity is 0, the static type map will not allocate.

§Examples
use static_type_map::SyncTypeMap;

let type_map = SyncTypeMap::with_capacity(10);
Source

pub fn capacity(&self) -> usize

Returns the number of types the map can hold without reallocating.

This number is a lower bound; the map might be able to hold more, but it is guaranteed to be able to hold at least so many.

§Examples
use static_type_map::SyncTypeMap;

let type_map = SyncTypeMap::with_capacity(100);
assert!(type_map.capacity() >= 100);
Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no instances of any type.

§Examples
use static_type_map::SyncTypeMap;

let type_map = SyncTypeMap::new();
assert!(type_map.is_empty());
Source

pub fn len(&self) -> usize

Returns the number of types in the map.

§Examples
use static_type_map::SyncTypeMap;

let mut type_map = SyncTypeMap::new();
assert_eq!(type_map.len(), 0);
type_map.insert("a");
assert_eq!(type_map.len(), 1);
Source

pub fn clear(&mut self)

Clears the map. Keep allocated memory for reuse.

§Examples
use static_type_map::SyncTypeMap;

let mut type_map = SyncTypeMap::new();
type_map.insert("a");
type_map.clear();
assert!(type_map.is_empty());
Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more types to be inserted in the map. The collection may reserve more space to avoid frequent reallocations.

§Panics

Panics if the new allocation size overflows usize.

§Examples
use static_type_map::SyncTypeMap;

let mut type_map = SyncTypeMap::new();
assert_eq!(type_map.capacity(), 0);
type_map.reserve(10);
assert!(type_map.capacity() >= 10);
Source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the map as much as possible. It will drop down as much as possible while mainting the internal rules and possibly leaving some space in accordance with the resize policy.

§Examples
use static_type_map::SyncTypeMap;

let mut type_map = SyncTypeMap::with_capacity(100);
assert!(type_map.capacity() >= 0);
type_map.insert("a");
type_map.insert(true);
assert!(type_map.capacity() >= 2);
Source

pub fn contains<T>(&self) -> bool
where T: Any,

Returns true if the map contains an instance of T.

§Examples
use static_type_map::SyncTypeMap;

let mut type_map = SyncTypeMap::new();
type_map.insert("a");
assert!(type_map.contains::<&str>());
Source

pub fn get<T>(&self) -> Option<&T>
where T: Any + Send + Sync,

Returns a reference to an instance of T.

If the map does not have an instance of T, None is returned.

§Examples
use static_type_map::SyncTypeMap;

let mut type_map = SyncTypeMap::new();
type_map.insert("a");
assert_eq!(type_map.get::<&str>(), Some(&"a"));
assert_eq!(type_map.get::<bool>(), None);
Source

pub fn get_mut<T>(&mut self) -> Option<&mut T>
where T: Any + Send + Sync,

Returns a mutable reference to an instance of T.

If the map does not have an instance of T, None is returned.

§Examples
use static_type_map::SyncTypeMap;

let mut type_map = SyncTypeMap::new();
type_map.insert("a");
if let Some(x) = type_map.get_mut::<&str>() {
    *x = "b";
}
assert_eq!(type_map.get::<&str>(), Some(&"b"));
Source

pub fn insert<T>(&mut self, value: T) -> Option<T>
where T: Any + Send + Sync,

Insert an instance of type T into the map.

If the map did not have this type present, None is returned.

§Examples
use static_type_map::SyncTypeMap;

let mut type_map = SyncTypeMap::new();
assert_eq!(type_map.insert("a"), None);
assert_eq!(type_map.insert("b"), Some("a"));
Source

pub fn remove<T>(&mut self) -> Option<T>
where T: Any + Send + Sync,

Remove and return an instance of type T from the map.

If the map did not have this type present, None is returned.

§Examples
use static_type_map::SyncTypeMap;

let mut type_map = SyncTypeMap::new();
type_map.insert("a");
assert_eq!(type_map.remove::<&str>(), Some("a"));

Trait Implementations§

Source§

impl Debug for SyncTypeMap

Source§

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

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

impl Default for SyncTypeMap

Source§

fn default() -> SyncTypeMap

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

Auto Trait Implementations§

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