Skip to content

Instantly share code, notes, and snippets.

Created May 11, 2016 00:13
Show Gist options
  • Save anonymous/27c6d51e60e9cecf3693ef8c9f814dc4 to your computer and use it in GitHub Desktop.
Save anonymous/27c6d51e60e9cecf3693ef8c9f814dc4 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
use std::sync::{Arc, RwLock};
use std::collections::HashMap;
use std::hash::Hash;
use std::cmp::Eq;
trait SessionStore<K, V> {
fn remove(&self, key: &K) -> bool;
}
type Store<K, V> = RwLock<HashMap<K, RwLock<V>>>;
pub struct HashSessionStore<K, V>{
store: Arc<Store<K, V>>
}
impl<K: Clone + Send, V: Send> Clone for HashSessionStore<K, V> {
fn clone(&self) -> HashSessionStore<K, V> {
HashSessionStore {
store: self.store.clone()
}
}
}
impl<K: Hash + Eq + Send + Sync, V: Send + Sync> HashSessionStore<K, V> {
/// Create a new instance of the session store
pub fn new() -> HashSessionStore<K, V> {
HashSessionStore {
store: Arc::new(RwLock::new(HashMap::<K, RwLock<V>>::new()))
}
}
}
impl<K: Hash + Eq + Send + Sync + Clone, V: Send + Sync + Clone> SessionStore<K, V> for HashSessionStore<K, V> {
fn remove(&self, key: &K) -> bool {
self.store.write().unwrap().remove(key).is_some()
}
}
fn main() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment