AsyncPeekable

Struct AsyncPeekable 

Source
pub struct AsyncPeekable<R, B = DefaultBuffer> { /* private fields */ }
Available on crate feature future only.
Expand description

A wrapper around an AsyncRead types to make them support AsyncPeek methods.

Implementations§

Source§

impl<R> AsyncPeekable<R>

Source

pub fn new(reader: R) -> Self

Creates a new AsyncPeekable which will wrap the given reader.

The peek buffer will have the default capacity.

§Examples
use peekable::future::AsyncPeekable;

let reader = futures::io::Cursor::new(&b"hello"[..]);
let peekable = AsyncPeekable::new(reader);
Source

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

Creates a new peekable wrapper around the given reader with the specified capacity for the peek buffer.

§Examples
use peekable::future::AsyncPeekable;

let reader = futures::io::Cursor::new([0; 1024]);
let peekable = AsyncPeekable::with_capacity(reader, 1024);
Source§

impl<R, B> AsyncPeekable<R, B>

Source

pub fn with_buffer(reader: R) -> Self
where B: Buffer,

Creates a new AsyncPeekable which will wrap the given reader. This method allows you to specify a custom buffer type.

The peek buffer will have the default capacity.

§Examples
use peekable::future::AsyncPeekable;

let reader = futures::io::Cursor::new(&b"hello"[..]);
let peekable: AsyncPeekable<_, Vec<u8>> = AsyncPeekable::with_buffer(reader);
Source

pub fn with_capacity_and_buffer(reader: R, capacity: usize) -> Self
where B: Buffer,

Creates a new peekable wrapper around the given reader with the specified capacity for the peek buffer. This method allows you to specify a custom buffer type.

§Examples
use peekable::future::AsyncPeekable;
use std::vec::Vec;

let reader = futures::io::Cursor::new([0; 1024]);
let peekable: AsyncPeekable<_, Vec<u8>> = AsyncPeekable::with_capacity_and_buffer(reader, 1024);
Source

pub fn consume(&mut self) -> B
where B: Buffer,

Consumes the peek buffer and returns the buffer.

§Examples
use peekable::future::AsyncPeekable;

let reader = futures::io::Cursor::new([1, 2, 3, 4]);
let mut peekable: AsyncPeekable<_> = AsyncPeekable::from(reader);

let mut output = [0u8; 2];
let bytes = peekable.peek(&mut output).await.unwrap();
assert_eq!(bytes, 2);
assert_eq!(output, [1, 2]);

let buffer = peekable.consume();
assert_eq!(buffer.as_slice(), [1, 2].as_slice());

let mut output = [0u8; 2];
let bytes = peekable.peek(&mut output).await.unwrap();
assert_eq!(bytes, 2);
assert_eq!(output, [3, 4]);
Source

pub fn consume_in_place(&mut self)
where B: Buffer,

Consumes the peek buffer in place so that the peek buffer can be reused and avoid allocating.

§Examples
use peekable::future::AsyncPeekable;

let reader = futures::io::Cursor::new([1, 2, 3, 4]);
let mut peekable: AsyncPeekable<_> = AsyncPeekable::from(reader);

let mut output = [0u8; 2];
let bytes = peekable.peek(&mut output).await.unwrap();
assert_eq!(bytes, 2);
assert_eq!(output, [1, 2]);

peekable.consume_in_place();

let mut output = [0u8; 2];
let bytes = peekable.peek(&mut output).await.unwrap();
assert_eq!(bytes, 2);
assert_eq!(output, [3, 4]);
Source

pub fn get_mut(&mut self) -> (&[u8], &mut R)
where B: Buffer,

Returns the bytes already be peeked into memory and a mutable reference to the underlying reader.

WARNING: If you invoke AsyncRead or AsyncReadExt methods on the underlying reader, may lead to unexpected read behaviors.

§Examples
use peekable::future::AsyncPeekable;

let reader = futures::io::Cursor::new([1, 2, 3, 4]);
let mut peekable: AsyncPeekable<_> = AsyncPeekable::from(reader);

let mut output = [0u8; 2];
let bytes = peekable.peek(&mut output).await.unwrap();
assert_eq!(bytes, 2);
assert_eq!(output, [1, 2]);

let (buffer, reader) = peekable.get_mut();
assert_eq!(buffer, [1, 2].as_slice());
Source

