]>
git.proxmox.com Git - rustc.git/blob - vendor/futures-util/src/io/repeat.rs
1 use futures_core
::ready
;
2 use futures_core
::task
::{Context, Poll}
;
3 use futures_io
::{AsyncRead, IoSliceMut}
;
8 /// Reader for the [`repeat()`] function.
9 #[must_use = "readers do nothing unless polled"]
14 /// Creates an instance of a reader that infinitely repeats one byte.
16 /// All reads from this reader will succeed by filling the specified buffer with
22 /// # futures::executor::block_on(async {
23 /// use futures::io::{self, AsyncReadExt};
25 /// let mut buffer = [0; 3];
26 /// let mut reader = io::repeat(0b101);
27 /// reader.read_exact(&mut buffer).await.unwrap();
28 /// assert_eq!(buffer, [0b101, 0b101, 0b101]);
29 /// # Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap();
31 pub fn repeat(byte
: u8) -> Repeat
{
35 impl AsyncRead
for Repeat
{
41 ) -> Poll
<io
::Result
<usize>> {
42 for slot
in &mut *buf
{
45 Poll
::Ready(Ok(buf
.len()))
49 fn poll_read_vectored(
50 mut self: Pin
<&mut Self>,
52 bufs
: &mut [IoSliceMut
<'_
>],
53 ) -> Poll
<io
::Result
<usize>> {
56 nwritten
+= ready
!(self.as_mut().poll_read(cx
, buf
))?
;
58 Poll
::Ready(Ok(nwritten
))
62 impl fmt
::Debug
for Repeat
{
63 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
64 f
.pad("Repeat { .. }")