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
use std::{
ops::RangeBounds,
pin::Pin,
slice,
task::{Context, Poll},
vec,
};
use async_component_core::{AsyncComponent, StateCell};
#[derive(Debug)]
pub struct VecComponent<T> {
updated: StateCell<()>,
vec: Vec<T>,
}
impl<T: AsyncComponent> VecComponent<T> {
pub const fn new() -> Self {
Self {
updated: StateCell::new(()),
vec: Vec::new(),
}
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
updated: StateCell::new(()),
vec: Vec::with_capacity(capacity),
}
}
pub fn capacity(&self) -> usize {
self.vec.capacity()
}
pub fn remove(&mut self, index: usize) {
self.vec.remove(index);
StateCell::invalidate(&mut self.updated);
}
pub fn push(&mut self, component: T) {
self.vec.push(component);
StateCell::invalidate(&mut self.updated);
}
pub fn append(&mut self, other: &mut Vec<T>) {
self.vec.append(other);
StateCell::invalidate(&mut self.updated);
}
pub fn pop(&mut self) -> Option<()> {
self.vec.pop()?;
StateCell::invalidate(&mut self.updated);
Some(())
}
pub fn drain(&mut self, range: impl RangeBounds<usize>) {
self.vec.drain(range);
StateCell::invalidate(&mut self.updated);
}
pub fn retain(&mut self, f: impl FnMut(&T) -> bool) {
self.vec.retain(f);
StateCell::invalidate(&mut self.updated);
}
pub fn clear(&mut self) {
self.vec.clear();
StateCell::invalidate(&mut self.updated);
}
}
impl<T: AsyncComponent> Default for VecComponent<T> {
fn default() -> Self {
Self::new()
}
}
impl<'a, T: AsyncComponent> IntoIterator for &'a VecComponent<T> {
type Item = &'a T;
type IntoIter = slice::Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.vec.iter()
}
}
impl<'a, T: AsyncComponent> IntoIterator for &'a mut VecComponent<T> {
type Item = &'a mut T;
type IntoIter = slice::IterMut<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.vec.iter_mut()
}
}
impl<T: AsyncComponent> IntoIterator for VecComponent<T> {
type Item = T;
type IntoIter = vec::IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
self.vec.into_iter()
}
}
impl<T: AsyncComponent> AsyncComponent for VecComponent<T> {
fn poll_next_state(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<()> {
let mut result = Poll::Pending;
if StateCell::poll_state(Pin::new(&mut self.updated), cx).is_ready() {
result = Poll::Ready(());
}
for component in &mut self.vec {
if Pin::new(component).poll_next_state(cx).is_ready() {
if result.is_pending() {
result = Poll::Ready(());
}
}
}
result
}
fn poll_next_stream(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<()> {
let mut result = Poll::Pending;
for component in &mut self.vec {
if Pin::new(component).poll_next_stream(cx).is_ready() {
if result.is_pending() {
result = Poll::Ready(());
}
}
}
result
}
}