pub fn get_ref(&self) -> (&[u8], &R)
where B: Buffer,

Returns the bytes already be peeked into memory and a reference to the underlying reader.

WARNING: If you invoke AsyncRead or AsyncReadExt methods on the underlying reader, may lead to unexpected read behaviors.

§Examples
use peekable::future::AsyncPeekable;

let reader = futures::io::Cursor::new([1, 2, 3, 4]);
let mut peekable: AsyncPeekable<_> = AsyncPeekable::from(reader);

let mut output = [0u8; 2];
let bytes = peekable.peek(&mut output).await.unwrap();
assert_eq!(bytes, 2);
assert_eq!(output, [1, 2]);

let (buffer, reader) = peekable.get_ref();
assert_eq!(buffer, [1, 2].as_slice());
Source

pub fn into_components(self) -> (B, R)

Consumes the AsyncPeekable, returning the a vec may contain the bytes already be peeked into memory and the wrapped reader.

§Examples
use peekable::future::AsyncPeekable;

let reader = futures::io::Cursor::new([1, 2, 3, 4]);
let mut peekable = AsyncPeekable::from(reader);

let mut output = [0u8; 2];
let bytes = peekable.peek(&mut output).await.unwrap();
assert_eq!(bytes, 2);
assert_eq!(output, [1, 2]);

let (buffer, reader) = peekable.into_components();
assert_eq!(buffer.as_slice(), [1, 2].as_slice());
Source§

impl<R, B> AsyncPeekable<R, B>
where R: AsyncRead + Unpin, B: Buffer,

Source

pub fn peek<'a>(&'a mut self, buf: &'a mut [u8]) -> Peek<'a, R, B>
where Self: Unpin,

Tries to peek some bytes directly into the given buf in asynchronous manner, returning a future type.

The returned future will resolve to the number of bytes read once the read operation is completed.

§Examples
use futures::io::{AsyncReadExt, Cursor};
use peekable::future::AsyncPeekExt;


let mut peekable = Cursor::new([1, 2, 3, 4]).peekable();
let mut output = [0u8; 5];

let bytes = peekable.peek(&mut output[..3]).await?;

// This is only guaranteed to be 4 because `&[u8]` is a synchronous
// reader. In a real system you could get anywhere from 1 to
// `output.len()` bytes in a single read.
assert_eq!(bytes, 3);
assert_eq!(output, [1, 2, 3, 0, 0]);

// you can peek mutiple times

let bytes = peekable.peek(&mut output[..]).await?;
assert_eq!(bytes, 4);
assert_eq!(output, [1, 2, 3, 4, 0]);

// you can read after peek
let mut output = [0u8; 5];
let bytes = peekable.read(&mut output[..2]).await?;
assert_eq!(bytes, 2);
assert_eq!(output, [1, 2, 0, 0, 0]);

// peek after read
let mut output = [0u8; 5];
let bytes = peekable.peek(&mut output[..]).await?;
assert_eq!(bytes, 2);
assert_eq!(output, [3, 4, 0, 0, 0]);
Source

pub fn peek_vectored<'a>( &'a mut self, bufs: &'a mut [IoSliceMut<'a>], ) -> PeekVectored<'a, R, B>
where Self: Unpin,

Creates a future which will peek from the AsyncPeek into bufs using vectored IO operations.

The returned future will resolve to the number of bytes read once the read operation is completed.

Source

pub fn peek_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> PeekExact<'a, R, B>
where Self: Unpin,

Creates a future which will peek exactly enough bytes to fill buf, returning an error if end of file (EOF) is hit sooner.

The returned future will resolve once the read operation is completed.

In the case of an error the buffer and the object will be discarded, with the error yielded.

§Examples
use futures::io::{AsyncReadExt, Cursor};
use peekable::future::AsyncPeekExt;

let mut peekable = Cursor::new([1, 2, 3, 4]).peekable();
let mut output = [0u8; 4];

peekable.peek_exact(&mut output).await?;

assert_eq!(output, [1, 2, 3, 4]);

// read after peek
let mut output = [0u8; 2];

peekable.read_exact(&mut output[..]).await?;

assert_eq!(output, [1, 2]);

// peek after read
let mut output = [0u8; 2];
peekable.peek_exact(&mut output).await?;

assert_eq!(output, [3, 4]);
§EOF is hit before buf is filled
use futures::io::{self, AsyncReadExt, Cursor};
use peekable::future::AsyncPeekExt;

