1use std::collections::HashMap;
2use std::hash::Hash;
3use std::sync::Arc;
4use std::sync::RwLock;
5
6use futures::future::BoxFuture;
7use futures::future::Shared;
8
9pub type Value<'a, T> = Shared<BoxFuture<'a, T>>;
10
11#[derive(Clone, Debug)]
12pub struct Context<'a, K, V> {
13 context: Arc<RwLock<HashMap<K, Value<'a, V>>>>,
14}
15
16impl<K, V> Context<'_, K, V> {
17 pub fn new() -> Self {
18 Self {
19 context: Arc::new(RwLock::new(HashMap::new())),
20 }
21 }
22}
23
24impl<K, V> Default for Context<'_, K, V> {
25 fn default() -> Self {
26 Self::new()
27 }
28}
29
30impl<'a, K, V> Context<'a, K, V>
31where
32 K: Eq + Hash,
33{
34 pub fn get(&self, key: &K) -> Option<Value<'a, V>> {
35 self.context.read().unwrap().get(key).cloned()
36 }
37
38 pub fn set(&self, key: K, value: Value<'a, V>) -> Option<Value<'a, V>> {
39 self.context.write().unwrap().insert(key, value)
40 }
41}