deluge/ops/
take.rs

1use crate::deluge::Deluge;
2use futures::Future;
3use std::cell::RefCell;
4
5pub struct Take<Del> {
6    deluge: Del,
7    how_many: usize,
8    how_many_provided: RefCell<usize>,
9}
10
11impl<Del> Take<Del> {
12    pub(crate) fn new(deluge: Del, how_many: usize) -> Self {
13        Self {
14            deluge,
15            how_many,
16            how_many_provided: RefCell::new(0),
17        }
18    }
19}
20impl<Del> Deluge for Take<Del>
21where
22    Del: Deluge + 'static,
23{
24    type Item = Del::Item;
25    type Output<'a> = impl Future<Output = Option<Self::Item>> + 'a;
26
27    fn next(&self) -> Option<Self::Output<'_>> {
28        let mut how_many_provided = self.how_many_provided.borrow_mut();
29        if *how_many_provided < self.how_many {
30            *how_many_provided += 1;
31            self.deluge.next()
32        } else {
33            None
34        }
35    }
36}