Sha256: d4effcd81338831eb373cf2db972a99218b8379b91066940a732edcf4524c7c2

Contents?: true

Size: 1.75 KB

Versions: 24

Compression:

Stored size: 1.75 KB

Contents

use crate::io::{AsyncRead, ReadBuf};

use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};

cfg_io_util! {
    /// 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
    /// [std]: std::io::Repeat
    #[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].
    ///
    /// [std]: std::io::repeat
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio::io::{self, AsyncReadExt};
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let mut buffer = [0; 3];
    ///     io::repeat(0b101).read_exact(&mut buffer).await.unwrap();
    ///     assert_eq!(buffer, [0b101, 0b101, 0b101]);
    /// }
    /// ```
    pub fn repeat(byte: u8) -> Repeat {
        Repeat { byte }
    }
}

impl AsyncRead for Repeat {
    #[inline]
    fn poll_read(
        self: Pin<&mut Self>,
        _: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<io::Result<()>> {
        // TODO: could be faster, but should we unsafe it?
        while buf.remaining() != 0 {
            buf.put_slice(&[self.byte]);
        }
        Poll::Ready(Ok(()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn assert_unpin() {
        crate::is_unpin::<Repeat>();
    }
}

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
wasmtime-17.0.1 ./ext/cargo-vendor/tokio-1.35.1/src/io/util/repeat.rs
wasmtime-17.0.0 ./ext/cargo-vendor/tokio-1.35.1/src/io/util/repeat.rs
wasmtime-16.0.0 ./ext/cargo-vendor/tokio-1.35.1/src/io/util/repeat.rs
wasmtime-15.0.1 ./ext/cargo-vendor/tokio-1.35.1/src/io/util/repeat.rs
wasmtime-15.0.0 ./ext/cargo-vendor/tokio-1.35.1/src/io/util/repeat.rs
wasmtime-14.0.4 ./ext/cargo-vendor/tokio-1.33.0/src/io/util/repeat.rs
wasmtime-14.0.3 ./ext/cargo-vendor/tokio-1.33.0/src/io/util/repeat.rs
wasmtime-14.0.1 ./ext/cargo-vendor/tokio-1.33.0/src/io/util/repeat.rs
wasmtime-14.0.0 ./ext/cargo-vendor/tokio-1.33.0/src/io/util/repeat.rs
wasmtime-13.0.0 ./ext/cargo-vendor/tokio-1.32.0/src/io/util/repeat.rs
wasmtime-12.0.1 ./ext/cargo-vendor/tokio-1.32.0/src/io/util/repeat.rs
wasmtime-12.0.0 ./ext/cargo-vendor/tokio-1.32.0/src/io/util/repeat.rs
wasmtime-11.0.0 ./ext/cargo-vendor/tokio-1.32.0/src/io/util/repeat.rs
wasmtime-10.0.1 ./ext/cargo-vendor/tokio-1.30.0/src/io/util/repeat.rs
wasmtime-10.0.0 ./ext/cargo-vendor/tokio-1.30.0/src/io/util/repeat.rs
wasmtime-9.0.4 ./ext/cargo-vendor/tokio-1.30.0/src/io/util/repeat.rs
wasmtime-9.0.1 ./ext/cargo-vendor/tokio-1.28.1/src/io/util/repeat.rs
wasmtime-8.0.0 ./ext/cargo-vendor/tokio-1.27.0/src/io/util/repeat.rs
wasmtime-7.0.0 ./ext/cargo-vendor/tokio-1.27.0/src/io/util/repeat.rs
wasmtime-6.0.1 ./ext/cargo-vendor/tokio-1.25.0/src/io/util/repeat.rs