quic_rpc/transport/
mod.rs1use std::{
21 fmt::{self, Debug, Display},
22 net::SocketAddr,
23};
24
25use boxed::{BoxableConnector, BoxableListener, BoxedConnector, BoxedListener};
26use futures_lite::{Future, Stream};
27use futures_sink::Sink;
28use mapped::MappedConnector;
29
30use crate::{RpcError, RpcMessage};
31
32pub mod boxed;
33pub mod combined;
34#[cfg(feature = "flume-transport")]
35#[cfg_attr(quicrpc_docsrs, doc(cfg(feature = "flume-transport")))]
36pub mod flume;
37#[cfg(feature = "hyper-transport")]
38#[cfg_attr(quicrpc_docsrs, doc(cfg(feature = "hyper-transport")))]
39pub mod hyper;
40#[cfg(feature = "iroh-transport")]
41#[cfg_attr(quicrpc_docsrs, doc(cfg(feature = "iroh-transport")))]
42pub mod iroh;
43pub mod mapped;
44pub mod misc;
45#[cfg(feature = "quinn-transport")]
46#[cfg_attr(quicrpc_docsrs, doc(cfg(feature = "quinn-transport")))]
47pub mod quinn;
48
49#[cfg(any(feature = "quinn-transport", feature = "iroh-transport"))]
50#[cfg_attr(
51 quicrpc_docsrs,
52 doc(cfg(any(feature = "quinn-transport", feature = "iroh-transport")))
53)]
54mod util;
55
56pub trait ConnectionErrors: Debug + Clone + Send + Sync + 'static {
58 type SendError: RpcError;
60 type RecvError: RpcError;
62 type OpenError: RpcError;
64 type AcceptError: RpcError;
66}
67
68pub trait StreamTypes: ConnectionErrors {
72 type In: RpcMessage;
74 type Out: RpcMessage;
76 type RecvStream: Stream<Item = Result<Self::In, Self::RecvError>>
78 + Send
79 + Sync
80 + Unpin
81 + 'static;
82 type SendSink: Sink<Self::Out, Error = Self::SendError> + Send + Sync + Unpin + 'static;
84}
85
86pub trait Connector: StreamTypes {
90 fn open(
92 &self,
93 ) -> impl Future<Output = Result<(Self::SendSink, Self::RecvStream), Self::OpenError>> + Send;
94
95 fn map<In1, Out1>(self) -> MappedConnector<In1, Out1, Self>
97 where
98 In1: TryFrom<Self::In>,
99 Self::Out: From<Out1>,
100 {
101 MappedConnector::new(self)
102 }
103
104 fn boxed(self) -> BoxedConnector<Self::In, Self::Out>
106 where
107 Self: BoxableConnector<Self::In, Self::Out> + Sized + 'static,
108 {
109 self::BoxedConnector::new(self)
110 }
111}
112
113pub trait Listener: StreamTypes {
118 fn accept(
121 &self,
122 ) -> impl Future<Output = Result<(Self::SendSink, Self::RecvStream), Self::AcceptError>> + Send;
123
124 fn local_addr(&self) -> &[LocalAddr];
126
127 fn boxed(self) -> BoxedListener<Self::In, Self::Out>
129 where
130 Self: BoxableListener<Self::In, Self::Out> + Sized + 'static,
131 {
132 BoxedListener::new(self)
133 }
134}
135
136#[derive(Debug, Clone)]
142#[non_exhaustive]
143pub enum LocalAddr {
144 Socket(SocketAddr),
146 Mem,
148}
149
150impl Display for LocalAddr {
151 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
152 match self {
153 LocalAddr::Socket(sockaddr) => write!(f, "{sockaddr}"),
154 LocalAddr::Mem => write!(f, "mem"),
155 }
156 }
157}