webtransport_generic/
recv.rs

1use std::{
2    future::{poll_fn, Future},
3    task::{Context, Poll},
4};
5
6use bytes::{BufMut, Bytes};
7
8use crate::ErrorCode;
9
10/// A trait describing the "receive" actions of a QUIC stream.
11pub trait RecvStream: Unpin + Send {
12    type Error: ErrorCode;
13
14    /// Send a `STOP_SENDING` QUIC code.
15    fn close(self, code: u32);
16
17    /// Attempt to read from the stream into the given buffer.
18    fn poll_read_buf<B: BufMut>(
19        &mut self,
20        cx: &mut Context<'_>,
21        buf: &mut B,
22    ) -> Poll<Result<usize, Self::Error>>;
23
24    // A helper future that calls poll_read_buf.
25    fn read_buf<B: BufMut + Send>(
26        &mut self,
27        buf: &mut B,
28    ) -> impl Future<Output = Result<usize, Self::Error>> + Send {
29        poll_fn(move |cx| self.poll_read_buf(cx, buf))
30    }
31
32    /// Attempt to write the given buffer to the stream.
33    fn poll_read_chunk(&mut self, cx: &mut Context<'_>)
34        -> Poll<Result<Option<Bytes>, Self::Error>>;
35
36    // A helper future that calls poll_read_chunk.
37    fn read_chunk(&mut self) -> impl Future<Output = Result<Option<Bytes>, Self::Error>> + Send {
38        poll_fn(|cx| self.poll_read_chunk(cx))
39    }
40}