webtransport_generic/
send.rs

1use std::{
2    future::{poll_fn, Future},
3    task::{Context, Poll},
4};
5
6use bytes::Buf;
7
8use crate::ErrorCode;
9
10/// A trait describing the "send" actions of a QUIC stream.
11pub trait SendStream: Unpin + Send {
12    type Error: ErrorCode;
13
14    /// Set the stream's priority relative to other streams on the same connection.
15    /// The **highest** priority stream with pending data will be sent first.
16    /// Zero is the default value.
17    fn priority(&mut self, order: i32);
18
19    /// Send a QUIC reset code.
20    fn close(self, code: u32);
21
22    /// Attempt to write some of the given buffer to the stream.
23    fn poll_write(&mut self, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize, Self::Error>>;
24
25    // A helper future that calls poll_write
26    fn write(&mut self, buf: &[u8]) -> impl Future<Output = Result<usize, Self::Error>> + Send {
27        poll_fn(|cx| self.poll_write(cx, buf))
28    }
29
30    fn poll_write_buf<B: Buf>(
31        &mut self,
32        cx: &mut Context<'_>,
33        buf: &mut B,
34    ) -> Poll<Result<usize, Self::Error>>;
35
36    // A helper future that calls poll_write
37    fn write_buf<B: Buf + Send>(
38        &mut self,
39        buf: &mut B,
40    ) -> impl Future<Output = Result<usize, Self::Error>> + Send {
41        poll_fn(|cx| self.poll_write_buf(cx, buf))
42    }
43
44    // TODO add write_chunk to avoid making a copy?
45}