mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-22 00:00:11 +02:00
The standard library's `io` module has small utilities such as `repeat`, `empty`, and `sink`, which return `Read` and `Write` implementations. These can come in handy in some circiumstances. `tokio::io` has no equivalents that implement `AsyncRead`/`AsyncWrite`. This commit adds `repeat`, `empty`, and `sink` helpers to `tokio::io`.
68 lines
1.5 KiB
Rust
68 lines
1.5 KiB
Rust
use crate::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 as io, 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>();
|
|
}
|
|
}
|