mutringbuf/iterators/async_iterators/
cons_iter.rs

1use core::marker::PhantomData;
2use core::task::Waker;
3
4use crate::Storage;
5use crate::iterators::ConsIter;
6use crate::iterators::async_iterators::async_macros::gen_common_futs_fn;
7use crate::iterators::async_iterators::{AsyncIterator, MRBFuture};
8use crate::iterators::iterator_trait::MutableSlice;
9use crate::iterators::iterator_trait::{MRBIterator, NonMutableSlice};
10use crate::iterators::util_macros::delegate;
11use crate::ring_buffer::variants::async_rb::AsyncMutRingBuf;
12use crate::ring_buffer::wrappers::buf_ref::BufRef;
13
14#[doc = r##"
15Async version of [`ConsIter`].
16"##]
17pub struct AsyncConsIter<'buf, S: Storage, const W: bool> {
18    inner: ConsIter<'buf, AsyncMutRingBuf<S>, W>,
19    waker: BufRef<'buf, AsyncMutRingBuf<S>>,
20}
21unsafe impl<S: Storage, const W: bool> Send for AsyncConsIter<'_, S, W> {}
22
23impl<'buf, S: Storage, const W: bool> AsyncIterator<'buf> for AsyncConsIter<'buf, S, W> {
24    type I = ConsIter<'buf, AsyncMutRingBuf<S>, W>;
25    type S = S;
26
27    fn register_waker(&self, waker: &Waker) {
28        (*self.waker).cons_waker.register(waker);
29    }
30
31    fn take_waker(&self) -> Option<Waker> {
32        (*self.waker).cons_waker.take()
33    }
34
35    fn wake_next(&self) {
36        (*self.waker).prod_waker.wake()
37    }
38
39    #[inline]
40    fn inner(&self) -> &Self::I {
41        &self.inner
42    }
43    #[inline]
44    fn inner_mut(&mut self) -> &mut Self::I {
45        &mut self.inner
46    }
47    fn into_sync(self) -> Self::I {
48        self.inner
49    }
50    fn from_sync(iter: Self::I, waker: BufRef<'buf, AsyncMutRingBuf<S>>) -> Self {
51        Self { inner: iter, waker }
52    }
53}
54
55impl<'buf, S: Storage<Item = T>, T, const W: bool> AsyncConsIter<'buf, S, W> {
56    gen_common_futs_fn!( 'buf );
57
58    delegate!(ConsIter, pub fn reset_index(&(mut) self));
59
60    /// Async version of [`ConsIter::peek_ref`].
61    pub fn peek_ref<'b>(&'b mut self) -> MRBFuture<'buf, 'b, Self, (), &'b T, true> {
62        #[inline]
63        fn f<'b, S: Storage<Item = T>, const W: bool, T>(
64            s: &mut AsyncConsIter<S, W>,
65            _: &mut (),
66        ) -> Option<&'b T> {
67            s.inner_mut().peek_ref()
68        }
69
70        MRBFuture {
71            iter: self,
72            p: Some(()),
73            f_r: Some(f),
74            f_m: None,
75            phantom: PhantomData,
76        }
77    }
78
79    /// Async version of [`ConsIter::peek_slice`].
80    pub fn peek_slice<'b>(
81        &'b mut self,
82        count: usize,
83    ) -> MRBFuture<'buf, 'b, Self, usize, NonMutableSlice<'b, T>, true> {
84        #[inline]
85        fn f<'b, S: Storage<Item = T>, const W: bool, T>(
86            s: &mut AsyncConsIter<S, W>,
87            count: &mut usize,
88        ) -> Option<NonMutableSlice<'b, T>> {
89            s.inner_mut().peek_slice(*count)
90        }
91
92        MRBFuture {
93            iter: self,
94            p: Some(count),
95            f_r: Some(f),
96            f_m: None,
97            phantom: PhantomData,
98        }
99    }
100
101    /// Async version of [`ConsIter::peek_available`].
102    pub fn peek_available<'b>(
103        &'b mut self,
104    ) -> MRBFuture<'buf, 'b, Self, (), NonMutableSlice<'b, T>, true> {
105        #[inline]
106        fn f<'b, S: Storage<Item = T>, const W: bool, T>(
107            s: &mut AsyncConsIter<S, W>,
108            _: &mut (),
109        ) -> Option<NonMutableSlice<'b, T>> {
110            s.inner_mut().peek_available()
111        }
112
113        MRBFuture {
114            iter: self,
115            p: Some(()),
116            f_r: Some(f),
117            f_m: None,
118            phantom: PhantomData,
119        }
120    }
121
122    /// Async version of [`ConsIter::pop`].
123    /// # Safety
124    /// See above.
125    pub fn pop<'b>(&'b mut self) -> MRBFuture<'buf, 'b, Self, (), T, true> {
126        #[inline]
127        fn f<S: Storage<Item = T>, const W: bool, T>(
128            s: &mut AsyncConsIter<S, W>,
129            _: &mut (),
130        ) -> Option<T> {
131            s.inner_mut().pop()
132        }
133
134        MRBFuture {
135            iter: self,
136            p: Some(()),
137            f_r: Some(f),
138            f_m: None,
139            phantom: PhantomData,
140        }
141    }
142
143    /// Async version of [`ConsIter::pop_move`].
144    /// # Safety
145    /// See above.
146    pub unsafe fn pop_move<'b>(&'b mut self) -> MRBFuture<'buf, 'b, Self, (), T, true> {
147        #[inline]
148        fn f<S: Storage<Item = T>, const W: bool, T>(
149            s: &mut AsyncConsIter<S, W>,
150            _: &mut (),
151        ) -> Option<T> {
152            unsafe { s.inner_mut().pop_move() }
153        }
154
155        MRBFuture {
156            iter: self,
157            p: Some(()),
158            f_r: Some(f),
159            f_m: None,
160            phantom: PhantomData,
161        }
162    }
163
164    /// Async version of [`ConsIter::copy_item`].
165    pub fn copy_item<'b>(
166        &'b mut self,
167        dst: &'b mut T,
168    ) -> MRBFuture<'buf, 'b, Self, &'b mut T, (), true>
169    where
170        T: Copy,
171    {
172        #[inline]
173        fn f<S: Storage<Item = T>, const W: bool, T: Copy>(
174            s: &mut AsyncConsIter<S, W>,
175            dst: &mut &mut T,
176        ) -> Option<()> {
177            s.inner_mut().copy_item(*dst)
178        }
179
180        MRBFuture {
181            iter: self,
182            p: Some(dst),
183            f_r: Some(f),
184            f_m: None,
185            phantom: PhantomData,
186        }
187    }
188
189    /// Async version of [`ConsIter::clone_item`].
190    pub fn clone_item<'b>(
191        &'b mut self,
192        dst: &'b mut T,
193    ) -> MRBFuture<'buf, 'b, Self, &'b mut T, (), true>
194    where
195        T: Clone,
196    {
197        #[inline]
198        fn f<S: Storage<Item = T>, const W: bool, T: Clone>(
199            s: &mut AsyncConsIter<S, W>,
200            dst: &mut &mut T,
201        ) -> Option<()> {
202            s.inner_mut().clone_item(*dst)
203        }
204
205        MRBFuture {
206            iter: self,
207            p: Some(dst),
208            f_r: Some(f),
209            f_m: None,
210            phantom: PhantomData,
211        }
212    }
213
214    /// Async version of [`ConsIter::copy_slice`].
215    pub fn copy_slice<'b>(
216        &'b mut self,
217        dst: &'b mut [T],
218    ) -> MRBFuture<'buf, 'b, Self, &'b mut [T], (), true>
219    where
220        T: Copy,
221    {
222        #[inline]
223        fn f<S: Storage<Item = T>, const W: bool, T: Copy>(
224            s: &mut AsyncConsIter<S, W>,
225            dst: &mut &mut [T],
226        ) -> Option<()> {
227            s.inner_mut().copy_slice(dst)
228        }
229
230        MRBFuture {
231            iter: self,
232            p: Some(dst),
233            f_r: Some(f),
234            f_m: None,
235            phantom: PhantomData,
236        }
237    }
238
239    /// Async version of [`ConsIter::clone_slice`].
240    pub fn clone_slice<'b>(
241        &'b mut self,
242        dst: &'b mut [T],
243    ) -> MRBFuture<'buf, 'b, Self, &'b mut [T], (), true>
244    where
245        T: Clone,
246    {
247        #[inline]
248        fn f<S: Storage<Item = T>, const W: bool, T: Clone>(
249            s: &mut AsyncConsIter<S, W>,
250            dst: &mut &mut [T],
251        ) -> Option<()> {
252            s.inner_mut().clone_slice(dst)
253        }
254
255        MRBFuture {
256            iter: self,
257            p: Some(dst),
258            f_r: Some(f),
259            f_m: None,
260            phantom: PhantomData,
261        }
262    }
263}