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
172
173
174
175
176
177
178
179
180
181
182
use std::{net::SocketAddr, sync::Arc, time::Duration};
use crate::traits::*;
use async_trait::async_trait;
use educe::Educe;
use futures::{future::FutureObj, task::Spawn};
use std::io::Result as IoResult;
use std::time::{Instant, SystemTime};
#[derive(Educe)]
#[educe(Clone)]
pub struct CompoundRuntime<SpawnR, SleepR, TcpR, TlsR, UdpR> {
inner: Arc<Inner<SpawnR, SleepR, TcpR, TlsR, UdpR>>,
}
struct Inner<SpawnR, SleepR, TcpR, TlsR, UdpR> {
spawn: SpawnR,
sleep: SleepR,
tcp: TcpR,
tls: TlsR,
udp: UdpR,
}
impl<SpawnR, SleepR, TcpR, TlsR, UdpR> CompoundRuntime<SpawnR, SleepR, TcpR, TlsR, UdpR> {
pub fn new(spawn: SpawnR, sleep: SleepR, tcp: TcpR, tls: TlsR, udp: UdpR) -> Self {
CompoundRuntime {
inner: Arc::new(Inner {
spawn,
sleep,
tcp,
tls,
udp,
}),
}
}
}
impl<SpawnR, SleepR, TcpR, TlsR, UdpR> Spawn for CompoundRuntime<SpawnR, SleepR, TcpR, TlsR, UdpR>
where
SpawnR: Spawn,
{
#[inline]
fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), futures::task::SpawnError> {
self.inner.spawn.spawn_obj(future)
}
}
impl<SpawnR, SleepR, TcpR, TlsR, UdpR> BlockOn for CompoundRuntime<SpawnR, SleepR, TcpR, TlsR, UdpR>
where
SpawnR: BlockOn,
SleepR: Clone + Send + Sync + 'static,
TcpR: Clone + Send + Sync + 'static,
TlsR: Clone + Send + Sync + 'static,
UdpR: Clone + Send + Sync + 'static,
{
#[inline]
fn block_on<F: futures::Future>(&self, future: F) -> F::Output {
self.inner.spawn.block_on(future)
}
}
impl<SpawnR, SleepR, TcpR, TlsR, UdpR> SleepProvider
for CompoundRuntime<SpawnR, SleepR, TcpR, TlsR, UdpR>
where
SleepR: SleepProvider,
SpawnR: Clone + Send + Sync + 'static,
TcpR: Clone + Send + Sync + 'static,
TlsR: Clone + Send + Sync + 'static,
UdpR: Clone + Send + Sync + 'static,
{
type SleepFuture = SleepR::SleepFuture;
#[inline]
fn sleep(&self, duration: Duration) -> Self::SleepFuture {
self.inner.sleep.sleep(duration)
}
#[inline]
fn now(&self) -> Instant {
self.inner.sleep.now()
}
#[inline]
fn wallclock(&self) -> SystemTime {
self.inner.sleep.wallclock()
}
}
#[async_trait]
impl<SpawnR, SleepR, TcpR, TlsR, UdpR> TcpProvider
for CompoundRuntime<SpawnR, SleepR, TcpR, TlsR, UdpR>
where
TcpR: TcpProvider,
SpawnR: Send + Sync + 'static,
SleepR: Send + Sync + 'static,
TcpR: Send + Sync + 'static,
TlsR: Send + Sync + 'static,
UdpR: Send + Sync + 'static,
{
type TcpStream = TcpR::TcpStream;
type TcpListener = TcpR::TcpListener;
#[inline]
async fn connect(&self, addr: &SocketAddr) -> IoResult<Self::TcpStream> {
self.inner.tcp.connect(addr).await
}
#[inline]
async fn listen(&self, addr: &SocketAddr) -> IoResult<Self::TcpListener> {
self.inner.tcp.listen(addr).await
}
}
impl<SpawnR, SleepR, TcpR, TlsR, UdpR, S> TlsProvider<S>
for CompoundRuntime<SpawnR, SleepR, TcpR, TlsR, UdpR>
where
TcpR: TcpProvider,
TlsR: TlsProvider<S>,
SleepR: Clone + Send + Sync + 'static,
SpawnR: Clone + Send + Sync + 'static,
UdpR: Clone + Send + Sync + 'static,
{
type Connector = TlsR::Connector;
type TlsStream = TlsR::TlsStream;
#[inline]
fn tls_connector(&self) -> Self::Connector {
self.inner.tls.tls_connector()
}
}
impl<SpawnR, SleepR, TcpR, TlsR, UdpR> std::fmt::Debug
for CompoundRuntime<SpawnR, SleepR, TcpR, TlsR, UdpR>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CompoundRuntime").finish_non_exhaustive()
}
}
#[async_trait]
impl<SpawnR, SleepR, TcpR, TlsR, UdpR> UdpProvider
for CompoundRuntime<SpawnR, SleepR, TcpR, TlsR, UdpR>
where
UdpR: UdpProvider,
SpawnR: Send + Sync + 'static,
SleepR: Send + Sync + 'static,
TcpR: Send + Sync + 'static,
TlsR: Send + Sync + 'static,
UdpR: Send + Sync + 'static,
{
type UdpSocket = UdpR::UdpSocket;
#[inline]
async fn bind(&self, addr: &SocketAddr) -> IoResult<Self::UdpSocket> {
self.inner.udp.bind(addr).await
}
}