runtime-context
A lightweight, type-safe runtime context for storing heterogenous values by type. It supports owned values and borrowed references (immutable or mutable) while keeping lookups fast via TypeId hashing.
Features
- Store owned values, borrowed references, or mutable references
- Type-safe retrieval via
TypeId - Zero-cost lookups using a specialized
TypeIdhasher - Works with trait objects via
better_any
Install
Add the crate to your Cargo.toml:
[]
= "0.1"
Quick Start
use ;
;
tid!;
Borrowed and Mutable References
use ;
;
tid!;
Advanced: Downcasting to Trait Objects
use ;
;
;
tid!
API Overview
Context::insert,Context::insert_ref,Context::insert_mut— insert valuesContext::get,Context::get_mut— retrieve typed valuesContext::get_data,Context::get_data_mut— retrieve byTypeIdContext::take— remove and return an owned valueContext::remove— remove a storedData
Safety and Notes
- Borrowed values are not cloned unless explicitly requested via
Data::into_owned. - Mutable references stored in the context follow Rust’s usual borrow rules.
TypeIdkeys are generated viabetter_any::tid.
License
Licensed under the MIT