1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::borrow::Borrow;
use std::collections::HashSet;
use std::hash::Hash;
use tokio::sync::RwLock;
#[derive(Default)]
pub struct AsyncSet<T>
where
T: Eq + Hash,
{
data: RwLock<HashSet<T>>,
}
impl<T> AsyncSet<T>
where
T: Eq + Hash + Clone,
{
pub fn new() -> Self {
Self {
data: Default::default(),
}
}
pub async fn reserve(&self, additional: usize) {
let mut guard = self.data.write().await;
guard.reserve(additional);
}
#[allow(clippy::clippy::len_without_is_empty)]
pub async fn len(&self) -> usize {
let guard = self.data.read().await;
guard.len()
}
pub async fn is_empty(&self) -> bool {
let guard = self.data.read().await;
guard.is_empty()
}
pub async fn clear(&self) {
let mut guard = self.data.write().await;
guard.clear();
}
pub async fn insert(&self, value: T) -> bool {
let mut guard = self.data.write().await;
guard.insert(value)
}
pub async fn contains<Q: ?Sized>(&self, value: &Q) -> bool
where
T: Borrow<Q>,
Q: Hash + Eq,
{
let guard = self.data.read().await;
guard.contains(value)
}
pub async fn remove<Q: ?Sized>(&self, value: &Q) -> bool
where
T: Borrow<Q>,
Q: Hash + Eq,
{
let mut guard = self.data.write().await;
guard.remove(value)
}
}