SerializableAnyMap

Struct SerializableAnyMap 

Source
pub struct SerializableAnyMap { /* private fields */ }
Expand description

A serializable heterogeneous-map keyed by the stable type_name::<T>().

SerializableAnyMap behaves like anymap3 for most basic use-cases but stores values in serialized form so the entire map can be serialized and sent across process boundaries. The map implements Serialize and Deserialize where only the serialized form is persisted; the cached deserialized values are not persisted.

Inserted types must implement Serialize + Deserialize<'de> + 'static.

See method docs for usage patterns (insert, get, get_mut, entry, …).

Safety notes:

  • Some escape hatches (as_raw_mut, from_raw) are unsafe — the caller must ensure the key string matches the type stored under it.

A stored entry containing the serialized representation and an optional cached runtime value.

Implementations§

Source§

impl SerializableAnyMap

Source

pub fn new() -> Self

Create a new empty SerializableAnyMap.

Source

pub fn with_capacity(capacity: usize) -> Self

Create a new SerializableAnyMap with the specified capacity.

Source

pub fn capacity(&self) -> usize

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

Source

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

Reserves capacity for at least additional more elements to be inserted. The collection may reserve more space to avoid frequent reallocations.

§Panics

Panics if the new capacity 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

pub fn try_get<T>(&self) -> Option<&T>
where T: for<'de> Deserialize<'de> + Any + 'static,

Get an immutable reference to a cached value of type T if it exists AND was deserialized.

§Notes

This does NOT lazily deserialize, use Self::get if you want lazy deserialization. This may return None even if the type is present in the map, but not deserialized.

Source

pub fn get<T>(&mut self) -> Option<Result<&T, DeserializerError>>
where T: Serialize + for<'de> Deserialize<'de> + 'static,

Get an immutable reference to a value of type T, lazily deserializing if necessary.

§Notes

This requires a &mut self since it may need to deserialize and modify the entry.

Source

pub fn get_deserialized_copy<T>(&self) -> Option<T>
where T: for<'de> Deserialize<'de> + Any + 'static,

Gets a copy value of type T, by deserializing the value in the map.

§Warning

This always runs deserialization, and may return an instance that is not equivalent to the one in the map if any fields are marked #[serde(skip)] for example.

Source

pub fn get_mut<T>( &mut self, ) -> Option<Result<WriteGuard<'_, T>, DeserializerError>>
where T: Serialize + for<'de> Deserialize<'de> + Any + 'static,

Get a mutable reference to a value of type T, lazily deserializing into the cache if necessary.

Source

pub fn get_serialized_value<T>(&self) -> Option<&Value>
where T: 'static,

Get the serialized serde_value::Value for type T if present.

Source

pub unsafe fn insert_value_by_name( &mut self, type_name: String, value: Value, ) -> Option<Value>

Insert by type name key.

This is lower-level and useful if you already have a Value.

§Safety

The provided Value needs to match the given matches the type name.

Source

pub fn insert<T>(&mut self, value: T) -> Option<Result<T, DeserializerError>>
where T: Serialize + for<'de> Deserialize<'de> + Any + 'static,

Insert a value of type T. Returns the previous value of that type if present.

Source

pub fn try_insert<'a, T>( &'a mut self, value: T, ) -> Result<WriteGuard<'a, T>, OccupiedError<'a, T>>
where T: Serialize + for<'de> Deserialize<'de> + Any + 'static,

Tries to insert a value into the map, and returns a mutable reference to the value if successful.

If the map already had this type of value present, nothing is updated, and an error containing the occupied entry and the value is returned.

Source

pub fn remove<T>(&mut self) -> Option<T>
where T: Serialize + for<'de> Deserialize<'de> + Any + 'static,

Remove the stored value of type T, returning it if was present, or None if it was not.

Source

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

Returns true if a value of type T exists in the map (regardless whether already deserialized or not).

Source

pub fn contains_deserialized<T>(&self) -> bool
where T: 'static,

Returns true if a value of type T exists in the map and has already been deserialized

Source

pub fn entry<T>(&mut self) -> Entry<'_, T>
where T: Serialize + for<'de> Deserialize<'de> + Any + 'static,

Entry API similar to HashMap::entry / anymap3::entry::<T>().

Source

pub fn keys(&self) -> impl Iterator<Item = &String>

Return an iterator over type-name keys.

Source

pub fn as_raw(&self) -> &HashMap<String, Wrapper>

Get access to the raw hash map that backs this.

This will seldom be useful, but it’s conceivable that you could wish to iterate over all the items in the collection, and this lets you do that.

Source

pub unsafe fn as_raw_mut(&mut self) -> &mut HashMap<String, Wrapper>

Get mutable access to the raw hash map that backs this.

This will seldom be useful, but it’s conceivable that you could wish to iterate over all the items in the collection mutably, or drain or something, or possibly even batch insert, and this lets you do that.

§Safety

If you insert any values to the raw map, the key (a String) must match the value’s type name as returned by any::type_name(), or undefined behaviour will occur when you access those values.

(Removing entries is perfectly safe.)

Source

pub fn into_raw(self) -> HashMap<String, Wrapper>

Convert this into the raw hash map that backs this.

This will seldom be useful, but it’s conceivable that you could wish to consume all the items in the collection and do something with some or all of them, and this lets you do that, without the unsafe that .as_raw_mut().drain() would require.

Source

pub unsafe fn from_raw(raw: HashMap<String, Wrapper>) -> SerializableAnyMap

Construct a map from a collection of raw values.

You know what? I can’t immediately think of any legitimate use for this.

Perhaps this will be most practical as unsafe { SerializableAnyMap::from_raw(iter.collect()) }, iter being an iterator over (String, Box<dyn Any + Serialize + Deserialize + 'static>) pairs. Eh, this method provides symmetry with into_raw, so I don’t care if literally no one ever uses it. I’m not even going to write a test for it, it’s so trivial.

§Safety

For all entries in the raw map, the key (a String) must match the value’s type as returned by any::type_name(), or undefined behaviour will occur when you access that entry.

Trait Implementations§

Source§

impl Clone for SerializableAnyMap

Source§

fn clone(&self) -> SerializableAnyMap

Returns a duplicate 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 Debug for SerializableAnyMap

Source§

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

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

impl Default for SerializableAnyMap

Source§

fn default() -> SerializableAnyMap

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

impl<'de> Deserialize<'de> for SerializableAnyMap

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<A: Any + Serialize + for<'de> Deserialize<'de>> Extend<Box<A>> for SerializableAnyMap

Source§

fn extend<T: IntoIterator<Item = Box<A>>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl Serialize for SerializableAnyMap

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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> CloneToUninit for T
where T: Clone,

Source§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,