๐ Announcing **anymap-serde** โ a serde-friendly heterogeneous container for Rust
โ Rust ๐ 2025-11-28 ๐ค surdeus ๐๏ธ 2Hello Rustaceans,
Iโm excited to share a new crate Iโve been working on: anymap-serde! Itโs now available on GitHub (and crates.io: Rust Package Registry), and provides a serde-powered alternative to classic type-indexed โanymapโ containers.
What it is
- Type-indexed heterogeneous map โ you can store exactly one value per type, and retrieve it by its type.
- Serde-compatible โ an
anymap-serdecontainer can be serialized and deserialzed under the hood usingserde. That enables you to serialize / deserialize the entire map of heterogenous values to/from JSON, TOML, or other formats supported by Serde. - Ergonomic API โ almost identical mental model to the โanymapโ crate, but exposes deserialization errors where it makes sense.
Typical usage
use anymap_serde::SerializableAnyMap;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct Config { /* โฆ */ }
#[derive(Serialize, Deserialize, Debug)]
struct Stats { /* โฆ */ }
let mut bag = SerializableAnyMap::new();
bag.insert(Config { /* โฆ */ });
bag.insert(Stats { /* โฆ */ });
// You can serialize the bag:
let s = serde_json::to_string(&bag)?;
// Later deserialize:
let bag2: SerializableAnyMap = serde_json::from_str(&s)?;
// And retrieve typed data:
let cfg = bag2.get::<Config>().unwrap();
let stats = bag2.get::<Stats>().unwrap();
This makes anymap-serde ideal for use-cases like: configuration stores, plugin-state bags, context/extension objects, or any scenario where you want a type-safe, heterogenous โbagโ of data โ and want to persist or transmit it.
Caveats / What to watch out for
- Stored types must implement
Serialize + Deserialize + 'static. - Because data is serialized internally, renaming types or changing their structure may break deserialization.
- At deserialization time, you need to know what types you expect to extract; missing types will simply return
None. - Serialization happens on insert, and all modifications of values using
.get_mut(). This is a significant case that is not fit for hot-paths. - Deserialization of values is lazy (and cannot be made eager) - it happens on first access of the values where type information of T is available.
Status & how to try it out
- The crate is currently hosted on GitHub: https://github.com/axos88/anymap-serde
- Documentation and examples are included.
- The API still needs some polishing.
If you've ever needed an โanymap + serdeโ without wrestling with manual serialization, or a flexible context store with type safety and persistability โ please give anymap-serde a spin and let me know what you think! Iโm especially curious about real-world use cases, edge-cases with nested data, and any feedback about ergonomics.
Thanks for reading, and happy Rusting! ![]()
2 posts - 2 participants
๐ท๏ธ Rust_feed