futures_util/io/
repeat.rs1use futures_core::ready;
2use futures_core::task::{Context, Poll};
3use futures_io::{AsyncRead, IoSliceMut};
4use std::fmt;
5use std::io;
6use std::pin::Pin;
7
8#[must_use = "readers do nothing unless polled"]
10pub struct Repeat {
11    byte: u8,
12}
13
14pub fn repeat(byte: u8) -> Repeat {
32    Repeat { byte }
33}
34
35impl AsyncRead for Repeat {
36    #[inline]
37    fn poll_read(
38        self: Pin<&mut Self>,
39        _: &mut Context<'_>,
40        buf: &mut [u8],
41    ) -> Poll<io::Result<usize>> {
42        for slot in &mut *buf {
43            *slot = self.byte;
44        }
45        Poll::Ready(Ok(buf.len()))
46    }
47
48    #[inline]
49    fn poll_read_vectored(
50        mut self: Pin<&mut Self>,
51        cx: &mut Context<'_>,
52        bufs: &mut [IoSliceMut<'_>],
53    ) -> Poll<io::Result<usize>> {
54        let mut nwritten = 0;
55        for buf in bufs {
56            nwritten += ready!(self.as_mut().poll_read(cx, buf))?;
57        }
58        Poll::Ready(Ok(nwritten))
59    }
60}
61
62impl fmt::Debug for Repeat {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        f.pad("Repeat { .. }")
65    }
66}