dag_flow/
context.rs

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
9#[derive(Clone, Debug)]
10pub struct Context<'a, K, V> {
11    context: Arc<RwLock<HashMap<K, Shared<BoxFuture<'a, V>>>>>,
12}
13
14impl<K, V> Context<'_, K, V> {
15    pub fn new() -> Self {
16        Self {
17            context: Arc::new(RwLock::new(HashMap::new())),
18        }
19    }
20}
21
22impl<K, V> Default for Context<'_, K, V> {
23    fn default() -> Self {
24        Self::new()
25    }
26}
27
28impl<'a, K, V> Context<'a, K, V>
29where
30    K: Eq + Hash,
31{
32    pub fn get(&self, key: &K) -> Option<Shared<BoxFuture<'a, V>>> {
33        self.context.read().unwrap().get(key).cloned()
34    }
35
36    pub fn set(&self, key: K, value: Shared<BoxFuture<'a, V>>) -> Option<Shared<BoxFuture<'a, V>>> {
37        self.context.write().unwrap().insert(key, value)
38    }
39}