TypedMap
TypedMap
is a typed HashMap
. It allows you to define different value type depending on a Key type. It's useful if you want to store different key-value pairs in a single hashmap, for example in HTTP app that implements multiple services.
use ;
// Define key types
;
;
;
;
// Define value type for key types
// Create a new empty map
let mut animal_sounds: TypedMap = new;
// Insert data
animal_sounds.insert;
animal_sounds.insert;
// Get for Dog key get value of type Bark.
let spiky_volume = animal_sounds.get.unwrap.volume;
assert_eq!;
// Define key types
;
;
// Define value type for key types
// Create a new empty map
let mut data: TypedMap = new;
// Insert data
data.insert;
data.insert;
You can use special Marker
type to create more "type" key-value bindings.
;
;
use ;
;
;
;
;
let mut configs: = new;
let mut services: = new;
configs.insert;
configs.insert;
services.insert;
services.insert;
If dashmap
feature is enabled, one can use TypedDashMap
that can be used concurrently, as it's using Dashmap
under the hood.
use Arc;
use TypedDashMap;
use TypedMapKey;
;
;
;
// Implement key-value mapping for Configs marker
// Implement key-value mapping for Services marker
;
// Implement key-value mapping for Configs marker
// Implement key-value mapping for Services marker
// Implement key-value mapping for default (i.e. ()) marker
let configs: = new;
let services: = new;
let default: = new;
let configs1 = clone;
let services1 = clone;
let t1 = spawn;
// Line below would not compile, because TypeMapKey<Marker=()>
// is not implemented for Key.
// default.insert(Key(0), 1);
// Line below would not compile, because SerivceA key defines
// type value as usize for Configs marker (not &'static str)
// configs.insert(ServiceA(0), "one");
let configs2 = clone;
let services2 = clone;
let default2 = clone;
let t2 = spawn;
t1.join.unwrap;
t2.join.unwrap;
By default
TypedMap
accepts keys and values that implement std::any::Any
trait (and of course TypedMapKey
trait),
while TypedDashMap
requires that keys and values meet std::any::Any + Send + Sync
bounds.
However, both TypedMap
and TypedDashMap
can be parametrized with two type parameters: key bounds (KB
) and value bounds (VB
).
This mechanism allows to restrict what kind of keys and values can be stored in the hashmap. This crate provides four implementations of bounds:
bounds::AnyBounds
- representsAny
bounds (used by default inTypedMap
),bounds::SyncAnyBounds
- representsAny + Sync + Send
bounds (used by default inTypedDashMap
),clone::CloneBounds
(ifclone
feature is enabled) - representsClone + Any
bounds - allows to restrict keys/values to clonable types,clone::SyncCloneBounds
(ifclone
feature is enabled) - representsClone + Any + Send + Sync
bounds.
These bounds can be specified in the type signature, e.g.
use ;
use ;
let mut map: = new_with_bounds;
It is possible to define own bounds using Bounds and HasBounds traits to add custom restrictions to values. For example, you may want to enforce that each value implements a custom Component trait. This can be done with a few lines of code using impl_custom_bounds and impl_dyn_trait_wrapper macros.
// Your custom marker (could use also () as well)
use ;
;
// Trait that each value should implement
// Bounds
;
// This defines DynComponent that will be Any + Component, used internally to keep values
impl_dyn_trait_wrapper!;
// This defines new "bounds" struct, that requires to impl Component and uses
// dyn DynComponent + Send + Sync as a value container
impl_custom_bounds!;
;
;
// Create a new TypedDashMap that uses Components marker, keys may be Any, but value must impl Component
let state: = new_with_bounds;
state.insert;
let iter = state.iter.filter;
assert_eq!;
Related:
- type-map - hashmap that uses types as keys
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
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.