tokio_buf/util/
iter.rs

1use bytes::Buf;
2use futures::Poll;
3use std::error::Error;
4use std::fmt;
5use BufStream;
6
7/// Converts an `Iterator` into a `BufStream` which is always ready to yield the
8/// next value.
9///
10/// Iterators in Rust don't express the ability to block, so this adapter
11/// simply always calls `iter.next()` and returns that.
12pub fn iter<I>(i: I) -> Iter<I::IntoIter>
13where
14    I: IntoIterator,
15    I::Item: Buf,
16{
17    Iter {
18        iter: i.into_iter(),
19    }
20}
21
22/// `BufStream` returned by the [`iter`] function.
23#[derive(Debug)]
24pub struct Iter<I> {
25    iter: I,
26}
27
28#[derive(Debug)]
29pub enum Never {}
30
31impl<I> BufStream for Iter<I>
32where
33    I: Iterator,
34    I::Item: Buf,
35{
36    type Item = I::Item;
37    type Error = Never;
38
39    fn poll_buf(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
40        Ok(self.iter.next().into())
41    }
42}
43
44impl fmt::Display for Never {
45    fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
46        unreachable!();
47    }
48}
49
50impl Error for Never {
51    fn description(&self) -> &str {
52        unreachable!();
53    }
54}