2022-12-09 01:19:47 +00:00
|
|
|
use async_trait::async_trait;
|
2022-11-08 01:30:10 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-12-09 01:19:47 +00:00
|
|
|
#[async_trait]
|
2022-11-08 01:30:10 +00:00
|
|
|
pub trait RefCountValue {
|
2023-02-13 01:29:49 +00:00
|
|
|
async fn did_remove(&self) {}
|
2022-11-08 01:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct RefCountHandler<T> {
|
2023-02-13 01:29:49 +00:00
|
|
|
ref_count: usize,
|
|
|
|
inner: T,
|
2022-11-08 01:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> RefCountHandler<T> {
|
2023-02-13 01:29:49 +00:00
|
|
|
pub fn new(inner: T) -> Self {
|
|
|
|
Self {
|
|
|
|
ref_count: 1,
|
|
|
|
inner,
|
2022-11-08 01:30:10 +00:00
|
|
|
}
|
2023-02-13 01:29:49 +00:00
|
|
|
}
|
2022-11-08 01:30:10 +00:00
|
|
|
|
2023-02-13 01:29:49 +00:00
|
|
|
pub fn increase_ref_count(&mut self) {
|
|
|
|
self.ref_count += 1;
|
|
|
|
}
|
2022-11-08 01:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RefCountHashMap<T>(HashMap<String, RefCountHandler<T>>);
|
|
|
|
|
2022-11-08 03:32:07 +00:00
|
|
|
impl<T> std::default::Default for RefCountHashMap<T> {
|
2023-02-13 01:29:49 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self(HashMap::new())
|
|
|
|
}
|
2022-11-08 03:32:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 01:30:10 +00:00
|
|
|
impl<T> RefCountHashMap<T>
|
|
|
|
where
|
2023-02-13 01:29:49 +00:00
|
|
|
T: Clone + Send + Sync + RefCountValue + 'static,
|
2022-11-08 01:30:10 +00:00
|
|
|
{
|
2023-02-13 01:29:49 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
2022-11-08 01:30:10 +00:00
|
|
|
|
2023-02-13 01:29:49 +00:00
|
|
|
pub fn get(&self, key: &str) -> Option<T> {
|
|
|
|
self.0.get(key).map(|handler| handler.inner.clone())
|
|
|
|
}
|
2022-11-08 03:32:07 +00:00
|
|
|
|
2023-02-13 01:29:49 +00:00
|
|
|
pub fn values(&self) -> Vec<T> {
|
|
|
|
self
|
|
|
|
.0
|
|
|
|
.values()
|
|
|
|
.map(|value| value.inner.clone())
|
|
|
|
.collect::<Vec<T>>()
|
|
|
|
}
|
2022-11-08 01:30:10 +00:00
|
|
|
|
2023-02-13 01:29:49 +00:00
|
|
|
pub fn insert(&mut self, key: String, value: T) {
|
|
|
|
if let Some(handler) = self.0.get_mut(&key) {
|
|
|
|
handler.increase_ref_count();
|
|
|
|
} else {
|
|
|
|
let handler = RefCountHandler::new(value);
|
|
|
|
self.0.insert(key, handler);
|
2022-11-08 01:30:10 +00:00
|
|
|
}
|
2023-02-13 01:29:49 +00:00
|
|
|
}
|
2022-11-08 01:30:10 +00:00
|
|
|
|
2023-02-13 01:29:49 +00:00
|
|
|
pub async fn remove(&mut self, key: &str) {
|
|
|
|
let mut should_remove = false;
|
|
|
|
if let Some(value) = self.0.get_mut(key) {
|
|
|
|
if value.ref_count > 0 {
|
|
|
|
value.ref_count -= 1;
|
|
|
|
}
|
|
|
|
should_remove = value.ref_count == 0;
|
|
|
|
}
|
2022-11-08 01:30:10 +00:00
|
|
|
|
2023-02-13 01:29:49 +00:00
|
|
|
if should_remove {
|
|
|
|
if let Some(handler) = self.0.remove(key) {
|
|
|
|
tokio::spawn(async move {
|
|
|
|
handler.inner.did_remove().await;
|
|
|
|
});
|
|
|
|
}
|
2022-11-08 01:30:10 +00:00
|
|
|
}
|
2023-02-13 01:29:49 +00:00
|
|
|
}
|
2022-11-08 01:30:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 01:19:47 +00:00
|
|
|
#[async_trait]
|
2022-11-08 01:30:10 +00:00
|
|
|
impl<T> RefCountValue for Arc<T>
|
|
|
|
where
|
2023-02-13 01:29:49 +00:00
|
|
|
T: RefCountValue + Sync + Send,
|
2022-11-08 01:30:10 +00:00
|
|
|
{
|
2023-02-13 01:29:49 +00:00
|
|
|
async fn did_remove(&self) {
|
|
|
|
(**self).did_remove().await
|
|
|
|
}
|
2022-11-08 01:30:10 +00:00
|
|
|
}
|