pub struct RecvStream { /* private fields */ }
Expand description
A stream that can only be used to receive data
stop(0)
is implicitly called on drop unless:
§Cancellation
A read
method is said to be cancel-safe when dropping its future before the future becomes
ready cannot lead to loss of stream data. This is true of methods which succeed immediately when
any progress is made, and is not true of methods which might need to perform multiple reads
internally before succeeding. Each read
method documents whether it is cancel-safe.
§Common issues
§Data never received on a locally-opened stream
Peers are not notified of streams until they or a later-numbered stream are used to send data. If a bidirectional stream is locally opened but never used to send, then the peer may never see it. Application protocols should always arrange for the endpoint which will first transmit on a stream to be the endpoint responsible for opening it.
§Data never received on a remotely-opened stream
Verify that the stream you are receiving is the same one that the server is sending on, e.g. by
logging the id
of each. Streams are always accepted in the same order as they are created,
i.e. ascending order by StreamId
. For example, even if a sender first transmits on
bidirectional stream 1, the first stream yielded by Connection::accept_bi
on the receiver
will be bidirectional stream 0.
Implementations§
Source§impl RecvStream
impl RecvStream
Sourcepub async fn read(&mut self, buf: &mut [u8]) -> Result<Option<usize>, ReadError>
pub async fn read(&mut self, buf: &mut [u8]) -> Result<Option<usize>, ReadError>
Read data contiguously from the stream.
Yields the number of bytes read into buf
on success, or None
if the stream was finished.
This operation is cancel-safe.
Sourcepub async fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), ReadExactError>
pub async fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), ReadExactError>
Read an exact number of bytes contiguously from the stream.
See read()
for details. This operation is not cancel-safe.
Sourcepub fn poll_read(
&mut self,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<Result<usize, ReadError>>
pub fn poll_read( &mut self, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll<Result<usize, ReadError>>
Attempts to read from the stream into buf.
On success, returns Poll::Ready(Ok(num_bytes_read)) and places data in the buf. If no data was read, it implies that EOF has been reached.
If no data is available for reading, the method returns Poll::Pending and arranges for the current task (via cx.waker()) to receive a notification when the stream becomes readable or is closed.
Sourcepub async fn read_chunk(
&mut self,
max_length: usize,
ordered: bool,
) -> Result<Option<Chunk>, ReadError>
pub async fn read_chunk( &mut self, max_length: usize, ordered: bool, ) -> Result<Option<Chunk>, ReadError>
Read the next segment of data
Yields None
if the stream was finished. Otherwise, yields a segment of data and its
offset in the stream. If ordered
is true
, the chunk’s offset will be immediately after
the last data yielded by read()
or read_chunk()
. If ordered
is false
, segments may
be received in any order, and the Chunk
’s offset
field can be used to determine
ordering in the caller. Unordered reads are less prone to head-of-line blocking within a
stream, but require the application to manage reassembling the original data.
Slightly more efficient than read
due to not copying. Chunk boundaries do not correspond
to peer writes, and hence cannot be used as framing.
This operation is cancel-safe.
Sourcepub async fn read_chunks(
&mut self,
bufs: &mut [Bytes],
) -> Result<Option<usize>, ReadError>
pub async fn read_chunks( &mut self, bufs: &mut [Bytes], ) -> Result<Option<usize>, ReadError>
Read the next segments of data
Fills bufs
with the segments of data beginning immediately after the
last data yielded by read
or read_chunk
, or None
if the stream was
finished.
Slightly more efficient than read
due to not copying. Chunk boundaries
do not correspond to peer writes, and hence cannot be used as framing.
This operation is cancel-safe.
Sourcepub async fn read_to_end(
&mut self,
size_limit: usize,
) -> Result<Vec<u8>, ReadToEndError>
pub async fn read_to_end( &mut self, size_limit: usize, ) -> Result<Vec<u8>, ReadToEndError>
Convenience method to read all remaining data into a buffer
Fails with ReadToEndError::TooLong
on reading more than size_limit
bytes, discarding
all data read. Uses unordered reads to be more efficient than using AsyncRead
would
allow. size_limit
should be set to limit worst-case memory use.
If unordered reads have already been made, the resulting buffer may have gaps containing arbitrary data.
This operation is not cancel-safe.
Sourcepub fn stop(&mut self, error_code: VarInt) -> Result<(), ClosedStream>
pub fn stop(&mut self, error_code: VarInt) -> Result<(), ClosedStream>
Stop accepting data
Discards unread data and notifies the peer to stop transmitting. Once stopped, further
attempts to operate on a stream will yield ClosedStream
errors.
Sourcepub fn is_0rtt(&self) -> bool
pub fn is_0rtt(&self) -> bool
Check if this stream has been opened during 0-RTT.
In which case any non-idempotent request should be considered dangerous at the application level. Because read data is subject to replay attacks.
Sourcepub async fn received_reset(&mut self) -> Result<Option<VarInt>, ResetError>
pub async fn received_reset(&mut self) -> Result<Option<VarInt>, ResetError>
Completes when the stream has been reset by the peer or otherwise closed
Yields Some
with the reset error code when the stream is reset by the peer. Yields None
when the stream was previously stop()
ed, or when the stream was
finish()
ed by the peer and all data has been received, after
which it is no longer meaningful for the stream to be reset.
This operation is cancel-safe.
Trait Implementations§
Source§impl AsyncRead for RecvStream
impl AsyncRead for RecvStream
Source§impl AsyncRead for RecvStream
impl AsyncRead for RecvStream
Source§impl Debug for RecvStream
impl Debug for RecvStream
Auto Trait Implementations§
impl Freeze for RecvStream
impl RefUnwindSafe for RecvStream
impl Send for RecvStream
impl Sync for RecvStream
impl Unpin for RecvStream
impl UnwindSafe for RecvStream
Blanket Implementations§
Source§impl<R> AsyncReadExt for R
impl<R> AsyncReadExt for R
Source§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
Source§fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
Source§fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
Source§fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
Source§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
buf
. Read moreSource§fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
limit
bytes from it. Read moreSource§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> ReadExt for T
impl<T> ReadExt for T
Source§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
Source§fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
Source§fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
Source§fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
Source§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
buf
. Read moreSource§fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
limit
bytes from it. Read moreSource§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read more