1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
//! Declare a macro for making opaque runtime wrappers.
/// Implement delegating implementations of the runtime traits for a type $t
/// whose member $r implements Runtime. Used to hide the details of the
/// implementation of $t.
#[allow(unused)] // Can be unused if no runtimes are declared.
macro_rules! implement_opaque_runtime {
{
$t:ty { $member:ident : $mty:ty }
} => {
impl futures::task::Spawn for $t {
// `track_caller` required for tokio-console
#[inline]
#[track_caller]
fn spawn_obj(&self, future: futures::future::FutureObj<'static, ()>) -> Result<(), futures::task::SpawnError> {
self.$member.spawn_obj(future)
}
}
impl $crate::traits::Blocking for $t {
type ThreadHandle<T: Send + 'static> = <$mty as $crate::traits::Blocking>::ThreadHandle<T>;
// `track_caller` required for tokio-console
#[inline]
#[track_caller]
fn spawn_blocking<F, T>(&self, f: F) -> <$mty as $crate::traits::Blocking>::ThreadHandle<T>
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
{
self.$member.spawn_blocking(f)
}
// `track_caller` required for tokio-console
#[inline]
#[track_caller]
fn reenter_block_on<F>(&self, future: F) -> F::Output
where
F: futures::Future,
F::Output: Send + 'static
{
self.$member.reenter_block_on(future)
}
}
impl $crate::traits::ToplevelBlockOn for $t {
// `track_caller` required for tokio-console
#[inline]
#[track_caller]
fn block_on<F: futures::Future>(&self, future: F) -> F::Output {
self.$member.block_on(future)
}
}
impl $crate::traits::SleepProvider for $t {
type SleepFuture = <$mty as $crate::traits::SleepProvider>::SleepFuture;
#[inline]
fn sleep(&self, duration: std::time::Duration) -> Self::SleepFuture {
self.$member.sleep(duration)
}
}
impl $crate::CoarseTimeProvider for $t {
#[inline]
fn now_coarse(&self) -> $crate::CoarseInstant {
self.$member.now_coarse()
}
}
#[async_trait::async_trait]
impl $crate::traits::NetStreamProvider<std::net::SocketAddr> for $t {
type Stream = <$mty as $crate::traits::NetStreamProvider>::Stream;
type Listener = <$mty as $crate::traits::NetStreamProvider>::Listener;
type ConnectOptions = <$mty as $crate::traits::NetStreamProvider>::ConnectOptions;
type ListenOptions = <$mty as $crate::traits::NetStreamProvider>::ListenOptions;
#[inline]
#[tracing::instrument(skip_all, level = "trace")]
async fn connect(
&self,
addr: &std::net::SocketAddr,
options: &Self::ConnectOptions,
) -> std::io::Result<Self::Stream> {
self.$member.connect(addr, options).await
}
#[inline]
async fn listen(
&self,
addr: &std::net::SocketAddr,
options: &Self::ListenOptions,
) -> std::io::Result<Self::Listener> {
self.$member.listen(addr, options).await
}
}
#[async_trait::async_trait]
impl $crate::traits::NetStreamProvider<tor_general_addr::unix::SocketAddr> for $t {
type Stream = <$mty as $crate::traits::NetStreamProvider<tor_general_addr::unix::SocketAddr>>::Stream;
type Listener = <$mty as $crate::traits::NetStreamProvider<tor_general_addr::unix::SocketAddr>>::Listener;
type ConnectOptions = <$mty as $crate::traits::NetStreamProvider<tor_general_addr::unix::SocketAddr>>::ConnectOptions;
type ListenOptions = <$mty as $crate::traits::NetStreamProvider<tor_general_addr::unix::SocketAddr>>::ListenOptions;
#[inline]
#[tracing::instrument(skip_all, level = "trace")]
async fn connect(
&self,
addr: &tor_general_addr::unix::SocketAddr,
options: &Self::ConnectOptions,
) -> std::io::Result<Self::Stream> {
self.$member.connect(addr, options).await
}
#[inline]
async fn listen(
&self,
addr: &tor_general_addr::unix::SocketAddr,
options: &Self::ListenOptions,
) -> std::io::Result<Self::Listener> {
self.$member.listen(addr, options).await
}
}
impl<S> $crate::traits::TlsProvider<S> for $t
where S: futures::AsyncRead + futures::AsyncWrite + $crate::traits::StreamOps + Unpin + Send + 'static,
{
type Connector = <$mty as $crate::traits::TlsProvider<S>>::Connector;
type TlsStream = <$mty as $crate::traits::TlsProvider<S>>::TlsStream;
type Acceptor = <$mty as $crate::traits::TlsProvider<S>>::Acceptor;
type TlsServerStream = <$mty as $crate::traits::TlsProvider<S>>::TlsServerStream;
#[inline]
fn tls_connector(&self) -> Self::Connector {
self.$member.tls_connector()
}
#[inline]
fn tls_acceptor(&self, settings: $crate::traits::TlsAcceptorSettings) -> std::io::Result<Self::Acceptor> {
<$mty as $crate::traits::TlsProvider<S>>::tls_acceptor(&self.$member, settings)
}
#[inline]
fn supports_keying_material_export(&self) -> bool {
<$mty as $crate::traits::TlsProvider<S>>::supports_keying_material_export(&self.$member)
}
}
#[async_trait::async_trait]
impl $crate::traits::UdpProvider for $t {
type UdpSocket = <$mty as $crate::traits::UdpProvider>::UdpSocket;
#[inline]
async fn bind(&self, addr: &std::net::SocketAddr) -> std::io::Result<Self::UdpSocket> {
self.$member.bind(addr).await
}
}
impl std::fmt::Debug for $t {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct(stringify!($t)).finish_non_exhaustive()
}
}
// This boilerplate will fail unless $t implements Runtime.
#[allow(unused)]
const _ : () = {
fn assert_runtime<R: $crate::Runtime>() {}
fn check() {
assert_runtime::<$t>();
}
};
}
}
#[allow(unused)] // Can be unused if no runtimes are declared.
pub(crate) use implement_opaque_runtime;
|