passcod_web_transport_quinn/
send.rs1use std::{
2 io,
3 pin::Pin,
4 task::{Context, Poll},
5};
6
7use bytes::Bytes;
8
9use crate::{ClosedStream, StoppedError, WriteError};
10
11#[derive(Debug)]
16pub struct SendStream {
17 stream: quinn::SendStream,
18}
19
20impl SendStream {
21 pub(crate) fn new(stream: quinn::SendStream) -> Self {
22 Self { stream }
23 }
24
25 pub fn reset(&mut self, code: u32) -> Result<(), ClosedStream> {
28 let code = web_transport_proto::error_to_http3(code);
29 let code = quinn::VarInt::try_from(code).unwrap();
30 self.stream.reset(code).map_err(Into::into)
31 }
32
33 pub async fn stopped(&mut self) -> Result<Option<u32>, StoppedError> {
36 Ok(match self.stream.stopped().await? {
37 Some(code) => web_transport_proto::error_from_http3(code.into_inner()),
38 None => None,
39 })
40 }
41
42 pub async fn write(&mut self, buf: &[u8]) -> Result<usize, WriteError> {
46 self.stream.write(buf).await.map_err(Into::into)
47 }
48
49 pub async fn write_all(&mut self, buf: &[u8]) -> Result<(), WriteError> {
51 self.stream.write_all(buf).await.map_err(Into::into)
52 }
53
54 pub async fn write_chunks(
56 &mut self,
57 bufs: &mut [Bytes],
58 ) -> Result<quinn_proto::Written, WriteError> {
59 self.stream.write_chunks(bufs).await.map_err(Into::into)
60 }
61
62 pub async fn write_chunk(&mut self, buf: Bytes) -> Result<(), WriteError> {
64 self.stream.write_chunk(buf).await.map_err(Into::into)
65 }
66
67 pub async fn write_all_chunks(&mut self, bufs: &mut [Bytes]) -> Result<(), WriteError> {
69 self.stream.write_all_chunks(bufs).await.map_err(Into::into)
70 }
71
72 pub fn finish(&mut self) -> Result<(), ClosedStream> {
74 self.stream.finish().map_err(Into::into)
75 }
76
77 pub fn set_priority(&self, order: i32) -> Result<(), ClosedStream> {
78 self.stream.set_priority(order).map_err(Into::into)
79 }
80
81 pub fn priority(&self) -> Result<i32, ClosedStream> {
82 self.stream.priority().map_err(Into::into)
83 }
84}
85
86impl tokio::io::AsyncWrite for SendStream {
87 fn poll_write(
88 mut self: Pin<&mut Self>,
89 cx: &mut Context<'_>,
90 buf: &[u8],
91 ) -> Poll<io::Result<usize>> {
92 tokio::io::AsyncWrite::poll_write(Pin::new(&mut self.stream), cx, buf)
94 }
95
96 fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
97 Pin::new(&mut self.stream).poll_flush(cx)
98 }
99
100 fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
101 Pin::new(&mut self.stream).poll_shutdown(cx)
102 }
103}