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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#[cfg(not(any(
    feature = "runtime-actix-native-tls",
    feature = "runtime-async-std-native-tls",
    feature = "runtime-tokio-native-tls",
    feature = "runtime-actix-rustls",
    feature = "runtime-async-std-rustls",
    feature = "runtime-tokio-rustls",
)))]
compile_error!(
    "one of the features ['runtime-actix-native-tls', 'runtime-async-std-native-tls', \
     'runtime-tokio-native-tls', 'runtime-actix-rustls', 'runtime-async-std-rustls', \
     'runtime-tokio-rustls'] must be enabled"
);

#[cfg(any(
    all(feature = "_rt-actix", feature = "_rt-async-std"),
    all(feature = "_rt-actix", feature = "_rt-tokio"),
    all(feature = "_rt-async-std", feature = "_rt-tokio"),
    all(feature = "_tls-native-tls", feature = "_tls-rustls"),
))]
compile_error!(
    "only one of ['runtime-actix-native-tls', 'runtime-async-std-native-tls', \
     'runtime-tokio-native-tls', 'runtime-actix-rustls', 'runtime-async-std-rustls', \
     'runtime-tokio-rustls'] can be enabled"
);

#[cfg(all(feature = "_tls-native-tls"))]
pub use native_tls;

//
// Actix *OR* Tokio
//

#[cfg(all(
    any(feature = "_rt-tokio", feature = "_rt-actix"),
    not(feature = "_rt-async-std"),
))]
pub use tokio::{
    self, fs, io::AsyncRead, io::AsyncReadExt, io::AsyncWrite, io::AsyncWriteExt, io::ReadBuf,
    net::TcpStream, runtime::Handle, task::spawn, task::yield_now, time::sleep, time::timeout,
};

#[cfg(all(
    unix,
    any(feature = "_rt-tokio", feature = "_rt-actix"),
    not(feature = "_rt-async-std"),
))]
pub use tokio::net::UnixStream;

#[cfg(all(
    any(feature = "_rt-tokio", feature = "_rt-actix"),
    not(feature = "_rt-async-std"),
))]
pub use tokio_runtime::{block_on, enter_runtime};

#[cfg(any(feature = "_rt-tokio", feature = "_rt-actix"))]
mod tokio_runtime {
    use once_cell::sync::Lazy;
    use tokio::runtime::{self, Runtime};

    // lazily initialize a global runtime once for multiple invocations of the macros
    static RUNTIME: Lazy<Runtime> = Lazy::new(|| {
        runtime::Builder::new_multi_thread()
            .enable_io()
            .enable_time()
            .build()
            .expect("failed to initialize Tokio runtime")
    });

    pub fn block_on<F: std::future::Future>(future: F) -> F::Output {
        RUNTIME.block_on(future)
    }

    pub fn enter_runtime<F, R>(f: F) -> R
    where
        F: FnOnce() -> R,
    {
        let _rt = RUNTIME.enter();
        f()
    }
}

#[cfg(all(
    feature = "_tls-native-tls",
    any(feature = "_rt-tokio", feature = "_rt-actix"),
    not(any(feature = "_tls-rustls", feature = "_rt-async-std")),
))]
pub use tokio_native_tls::{TlsConnector, TlsStream};

#[cfg(all(
    feature = "_tls-rustls",
    any(feature = "_rt-tokio", feature = "_rt-actix"),
    not(any(feature = "_tls-native-tls", feature = "_rt-async-std")),
))]
pub use tokio_rustls::{client::TlsStream, TlsConnector};

//
// tokio
//

#[cfg(all(
    feature = "_rt-tokio",
    not(any(feature = "_rt-actix", feature = "_rt-async-std")),
))]
#[macro_export]
macro_rules! blocking {
    ($($expr:tt)*) => {
        $crate::tokio::task::spawn_blocking(move || { $($expr)* })
            .await.expect("Blocking task failed to complete.")
    };
}

//
// actix
//

#[cfg(feature = "_rt-actix")]
pub use actix_rt;

#[cfg(all(
    feature = "_rt-actix",
    not(any(feature = "_rt-tokio", feature = "_rt-async-std")),
))]
#[macro_export]
macro_rules! blocking {
    ($($expr:tt)*) => {
         // spawn_blocking is a re-export from tokio
         $crate::actix_rt::task::spawn_blocking(move || { $($expr)* })
            .await
            .expect("Blocking task failed to complete.")
    };
}

//
// async-std
//

#[cfg(all(
    feature = "_rt-async-std",
    not(any(feature = "_rt-actix", feature = "_rt-tokio")),
))]
pub use async_std::{
    self, fs, future::timeout, io::prelude::ReadExt as AsyncReadExt,
    io::prelude::WriteExt as AsyncWriteExt, io::Read as AsyncRead, io::Write as AsyncWrite,
    net::TcpStream, task::sleep, task::spawn, task::yield_now,
};

#[cfg(all(
    feature = "_rt-async-std",
    not(any(feature = "_rt-actix", feature = "_rt-tokio")),
))]
#[macro_export]
macro_rules! blocking {
    ($($expr:tt)*) => {
        $crate::async_std::task::spawn_blocking(move || { $($expr)* }).await
    };
}

#[cfg(all(
    unix,
    feature = "_rt-async-std",
    not(any(feature = "_rt-actix", feature = "_rt-tokio")),
))]
pub use async_std::os::unix::net::UnixStream;

#[cfg(all(
    feature = "_rt-async-std",
    not(any(feature = "_rt-actix", feature = "_rt-tokio")),
))]
pub use async_std::task::block_on;

#[cfg(all(
    feature = "_rt-async-std",
    not(any(feature = "_rt-actix", feature = "_rt-tokio")),
))]
pub fn enter_runtime<F, R>(f: F) -> R
where
    F: FnOnce() -> R,
{
    // no-op for async-std
    f()
}

#[cfg(all(feature = "async-native-tls", not(feature = "tokio-native-tls")))]
pub use async_native_tls::{TlsConnector, TlsStream};

#[cfg(all(
    feature = "_tls-rustls",
    feature = "_rt-async-std",
    not(any(
        feature = "_tls-native-tls",
        feature = "_rt-tokio",
        feature = "_rt-actix"
    )),
))]
pub use async_rustls::{client::TlsStream, TlsConnector};