-
Notifications
You must be signed in to change notification settings - Fork 633
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use our own io::{Empy, Repeat, Sink}
- Loading branch information
Showing
6 changed files
with
211 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use futures_core::task::{Context, Poll}; | ||
use futures_io::{AsyncBufRead, AsyncRead, Initializer}; | ||
use std::fmt; | ||
use std::io; | ||
use std::pin::Pin; | ||
|
||
/// Stream for the [`empty()`] function. | ||
#[must_use = "streams do nothing unless polled"] | ||
pub struct Empty { | ||
_priv: (), | ||
} | ||
|
||
/// Constructs a new handle to an empty reader. | ||
/// | ||
/// All reads from the returned reader will return `Poll::Ready(Ok(0))`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// A slightly sad example of not reading anything into a buffer: | ||
/// | ||
/// ``` | ||
/// # futures::executor::block_on(async { | ||
/// use futures::io::{self, AsyncReadExt}; | ||
/// | ||
/// let mut buffer = String::new(); | ||
/// io::empty().read_to_string(&mut buffer).await?; | ||
/// assert!(buffer.is_empty()); | ||
/// # Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap(); | ||
/// ``` | ||
pub fn empty() -> Empty { | ||
Empty { _priv: () } | ||
} | ||
|
||
impl AsyncRead for Empty { | ||
#[inline] | ||
fn poll_read( | ||
self: Pin<&mut Self>, | ||
_: &mut Context<'_>, | ||
_: &mut [u8], | ||
) -> Poll<io::Result<usize>> { | ||
Poll::Ready(Ok(0)) | ||
} | ||
|
||
#[inline] | ||
unsafe fn initializer(&self) -> Initializer { | ||
Initializer::nop() | ||
} | ||
} | ||
|
||
impl AsyncBufRead for Empty { | ||
#[inline] | ||
fn poll_fill_buf(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<&[u8]>> { | ||
Poll::Ready(Ok(&[])) | ||
} | ||
#[inline] | ||
fn consume(self: Pin<&mut Self>, _: usize) {} | ||
} | ||
|
||
impl fmt::Debug for Empty { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.pad("Empty { .. }") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use futures_core::task::{Context, Poll}; | ||
use futures_io::{AsyncRead, Initializer, IoSliceMut}; | ||
use std::fmt; | ||
use std::io; | ||
use std::pin::Pin; | ||
|
||
/// Stream for the [`repeat()`] function. | ||
#[must_use = "streams do nothing unless polled"] | ||
pub struct Repeat { | ||
byte: u8, | ||
} | ||
|
||
/// Creates an instance of a reader that infinitely repeats one byte. | ||
/// | ||
/// All reads from this reader will succeed by filling the specified buffer with | ||
/// the given byte. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// # futures::executor::block_on(async { | ||
/// use futures::io::{self, AsyncReadExt}; | ||
/// | ||
/// let mut buffer = [0; 3]; | ||
/// io::repeat(0b101).read_exact(&mut buffer).await.unwrap(); | ||
/// assert_eq!(buffer, [0b101, 0b101, 0b101]); | ||
/// # Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap(); | ||
/// ``` | ||
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 slot in &mut *buf { | ||
*slot = self.byte; | ||
} | ||
Poll::Ready(Ok(buf.len())) | ||
} | ||
|
||
#[inline] | ||
fn poll_read_vectored( | ||
mut self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
bufs: &mut [IoSliceMut<'_>], | ||
) -> Poll<io::Result<usize>> { | ||
let mut nwritten = 0; | ||
for buf in bufs { | ||
nwritten += ready!(self.as_mut().poll_read(cx, buf))?; | ||
} | ||
Poll::Ready(Ok(nwritten)) | ||
} | ||
|
||
#[inline] | ||
unsafe fn initializer(&self) -> Initializer { | ||
Initializer::nop() | ||
} | ||
} | ||
|
||
impl fmt::Debug for Repeat { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.pad("Repeat { .. }") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use futures_core::task::{Context, Poll}; | ||
use futures_io::{AsyncWrite, IoSlice}; | ||
use std::fmt; | ||
use std::io; | ||
use std::pin::Pin; | ||
|
||
/// Stream for the [`sink()`] function. | ||
#[must_use = "streams do nothing unless polled"] | ||
pub struct Sink { | ||
_priv: (), | ||
} | ||
|
||
/// Creates an instance of a writer which will successfully consume all data. | ||
/// | ||
/// All calls to `poll_write` on the returned instance will return `Poll::Ready(Ok(buf.len()))` | ||
/// and the contents of the buffer will not be inspected. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```rust | ||
/// # futures::executor::block_on(async { | ||
/// use futures::io::{self, AsyncWriteExt}; | ||
/// | ||
/// let buffer = vec![1, 2, 3, 5, 8]; | ||
/// let num_bytes = io::sink().write(&buffer).await?; | ||
/// assert_eq!(num_bytes, 5); | ||
/// # Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap(); | ||
/// ``` | ||
pub fn sink() -> Sink { | ||
Sink { _priv: () } | ||
} | ||
|
||
impl AsyncWrite for Sink { | ||
#[inline] | ||
fn poll_write( | ||
self: Pin<&mut Self>, | ||
_: &mut Context<'_>, | ||
buf: &[u8], | ||
) -> Poll<io::Result<usize>> { | ||
Poll::Ready(Ok(buf.len())) | ||
} | ||
|
||
#[inline] | ||
fn poll_write_vectored( | ||
self: Pin<&mut Self>, | ||
_: &mut Context<'_>, | ||
bufs: &[IoSlice<'_>], | ||
) -> Poll<io::Result<usize>> { | ||
Poll::Ready(Ok(bufs.iter().map(|b| b.len()).sum())) | ||
} | ||
|
||
#[inline] | ||
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> { | ||
Poll::Ready(Ok(())) | ||
} | ||
#[inline] | ||
fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> { | ||
Poll::Ready(Ok(())) | ||
} | ||
} | ||
|
||
impl fmt::Debug for Sink { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.pad("Sink { .. }") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters