summaryrefslogtreecommitdiffstats
path: root/tokio/src/io/util/repeat.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tokio/src/io/util/repeat.rs')
-rw-r--r--tokio/src/io/util/repeat.rs67
1 files changed, 67 insertions, 0 deletions
diff --git a/tokio/src/io/util/repeat.rs b/tokio/src/io/util/repeat.rs
new file mode 100644
index 00000000..22e62a93
--- /dev/null
+++ b/tokio/src/io/util/repeat.rs
@@ -0,0 +1,67 @@
+use crate::io::AsyncRead;
+
+use std::io;
+use std::pin::Pin;
+use std::task::{Context, Poll};
+
+/// An async reader which yields one byte over and over and over and over and
+/// over and...
+///
+/// This struct is generally created by calling [`repeat`][repeat]. Please
+/// see the documentation of `repeat()` for more details.
+///
+/// This is an asynchronous version of [`std::io::Repeat`][std].
+///
+/// [repeat]: fn.repeat.html
+/// [std]: https://doc.rust-lang.org/std/io/struct.Repeat.html
+#[derive(Debug)]
+pub struct Repeat {
+ byte: u8,
+}
+
+/// Creates an instance of an async reader that infinitely repeats one byte.
+///
+/// All reads from this reader will succeed by filling the specified buffer with
+/// the given byte.
+///
+/// This is an asynchronous version of [`std::io::repeat`][std].
+///
+/// # Examples
+///
+/// ```
+/// # use tokio::io::{self, AsyncReadExt};
+/// # async fn dox() {
+/// let mut buffer = [0; 3];
+/// io::repeat(0b101).read_exact(&mut buffer).await.unwrap();
+/// assert_eq!(buffer, [0b101, 0b101, 0b101]);
+/// # }
+/// ```
+///
+/// [std]: https://doc.rust-lang.org/std/io/fn.repeat.html
+pub fn repeat(byte: u8) -> Repeat {
+ Repeat { byte }
+}
+
+impl AsyncRead for Repeat {
+ #[inline]
+ fn poll_read(
+ self: Pin<&mut Self>,
+ _: &mut Context<'_>,
+ buf: &mut [u8],
+ ) -> Poll<io::Result<usize>> {
+ for byte in &mut *buf {
+ *byte = self.byte;
+ }
+ Poll::Ready(Ok(buf.len()))
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn assert_unpin() {
+ crate::is_unpin::<Repeat>();
+ }
+}