Struct ReaderStream

pub struct ReaderStream<R> { /* private fields */ }

Convert an AsyncRead into a Stream of byte chunks.

This stream is fused. It performs the inverse operation of StreamReader.

Example

# #[tokio::main(flavor = "current_thread")]
# async fn main() -> std::io::Result<()> {
use tokio_stream::StreamExt;
use tokio_util::io::ReaderStream;

// Create a stream of data.
let data = b"hello, world!";
let mut stream = ReaderStream::new(&data[..]);

// Read all of the chunks into a vector.
let mut stream_contents = Vec::new();
while let Some(chunk) = stream.next().await {
   stream_contents.extend_from_slice(&chunk?);
}

// Once the chunks are concatenated, we should have the
// original data.
assert_eq!(stream_contents, data);
# Ok(())
# }

Implementations

impl<R: AsyncRead> ReaderStream<R>

fn new(reader: R) -> Self

Convert an AsyncRead into a Stream with item type Result<Bytes, std::io::Error>.

Currently, the default capacity 4096 bytes (4 KiB). This capacity is not part of the semver contract and may be tweaked in future releases without requiring a major version bump.

fn with_capacity(reader: R, capacity: usize) -> Self

Convert an AsyncRead into a Stream with item type Result<Bytes, std::io::Error>, with a specific read buffer initial capacity.

Trait Implementations

impl<'__pin, R> Unpin for ReaderStream<R> where PinnedFieldsOf<__Origin<'__pin, R>>: Unpin,

impl<R: AsyncRead> Stream for ReaderStream<R>

type Item = Result<Bytes, Error>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>

impl<R: Debug> Debug for ReaderStream<R>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Auto Trait Implementations

impl<R> Freeze for ReaderStream<R> where Option<R>: Freeze,

impl<R> RefUnwindSafe for ReaderStream<R> where Option<R>: RefUnwindSafe,

impl<R> Send for ReaderStream<R> where Option<R>: Send,

impl<R> Sync for ReaderStream<R> where Option<R>: Sync,

impl<R> UnsafeUnpin for ReaderStream<R> where Option<R>: UnsafeUnpin,

impl<R> UnwindSafe for ReaderStream<R> where Option<R>: UnwindSafe,

Blanket Implementations

impl<S, T, E> TryStream for ReaderStream<R> where S: Stream<Item = Result<T, E>> + ?Sized,

type Ok = T;
type Error = E;
fn try_poll_next(self: Pin<&mut S>, cx: &mut Context<'_>) -> Poll<Option<Result<<S as TryStream>::Ok, <S as TryStream>::Error>>>

impl<T> Any for ReaderStream<R> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for ReaderStream<R> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for ReaderStream<R> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for ReaderStream<R>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into<U> for ReaderStream<R> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for ReaderStream<R> where U: Into<T>,

type Error = Infallible;
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto<U> for ReaderStream<R> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>