anystash 0.1.0

A modern, zero-dependency, zero-unsafe heterogeneous type map. Successor niche of the unmaintained `typemap` crate.
Documentation
  • Coverage
  • 100%
    27 out of 27 items documented27 out of 27 items with examples
  • Size
  • Source code size: 47.4 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 208.4 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 4s Average build duration of successful builds.
  • all releases: 4s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • faizanali2k05/anystash
    16 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • faizanali2k05

anystash

crates.io docs.rs CI license

A modern, zero-dependency, zero-unsafe heterogeneous type map for Rust.

Store at most one value of each type; retrieve it by type. This is the building block behind HTTP request extensions, plugin registries, dependency-injection containers and per-entity data — a niche long served by the unmaintained typemap crate, which anystash replaces.

[dependencies]
anystash = "0.1"

Example

use anystash::Stash;

#[derive(Debug, PartialEq)]
struct Config { retries: u32 }

let mut stash = Stash::new();
stash.insert(Config { retries: 3 });
stash.insert("hello");

assert_eq!(stash.get::<Config>(), Some(&Config { retries: 3 }));
assert_eq!(stash.get::<&str>(), Some(&"hello"));

stash.get_mut::<Config>().unwrap().retries = 5;
assert_eq!(stash.remove::<Config>(), Some(Config { retries: 5 }));

Choosing a map

Both types expose an identical API and differ only in the bound on stored values.

Type Value bound Map is Send + Sync Use when
Stash 'static no Single-threaded; values may be !Send
SyncStash 'static + Send + Sync yes Shared across threads behind Arc/a lock

Prefer Stash unless the map must cross a thread boundary — it accepts strictly more types, including Rc and RefCell.

API

Method Description
new / with_capacity Create an empty map
insert(value) Store a value, returning any previous one
get::<T> / get_mut::<T> Borrow the stored value of type T
get_or_default::<T> Borrow, inserting T::default() if absent
get_or_insert_with(f) Borrow, inserting f() if absent
remove::<T> Take the value of type T back out
contains::<T> Test whether a value of type T is present
len / is_empty / clear Inspect or empty the map

The newtype pattern

Lookup is keyed by type, so two logically distinct values sharing a type collide. Wrap them to keep them apart:

use anystash::Stash;

struct RequestId(String);
struct TraceId(String);

let mut stash = Stash::new();
stash.insert(RequestId("req-1".into()));
stash.insert(TraceId("trace-9".into()));

assert_eq!(stash.len(), 2); // two `String`s coexist

Why anystash

  • No dependencies. Nothing beyond std — minimal build time and supply-chain surface.
  • No unsafe. #![forbid(unsafe_code)]; downcasting goes through std::any.
  • No trait to implement. Unlike typemap, values need no Key impl — any 'static type works, including types from other crates.
  • Documented. Every public method carries a compiling example.

Examples

cargo run --example request_extensions

Shows middleware layers attaching typed data to a request without knowing about one another.

Minimum supported Rust version

Rust 1.75. An MSRV bump is treated as a breaking change and will come with a minor version bump before 1.0.

License

Licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.