let mut peekable = Cursor::new([1, 2, 3, 4]).peekable();
let mut output = [0u8; 5];

let result = peekable.peek_exact(&mut output).await;
assert_eq!(
  result.unwrap_err().kind(),
  std::io::ErrorKind::UnexpectedEof
);

let result = peekable.peek_exact(&mut output[..4]).await;
assert!(result.is_ok());
assert_eq!(output, [1, 2, 3, 4, 0]);
Source

pub fn peek_to_end<'a>( &'a mut self, buf: &'a mut Vec<u8>, ) -> PeekToEnd<'a, R, B>
where Self: Unpin,

Creates a future which will peek all the bytes from this AsyncPeek.

On success the total number of bytes peek is returned.

§Examples
use futures::io::{AsyncReadExt, Cursor};
use peekable::future::AsyncPeekExt;

let mut peekable = Cursor::new([1, 2, 3, 4]).peekable();
let mut output = Vec::with_capacity(4);

let bytes = peekable.peek_to_end(&mut output).await?;

assert_eq!(bytes, 4);
assert_eq!(output, vec![1, 2, 3, 4]);

// read after peek
let mut output = Vec::with_capacity(4);

let bytes = peekable.read_to_end(&mut output).await?;

assert_eq!(bytes, 4);
assert_eq!(output, vec![1, 2, 3, 4]);
Source

pub fn peek_to_string<'a>( &'a mut self, buf: &'a mut String, ) -> PeekToString<'a, R, B>
where Self: Unpin,

Creates a future which will peek all the bytes from this AsyncPeek.

On success the total number of bytes peek is returned.

§Examples
use futures::io::{AsyncReadExt, Cursor};
use peekable::future::AsyncPeekExt;

let mut peekable = Cursor::new(&b"1234"[..]).peekable();
let mut buffer = String::with_capacity(4);

let bytes = peekable.peek_to_string(&mut buffer).await?;

assert_eq!(bytes, 4);
assert_eq!(buffer, String::from("1234"));

// read after peek
let mut buffer = String::with_capacity(4);
let bytes = peekable.peek_to_string(&mut buffer).await?;

assert_eq!(bytes, 4);
assert_eq!(buffer, String::from("1234"));

// peek invalid utf-8
let mut peekable = Cursor::new([255; 4]).peekable();
let mut buffer = String::with_capacity(4);
assert!(peekable.peek_to_string(&mut buffer).await.is_err());
Source

pub fn fill_peek_buf(&mut self) -> FillPeekBuf<'_, R, B>

Try to fill the peek buffer with more data. Returns the number of bytes peeked.

§Examples
use futures::io::Cursor;
use futures::AsyncReadExt;
use peekable::future::AsyncPeekExt;


let mut peekable = Cursor::new([1, 2, 3, 4]).peekable_with_capacity(5);
let mut output = [0u8; 4];

peekable.peek_exact(&mut output[..1]).await.unwrap();
assert_eq!(output, [1, 0, 0, 0]);

let bytes = peekable.fill_peek_buf().await.unwrap();
assert_eq!(bytes, 3);

let bytes = peekable.peek(&mut output).await.unwrap();
assert_eq!(output, [1, 2, 3, 4].as_slice());

let readed = peekable.read(&mut output).await.unwrap();
assert_eq!(readed, 4);

Trait Implementations§

Source§

impl<R, B> AsyncPeek for AsyncPeekable<R, B>
where R: AsyncRead, B: Buffer,

Source§

