broker_tokio/io/util/repeat.rs
1use crate::io::AsyncRead;
2
3use std::io;
4use std::pin::Pin;
5use std::task::{Context, Poll};
6
7cfg_io_util! {
8 /// An async reader which yields one byte over and over and over and over and
9 /// over and...
10 ///
11 /// This struct is generally created by calling [`repeat`][repeat]. Please
12 /// see the documentation of `repeat()` for more details.
13 ///
14 /// This is an asynchronous version of [`std::io::Repeat`][std].
15 ///
16 /// [repeat]: fn.repeat.html
17 /// [std]: std::io::Repeat
18 #[derive(Debug)]
19 pub struct Repeat {
20 byte: u8,
21 }
22
23 /// Creates an instance of an async reader that infinitely repeats one byte.
24 ///
25 /// All reads from this reader will succeed by filling the specified buffer with
26 /// the given byte.
27 ///
28 /// This is an asynchronous version of [`std::io::repeat`][std].
29 ///
30 /// [std]: std::io::repeat
31 ///
32 /// # Examples
33 ///
34 /// ```
35 /// use tokio::io::{self, AsyncReadExt};
36 ///
37 /// #[tokio::main]
38 /// async fn main() {
39 /// let mut buffer = [0; 3];
40 /// io::repeat(0b101).read_exact(&mut buffer).await.unwrap();
41 /// assert_eq!(buffer, [0b101, 0b101, 0b101]);
42 /// }
43 /// ```
44 pub fn repeat(byte: u8) -> Repeat {
45 Repeat { byte }
46 }
47}
48
49impl AsyncRead for Repeat {
50 #[inline]
51 fn poll_read(
52 self: Pin<&mut Self>,
53 _: &mut Context<'_>,
54 buf: &mut [u8],
55 ) -> Poll<io::Result<usize>> {
56 for byte in &mut *buf {
57 *byte = self.byte;
58 }
59 Poll::Ready(Ok(buf.len()))
60 }
61}
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn assert_unpin() {
69 crate::is_unpin::<Repeat>();
70 }
71}