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) areunsafe— 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
impl SerializableAnyMap
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create a new SerializableAnyMap with the specified capacity.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the map can hold without reallocating.
Sourcepub fn reserve(&mut self, additional: usize)
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.
Sourcepub fn shrink_to_fit(&mut self)
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.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes all items from the collection. Keeps the allocated memory for reuse.
Sourcepub fn get<T>(&mut self) -> Option<Result<&T, DeserializerError>>where
T: Serialize + for<'de> Deserialize<'de> + 'static,
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.
Sourcepub fn get_deserialized_copy<T>(&self) -> Option<T>where
T: for<'de> Deserialize<'de> + Any + 'static,
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.
Sourcepub fn get_mut<T>(
&mut self,
) -> Option<Result<WriteGuard<'_, T>, DeserializerError>>
pub fn get_mut<T>( &mut self, ) -> Option<Result<WriteGuard<'_, T>, DeserializerError>>
Get a mutable reference to a value of type T, lazily deserializing into the cache if necessary.
Sourcepub fn get_serialized_value<T>(&self) -> Option<&Value>where
T: 'static,
pub fn get_serialized_value<T>(&self) -> Option<&Value>where
T: 'static,
Get the serialized serde_value::Value for type T if present.
Sourcepub unsafe fn insert_value_by_name(
&mut self,
type_name: String,
value: Value,
) -> Option<Value>
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.
Sourcepub fn insert<T>(&mut self, value: T) -> Option<Result<T, DeserializerError>>
pub fn insert<T>(&mut self, value: T) -> Option<Result<T, DeserializerError>>
Insert a value of type T. Returns the previous value of that type if present.
Sourcepub fn try_insert<'a, T>(
&'a mut self,
value: T,
) -> Result<WriteGuard<'a, T>, OccupiedError<'a, T>>
pub fn try_insert<'a, T>( &'a mut self, value: T, ) -> Result<WriteGuard<'a, T>, OccupiedError<'a, T>>
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.
Sourcepub fn remove<T>(&mut self) -> Option<T>
pub fn remove<T>(&mut self) -> Option<T>
Remove the stored value of type T, returning it if was present, or None if it was not.
Sourcepub fn contains<T>(&self) -> boolwhere
T: 'static,
pub fn contains<T>(&self) -> boolwhere
T: 'static,
Returns true if a value of type T exists in the map (regardless whether already deserialized or not).
Sourcepub fn contains_deserialized<T>(&self) -> boolwhere
T: 'static,
pub fn contains_deserialized<T>(&self) -> boolwhere
T: 'static,
Returns true if a value of type T exists in the map and has already been deserialized
Sourcepub fn entry<T>(&mut self) -> Entry<'_, T>
pub fn entry<T>(&mut self) -> Entry<'_, T>
Entry API similar to HashMap::entry / anymap3::entry::<T>().
Sourcepub fn as_raw(&self) -> &HashMap<String, Wrapper>
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.
Sourcepub unsafe fn as_raw_mut(&mut self) -> &mut HashMap<String, Wrapper>
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.)
Sourcepub fn into_raw(self) -> HashMap<String, Wrapper>
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.
Sourcepub unsafe fn from_raw(raw: HashMap<String, Wrapper>) -> SerializableAnyMap
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
impl Clone for SerializableAnyMap
Source§fn clone(&self) -> SerializableAnyMap
fn clone(&self) -> SerializableAnyMap
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SerializableAnyMap
impl Debug for SerializableAnyMap
Source§impl Default for SerializableAnyMap
impl Default for SerializableAnyMap
Source§fn default() -> SerializableAnyMap
fn default() -> SerializableAnyMap
Source§impl<'de> Deserialize<'de> for SerializableAnyMap
impl<'de> Deserialize<'de> for SerializableAnyMap
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<A: Any + Serialize + for<'de> Deserialize<'de>> Extend<Box<A>> for SerializableAnyMap
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)
fn extend<T: IntoIterator<Item = Box<A>>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)