[][src]Struct tokio_util::io::StreamReader

pub struct StreamReader<S, B> { /* fields omitted */ }
This is supported on crate feature io only.

Convert a Stream of byte chunks into an AsyncRead.

This type performs the inverse operation of ReaderStream.

Example

use bytes::Bytes;
use tokio::io::{AsyncReadExt, Result};
use tokio_util::io::StreamReader;

// Create a stream from an iterator.
let stream = tokio::stream::iter(vec![
    Result::Ok(Bytes::from_static(&[0, 1, 2, 3])),
    Result::Ok(Bytes::from_static(&[4, 5, 6, 7])),
    Result::Ok(Bytes::from_static(&[8, 9, 10, 11])),
]);

// Convert it to an AsyncRead.
let mut read = StreamReader::new(stream);

// Read five bytes from the stream.
let mut buf = [0; 5];
read.read_exact(&mut buf).await?;
assert_eq!(buf, [0, 1, 2, 3, 4]);

// Read the rest of the current chunk.
assert_eq!(read.read(&mut buf).await?, 3);
assert_eq!(&buf[..3], [5, 6, 7]);

// Read the next chunk.
assert_eq!(read.read(&mut buf).await?, 4);
assert_eq!(&buf[..4], [8, 9, 10, 11]);

// We have now reached the end.
assert_eq!(read.read(&mut buf).await?, 0);

Implementations

impl<S, B, E> StreamReader<S, B> where
    S: Stream<Item = Result<B, E>>,
    B: Buf,
    E: Into<Error>, 
[src]

pub fn new(stream: S) -> Self[src]

This is supported on crate feature io only.

Convert a stream of byte chunks into an AsyncRead.

The item should be a Result with the ok variant being something that implements the Buf trait (e.g. Vec<u8> or Bytes). The error should be convertible into an io error.

Trait Implementations

impl<S, B, E> AsyncBufRead for StreamReader<S, B> where
    S: Stream<Item = Result<B, E>>,
    B: Buf,
    E: Into<Error>, 
[src]

impl<S, B, E> AsyncRead for StreamReader<S, B> where
    S: Stream<Item = Result<B, E>>,
    B: Buf,
    E: Into<Error>, 
[src]

impl<S: Debug, B: Debug> Debug for StreamReader<S, B>[src]

impl<'__pin, S, B> Unpin for StreamReader<S, B> where
    __Origin<'__pin, S, B>: Unpin
[src]

Auto Trait Implementations

impl<S, B> RefUnwindSafe for StreamReader<S, B> where
    B: RefUnwindSafe,
    S: RefUnwindSafe

impl<S, B> Send for StreamReader<S, B> where
    B: Send,
    S: Send

impl<S, B> Sync for StreamReader<S, B> where
    B: Sync,
    S: Sync

impl<S, B> UnwindSafe for StreamReader<S, B> where
    B: UnwindSafe,
    S: UnwindSafe

Blanket Implementations

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

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

impl<R> AsyncReadExt for R where
    R: AsyncRead + ?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> Tokio02AsyncReadCompatExt for T where
    T: AsyncRead
[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.