fn poll_peek( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll<Result<usize>>

Attempt to peek from the AsyncPeek into buf. Read more
Source§

fn poll_peek_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &mut [IoSliceMut<'_>], ) -> Poll<Result<usize>>

Attempt to read from the AsyncPeek into bufs using vectored IO operations. Read more
Source§

impl<R, B> AsyncRead for AsyncPeekable<R, B>
where R: AsyncRead, B: Buffer,

Source§

fn poll_read( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll<Result<usize>>

Attempt to read from the AsyncRead into buf. Read more
Source§

fn poll_read_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &mut [IoSliceMut<'_>], ) -> Poll<Result<usize, Error>>

Attempt to read from the AsyncRead into bufs using vectored IO operations. Read more
Source§

impl<W, B> AsyncWrite for AsyncPeekable<W, B>
where W: AsyncWrite,

Source§

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Attempt to close the object. Read more
Source§

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Attempt to flush the object, ensuring that any buffered data reach their destination. Read more
Source§

fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize>>

Attempt to write bytes from buf into the object. Read more
Source§

fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>], ) -> Poll<Result<usize, Error>>

Attempt to write bytes from bufs into the object using vectored IO operations. Read more
Source§

impl<R: Debug, B: Debug> Debug for AsyncPeekable<R, B>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<R> From<(usize, R)> for AsyncPeekable<R>

Source§

fn from((cap, reader): (usize, R)) -> Self

Converts to this type from the input type.
Source§

impl<R> From<R> for AsyncPeekable<R>

Source§

fn from(reader: R) -> Self

Converts to this type from the input type.
Source§

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

Auto Trait Implementations§

§

impl<R, B> Freeze for AsyncPeekable<R, B>
where R: Freeze, B: Freeze,

§

impl<R, B> RefUnwindSafe for AsyncPeekable<R, B>

§

impl<R, B> Send for AsyncPeekable<R, B>
where R: Send, B: Send,

§

impl<R, B> Sync for AsyncPeekable<R, B>
where R: Sync, B: Sync,

§

impl<R, B> UnwindSafe for AsyncPeekable<R, B>
where R: UnwindSafe, B: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<R> AsyncPeekExt for R
where R: AsyncRead,

Source§

fn peekable(self) -> AsyncPeekable<Self>
where Self: Sized,

Available on crate feature future only.
Creates a new AsyncPeekable which will wrap the given reader.
Source§

fn peekable_with_capacity(self, capacity: usize) -> AsyncPeekable<Self>
where Self: Sized,

Available on crate feature future only.
Wraps a AsyncRead type in a AsyncPeekable which provides a peek related methods with a specified capacity.
Source§

fn peekable_with_buffer<B>(self) -> AsyncPeekable<Self, B>
where Self: Sized, B: Buffer,

Available on crate feature future only.
Creates a new AsyncPeekable which will wrap the given reader. Read more
Source§

fn peekable_with_capacity_and_buffer<B>( self, capacity: usize, ) -> AsyncPeekable<Self, B>
where Self: Sized, B: Buffer,

Available on crate feature future only.
Wraps a AsyncRead type in a AsyncPeekable which provides a peek related methods with a specified capacity. Read more
Source§

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

Source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where Self: Sized, R: AsyncRead,

Creates an adaptor which will chain this stream with another. Read more
Source§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>
where Self: Unpin,

Tries to read some bytes directly into the given buf in asynchronous manner, returning a future type. Read more
Source§

fn read_vectored<'a>( &'a mut self, bufs: &'a mut [IoSliceMut<'a>], ) -> ReadVectored<'a, Self>
where Self: Unpin,

Creates a future which will read from the AsyncRead into bufs using vectored IO operations. Read more
Source§

fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>
where Self: Unpin,

Creates a future which will read exactly enough bytes to fill buf, returning an error if end of file (EOF) is hit sooner. Read more
Source§

fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>
where Self: Unpin,

Creates a future which will read all the bytes from this AsyncRead. Read more
Source§

fn read_to_string<'a>( &'a mut self, buf: &'a mut String, ) -> ReadToString<'a, Self>
where Self: Unpin,

Creates a future which will read all the bytes from this AsyncRead. Read more
Source§

fn split(self) -> (ReadHalf<Self>, WriteHalf<Self>)
where Self: Sized + AsyncWrite,

Helper method for splitting this read/write object into two halves. Read more
Source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an AsyncRead adapter which will read at most limit bytes from the underlying reader. Read more
Source§

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

Source§

fn flush(&mut self) -> Flush<'_, Self>
where Self: Unpin,

Creates a future which will entirely flush this AsyncWrite. Read more
Source§

fn close(&mut self) -> Close<'_, Self>
where Self: Unpin,

Creates a future which will entirely close this AsyncWrite.
Source§

fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self>
where Self: Unpin,

Creates a future which will write bytes from buf into the object. Read more
Source§

fn write_vectored<'a>( &'a mut self, bufs: &'a [IoSlice<'a>], ) -> WriteVectored<'a, Self>
where Self: Unpin,

Creates a future which will write bytes from bufs into the object using vectored IO operations. Read more
Source§

fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAll<'a, Self>
where Self: Unpin,

Write data into this object. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.