async_iterator/iter/
mod.rs

1mod lend;
2mod lend_mut;
3mod map;
4
5pub use lend::Lend;
6pub use lend_mut::LendMut;
7pub use map::Map;
8
9use crate::FromIterator;
10
11/// An interface for dealing with iterators.
12
13#[must_use = "iterators are lazy and do nothing unless consumed"]
14pub trait Iterator {
15    /// The type of the elements being iterated over.
16    type Item;
17
18    /// Advances the iterator and returns the next value.
19    async fn next(&mut self) -> Option<Self::Item>;
20
21    /// Returns the bounds on the remaining length of the iterator.
22    fn size_hint(&self) -> (usize, Option<usize>) {
23        (0, None)
24    }
25
26    /// Takes a closure and creates an iterator which calls that closure on each element.
27    #[must_use = "iterators do nothing unless iterated over"]
28    fn map<B, F>(self, f: F) -> Map<Self, F>
29    where
30        Self: Sized,
31        F: FnMut(Self::Item) -> B,
32    {
33        Map::new(self, f)
34    }
35
36    /// Transforms an iterator into a collection.
37    #[must_use = "if you really need to exhaust the iterator, consider `.for_each(drop)` instead"]
38    async fn collect<B: FromIterator<Self::Item>>(self) -> B
39    where
40        Self: Sized,
41    {
42        let fut = <B as crate::FromIterator<_>>::from_iter(self);
43        fut.await
44    }
45
46    /// Creates an iterator which yields a reference to `self` as well as
47    /// the next value.
48    #[must_use = "iterators do nothing unless iterated over"]
49    fn lend(self) -> Lend<Self>
50    where
51        Self: Sized,
52    {
53        Lend::new(self)
54    }
55
56    /// Creates an iterator which yields a mutable reference to `self` as well
57    /// as the next value.
58    #[must_use = "iterators do nothing unless iterated over"]
59    fn lend_mut(self) -> LendMut<Self>
60    where
61        Self: Sized,
62    {
63        LendMut::new(self)
64    }
65}