[][src]Struct partial_io::PartialAsyncRead

pub struct PartialAsyncRead<R> { /* fields omitted */ }

A wrapper that breaks inner AsyncRead instances up according to the provided iterator.

Available with the futures03 feature for futures traits, and with the tokio1 feature for tokio traits.

Examples

This example uses tokio.

use partial_io::{PartialAsyncRead, PartialOp};
use std::io::{self, Cursor};
use tokio::io::AsyncReadExt;

#[tokio::main]
async fn main() -> io::Result<()> {
    let reader = Cursor::new(vec![1, 2, 3, 4]);
    // Sequential calls to `poll_read()` and the other `poll_` methods simulate the following behavior:
    let iter = vec![
        PartialOp::Err(io::ErrorKind::WouldBlock),   // A not-ready state.
        PartialOp::Limited(2),                       // Only allow 2 bytes to be read.
        PartialOp::Err(io::ErrorKind::InvalidData),  // Error from the underlying stream.
        PartialOp::Unlimited,                        // Allow as many bytes to be read as possible.
    ];
    let mut partial_reader = PartialAsyncRead::new(reader, iter);
    let mut out = vec![0; 256];

    // This causes poll_read to be called twice, yielding after the first call (WouldBlock).
    assert_eq!(partial_reader.read(&mut out).await?, 2, "first read with Limited(2)");
    assert_eq!(&out[..4], &[1, 2, 0, 0]);

    // This next call returns an error.
    assert_eq!(
        partial_reader.read(&mut out[2..]).await.unwrap_err().kind(),
        io::ErrorKind::InvalidData,
    );

    // And this one causes the last two bytes to be written.
    assert_eq!(partial_reader.read(&mut out[2..]).await?, 2, "second read with Unlimited");
    assert_eq!(&out[..4], &[1, 2, 3, 4]);

    Ok(())
}

Implementations

impl<R> PartialAsyncRead<R>[src]

pub fn new<I>(inner: R, iter: I) -> Self where
    I: IntoIterator<Item = PartialOp> + 'static,
    I::IntoIter: Send
[src]

Creates a new PartialAsyncRead wrapper over the reader with the specified PartialOps.

pub fn set_ops<I>(&mut self, iter: I) -> &mut Self where
    I: IntoIterator<Item = PartialOp> + 'static,
    I::IntoIter: Send
[src]

Sets the PartialOps for this reader.

pub fn pin_set_ops<I>(self: Pin<&mut Self>, iter: I) -> Pin<&mut Self> where
    I: IntoIterator<Item = PartialOp> + 'static,
    I::IntoIter: Send
[src]

Sets the PartialOps for this reader in a pinned context.

pub fn get_ref(&self) -> &R[src]

Returns a shared reference to the underlying reader.

pub fn get_mut(&mut self) -> &mut R[src]

Returns a mutable reference to the underlying reader.

pub fn pin_get_mut(self: Pin<&mut Self>) -> Pin<&mut R>[src]

Returns a pinned mutable reference to the underlying reader.

pub fn into_inner(self) -> R[src]

Consumes this wrapper, returning the underlying reader.

Trait Implementations

impl<R> AsyncBufRead for PartialAsyncRead<R> where
    R: AsyncBufRead, 
[src]

impl<R> AsyncBufRead for PartialAsyncRead<R> where
    R: AsyncBufRead
[src]

impl<R> AsyncRead for PartialAsyncRead<R> where
    R: AsyncRead, 
[src]

impl<R> AsyncRead for PartialAsyncRead<R> where
    R: AsyncRead
[src]

impl<R> AsyncSeek for PartialAsyncRead<R> where
    R: AsyncSeek, 
[src]

This is a forwarding impl to support duplex structs.

impl<R> AsyncSeek for PartialAsyncRead<R> where
    R: AsyncSeek
[src]

This is a forwarding impl to support duplex structs.

impl<R> AsyncWrite for PartialAsyncRead<R> where
    R: AsyncWrite, 
[src]

This is a forwarding impl to support duplex structs.

impl<R> AsyncWrite for PartialAsyncRead<R> where
    R: AsyncWrite
[src]

This is a forwarding impl to support duplex structs.

impl<R> Debug for PartialAsyncRead<R> where
    R: Debug
[src]

impl<'pin, R> Unpin for PartialAsyncRead<R> where
    __PartialAsyncRead<'pin, R>: Unpin
[src]

Auto Trait Implementations

impl<R> !RefUnwindSafe for PartialAsyncRead<R>[src]

impl<R> Send for PartialAsyncRead<R> where
    R: Send
[src]

impl<R> !Sync for PartialAsyncRead<R>[src]

impl<R> !UnwindSafe for PartialAsyncRead<R>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<R> AsyncBufReadExt for R where
    R: AsyncBufRead + ?Sized

impl<R> AsyncBufReadExt for R where
    R: AsyncBufRead + ?Sized
[src]

impl<R> AsyncReadExt for R where
    R: AsyncRead + ?Sized

impl<R> AsyncReadExt for R where
    R: AsyncRead + ?Sized
[src]

impl<S> AsyncSeekExt for S where
    S: AsyncSeek + ?Sized

impl<S> AsyncSeekExt for S where
    S: AsyncSeek + ?Sized
[src]

impl<W> AsyncWriteExt for W where
    W: AsyncWrite + ?Sized

impl<W> AsyncWriteExt for W where
    W: AsyncWrite + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,