Skip to main content

http3_datagram/
quic_traits.rs

1//! QUIC Transport traits
2//!
3//! This module includes traits and types meant to allow being generic over any
4//! QUIC implementation.
5
6use core::task;
7use std::task::Poll;
8
9use bytes::Buf;
10use http3::quic::ConnectionErrorIncoming;
11
12use crate::datagram::EncodedDatagram;
13
14/// Connection Extension trait for a DatagramHandler type defined by the quic implementation
15pub trait DatagramConnectionExt<B: Buf> {
16    /// The type of the Datagram send Handler
17    type SendDatagramHandler: SendDatagram<B>;
18
19    /// The type of the Datagram receive Handler
20    type RecvDatagramHandler: RecvDatagram;
21
22    /// Get the send datagram handler
23    fn send_datagram_handler(&self) -> Self::SendDatagramHandler;
24
25    /// Get the receive datagram handler
26    fn recv_datagram_handler(&self) -> Self::RecvDatagramHandler;
27}
28
29/// Extends the `Connection` trait for sending datagrams
30///
31/// See: <https://www.rfc-editor.org/rfc/rfc9297>
32pub trait SendDatagram<B: Buf> {
33    /// Send a datagram
34    fn send_datagram<T: Into<EncodedDatagram<B>>>(
35        &mut self,
36        data: T,
37    ) -> Result<(), SendDatagramErrorIncoming>;
38}
39
40/// Extends the `Connection` trait for receiving datagrams
41///
42/// See: <https://www.rfc-editor.org/rfc/rfc9297>
43pub trait RecvDatagram {
44    /// The buffer type
45    type Buffer: Buf;
46
47    /// Poll the connection for incoming datagrams.
48    fn poll_incoming_datagram(
49        &mut self,
50        cx: &mut task::Context<'_>,
51    ) -> Poll<Result<Self::Buffer, ConnectionErrorIncoming>>;
52}
53
54/// Types of errors when sending a datagram.
55#[derive(Debug)]
56pub enum SendDatagramErrorIncoming {
57    /// The peer is not accepting datagrams
58    ///
59    /// This can be because the peer does not support it or disabled it or any other reason.
60    NotAvailable,
61    /// The datagram is too large to send
62    TooLarge,
63    /// Connection error
64    ConnectionError(ConnectionErrorIncoming),
65}