pub struct AsyncPeekable<R, B = DefaultBuffer> { /* private fields */ }future only.Implementations§
Source§impl<R> AsyncPeekable<R>
impl<R> AsyncPeekable<R>
Sourcepub fn new(reader: R) -> Self
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);Sourcepub fn with_capacity(reader: R, capacity: usize) -> Self
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>
impl<R, B> AsyncPeekable<R, B>
Sourcepub fn with_buffer(reader: R) -> Selfwhere
B: Buffer,
pub fn with_buffer(reader: R) -> Selfwhere
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);Sourcepub fn with_capacity_and_buffer(reader: R, capacity: usize) -> Selfwhere
B: Buffer,
pub fn with_capacity_and_buffer(reader: R, capacity: usize) -> Selfwhere
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);Sourcepub fn consume(&mut self) -> Bwhere
B: Buffer,
pub fn consume(&mut self) -> Bwhere
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]);Sourcepub fn consume_in_place(&mut self)where
B: Buffer,
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]);Sourcepub fn get_mut(&mut self) -> (&[u8], &mut R)where
B: Buffer,
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());Sourcepub fn get_ref(&self) -> (&[u8], &R)where
B: Buffer,
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());Sourcepub fn into_components(self) -> (B, R)
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>
impl<R, B> AsyncPeekable<R, B>
Sourcepub fn peek<'a>(&'a mut self, buf: &'a mut [u8]) -> Peek<'a, R, B> ⓘwhere
Self: Unpin,
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]);
Sourcepub fn peek_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> PeekVectored<'a, R, B> ⓘwhere
Self: Unpin,
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.
Sourcepub fn peek_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> PeekExact<'a, R, B> ⓘwhere
Self: Unpin,
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]);Sourcepub fn peek_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> PeekToEnd<'a, R, B> ⓘwhere
Self: Unpin,
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]);
Sourcepub fn peek_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> PeekToString<'a, R, B> ⓘwhere
Self: Unpin,
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());Sourcepub fn fill_peek_buf(&mut self) -> FillPeekBuf<'_, R, B> ⓘ
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>
impl<R, B> AsyncPeek for AsyncPeekable<R, B>
Source§impl<R, B> AsyncRead for AsyncPeekable<R, B>
impl<R, B> AsyncRead for AsyncPeekable<R, B>
Source§impl<W, B> AsyncWrite for AsyncPeekable<W, B>where
W: AsyncWrite,
impl<W, B> AsyncWrite for AsyncPeekable<W, B>where
W: AsyncWrite,
Source§fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
Source§fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
Source§impl<R> From<(usize, R)> for AsyncPeekable<R>
impl<R> From<(usize, R)> for AsyncPeekable<R>
Source§impl<R> From<R> for AsyncPeekable<R>
impl<R> From<R> for AsyncPeekable<R>
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>
impl<R, B> RefUnwindSafe for AsyncPeekable<R, B>where
R: RefUnwindSafe,
B: RefUnwindSafe,
impl<R, B> Send for AsyncPeekable<R, B>
impl<R, B> Sync for AsyncPeekable<R, B>
impl<R, B> UnwindSafe for AsyncPeekable<R, B>where
R: UnwindSafe,
B: UnwindSafe,
Blanket Implementations§
Source§impl<R> AsyncPeekExt for Rwhere
R: AsyncRead,
impl<R> AsyncPeekExt for Rwhere
R: AsyncRead,
Source§fn peekable(self) -> AsyncPeekable<Self>where
Self: Sized,
fn peekable(self) -> AsyncPeekable<Self>where
Self: Sized,
future only.AsyncPeekable which will wrap the given reader.Source§fn peekable_with_capacity(self, capacity: usize) -> AsyncPeekable<Self>where
Self: Sized,
fn peekable_with_capacity(self, capacity: usize) -> AsyncPeekable<Self>where
Self: Sized,
future only.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>
fn peekable_with_buffer<B>(self) -> AsyncPeekable<Self, B>
future only.AsyncPeekable which will wrap the given reader. Read moreSource§fn peekable_with_capacity_and_buffer<B>(
self,
capacity: usize,
) -> AsyncPeekable<Self, B>
fn peekable_with_capacity_and_buffer<B>( self, capacity: usize, ) -> AsyncPeekable<Self, B>
future only.Source§impl<R> AsyncReadExt for R
impl<R> AsyncReadExt for R
Source§fn chain<R>(self, next: R) -> Chain<Self, R>
fn chain<R>(self, next: R) -> Chain<Self, R>
Source§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>where
Self: Unpin,
buf in asynchronous
manner, returning a future type. Read moreSource§fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectored<'a, Self>where
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectored<'a, Self>where
Self: Unpin,
AsyncRead into bufs using vectored
IO operations. Read moreSource§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>where
Self: Unpin,
buf,
returning an error if end of file (EOF) is hit sooner. Read moreSource§fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>where
Self: Unpin,
fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>where
Self: Unpin,
AsyncRead. Read moreSource§fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToString<'a, Self>where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToString<'a, Self>where
Self: Unpin,
AsyncRead. Read moreSource§impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
Source§fn flush(&mut self) -> Flush<'_, Self>where
Self: Unpin,
fn flush(&mut self) -> Flush<'_, Self>where
Self: Unpin,
AsyncWrite. Read moreSource§fn close(&mut self) -> Close<'_, Self>where
Self: Unpin,
fn close(&mut self) -> Close<'_, Self>where
Self: Unpin,
AsyncWrite.Source§fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self>where
Self: Unpin,
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self>where
Self: Unpin,
buf into the object. Read moreSource§fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>],
) -> WriteVectored<'a, Self>where
Self: Unpin,
fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>],
) -> WriteVectored<'a, Self>where
Self: Unpin,
bufs into the object using vectored
IO operations. Read more