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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use std::{
collections::{
hash_map::{Entry, IntoIter, Iter, IterMut, Keys, RandomState, Values, ValuesMut},
HashMap,
},
hash::{BuildHasher, Hash},
pin::Pin,
task::{Context, Poll},
};
use async_component_core::{AsyncComponent, StateCell};
#[derive(Debug)]
pub struct HashMapComponent<K, V, S = RandomState> {
updated: StateCell<()>,
map: HashMap<K, V, S>,
}
impl<K: Eq + Hash, V> HashMapComponent<K, V, RandomState> {
pub fn new() -> Self {
Self {
updated: StateCell::new(()),
map: HashMap::new(),
}
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
updated: StateCell::new(()),
map: HashMap::with_capacity(capacity),
}
}
}
impl<K: Eq + Hash, V, S: BuildHasher> HashMapComponent<K, V, S> {
pub fn with_hasher(hash_builder: S) -> Self {
Self {
updated: StateCell::new(()),
map: HashMap::with_hasher(hash_builder),
}
}
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self {
Self {
updated: StateCell::new(()),
map: HashMap::with_capacity_and_hasher(capacity, hash_builder),
}
}
pub fn contains_key(&self, key: &K) -> bool {
self.map.contains_key(key)
}
pub fn insert(&mut self, key: K, value: V) {
self.map.insert(key, value);
StateCell::invalidate(&mut self.updated);
}
pub fn get(&self, key: &K) -> Option<&V> {
self.map.get(key)
}
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
self.map.get_mut(key)
}
pub fn remove(&mut self, key: &K) -> Option<()> {
self.map.remove(key)?;
StateCell::invalidate(&mut self.updated);
Some(())
}
pub fn keys(&self) -> Keys<K, V> {
self.map.keys()
}
pub fn values(&self) -> Values<K, V> {
self.map.values()
}
pub fn values_mut(&mut self) -> ValuesMut<K, V> {
self.map.values_mut()
}
pub fn len(&self) -> usize {
self.map.len()
}
pub fn capacity(&self) -> usize {
self.map.capacity()
}
pub fn clear(&mut self) {
self.map.clear();
StateCell::invalidate(&mut self.updated);
}
pub fn retain(&mut self, f: impl FnMut(&K, &mut V) -> bool) {
self.map.retain(f);
StateCell::invalidate(&mut self.updated);
}
pub fn entry(&mut self, key: K) -> Entry<K, V> {
StateCell::invalidate(&mut self.updated);
self.map.entry(key)
}
pub fn iter(&self) -> Iter<K, V> {
self.map.iter()
}
pub fn iter_mut(&mut self) -> IterMut<K, V> {
self.map.iter_mut()
}
}
impl<'a, K: Eq + Hash, V, S: BuildHasher> IntoIterator for &'a HashMapComponent<K, V, S> {
type Item = (&'a K, &'a V);
type IntoIter = Iter<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
self.map.iter()
}
}
impl<'a, K: Eq + Hash, V, S: BuildHasher> IntoIterator for &'a mut HashMapComponent<K, V, S> {
type Item = (&'a K, &'a mut V);
type IntoIter = IterMut<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
self.map.iter_mut()
}
}
impl<K: Eq + Hash, V, S: BuildHasher> IntoIterator for HashMapComponent<K, V, S> {
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
fn into_iter(self) -> Self::IntoIter {
self.map.into_iter()
}
}
impl<K: Eq + Hash + Unpin, V: Unpin + AsyncComponent, S: Unpin> AsyncComponent
for HashMapComponent<K, V, S>
{
fn poll_next_state(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<()> {
let mut poll = Poll::Pending;
for component in self.map.values_mut() {
if Pin::new(component).poll_next_state(cx).is_ready() && poll.is_pending() {
poll = Poll::Ready(());
}
}
if StateCell::poll_state(Pin::new(&mut self.updated), cx).is_ready() && poll.is_pending() {
poll = Poll::Ready(());
}
poll
}
fn poll_next_stream(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<()> {
let mut poll = Poll::Pending;
for component in self.map.values_mut() {
if Pin::new(component).poll_next_stream(cx).is_ready() && poll.is_pending() {
poll = Poll::Ready(());
}
}
poll
}
}