Crate anymap_serde

Crate anymap_serde 

Source
Expand description

A serializable AnyMap-like container keyed by type_name::<T>().

This crate provides SerializableAnyMap, an AnyMap-compatible container that stores values in a serializable form (serde_value::Value) and optionally caches a deserialized Box<dyn Any> for fast access. Keys are the stable string returned by key_for_type::<T>(), not TypeId, which makes the map suitable for sending over the network and reconstructing on another process / compilation unit.

Key properties:

  • The on-disk / on-wire representation is a HashMap<String, serde_value::Value>.
  • Deserialized values are stored in-memory only and are not serialized.
  • Inserting a value serializes it immediately and stores the value in both forms.
  • Deserialization reconstructs only the serialized representation; the cache is empty until a lazy access triggers deserialization, since we don’t have access to type information at collection deserialization time

§Motivation

This is useful for extension holders (for example request extensions) where heterogeneous typed data must be transported and reconstructed remotely.

Example (illustrative):

#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Ext { a: u32 }

let mut map = SerializableAnyMap::new();
map.insert(Ext { a: 10 });

// Serialize to JSON
let json = serde_json::to_string(&map).unwrap();

// On the other side, deserialize and access the stored type by `type_name::<T>()`
let mut map2: SerializableAnyMap = serde_json::from_str(&json).unwrap();
let value: &Ext = map2.get::<Ext>().unwrap().unwrap();
assert_eq!(value.a, 10);

Structs§

SerializableAnyMap
A serializable heterogeneous-map keyed by the stable type_name::<T>().
Wrapper
A stored entry containing the serialized representation and an optional cached runtime value.