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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
use alloc::string::String;
#[cfg(feature = "default")]
#[inline]
pub fn abort_on_panic<T>(f: impl FnOnce() -> T) -> T {
struct Bomb;
impl Drop for Bomb {
fn drop(&mut self) {
std::process::abort();
}
}
let bomb = Bomb;
let t = f();
std::mem::forget(bomb);
t
}
#[cfg(feature = "unstable")]
pub fn random(n: u32) -> u32 {
use std::cell::Cell;
use std::num::Wrapping;
thread_local! {
static RNG: Cell<Wrapping<u32>> = {
let mut x = 0i32;
let r = &mut x;
let addr = r as *mut i32 as usize;
Cell::new(Wrapping(addr as u32))
}
}
RNG.with(|rng| {
let mut x = rng.get();
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
rng.set(x);
((u64::from(x.0)).wrapping_mul(u64::from(n)) >> 32) as u32
})
}
pub(crate) trait Context {
fn context(self, message: impl Fn() -> String) -> Self;
}
#[cfg(all(
not(target_os = "unknown"),
any(feature = "default", feature = "unstable")
))]
mod timer {
pub type Timer = async_io::Timer;
}
#[cfg(any(feature = "unstable", feature = "default"))]
pub(crate) fn timer_after(dur: std::time::Duration) -> timer::Timer {
Timer::after(dur)
}
#[cfg(any(all(target_arch = "wasm32", feature = "default"),))]
mod timer {
use std::pin::Pin;
use std::task::Poll;
use gloo_timers::future::TimeoutFuture;
#[derive(Debug)]
pub(crate) struct Timer(TimeoutFuture);
impl Timer {
pub(crate) fn after(dur: std::time::Duration) -> Self {
Timer(TimeoutFuture::new(dur.as_millis() as u32))
}
}
impl std::future::Future for Timer {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Self::Output> {
match Pin::new(&mut self.0).poll(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(_) => Poll::Ready(()),
}
}
}
}
#[cfg(any(feature = "unstable", feature = "default"))]
pub(crate) use timer::*;
#[cfg(feature = "default")]
#[doc(hidden)]
macro_rules! defer {
($($body:tt)*) => {
let _guard = {
pub struct Guard<F: FnOnce()>(Option<F>);
impl<F: FnOnce()> Drop for Guard<F> {
fn drop(&mut self) {
(self.0).take().map(|f| f());
}
}
Guard(Some(|| {
let _ = { $($body)* };
}))
};
};
}
#[doc(hidden)]
macro_rules! cfg_unstable {
($($item:item)*) => {
$(
#[cfg(feature = "unstable")]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
$item
)*
}
}
#[doc(hidden)]
macro_rules! cfg_unstable_default {
($($item:item)*) => {
$(
#[cfg(all(feature = "default", feature = "unstable"))]
#[cfg_attr(feature = "docs", doc(unstable))]
$item
)*
}
}
#[doc(hidden)]
#[allow(unused_macros)]
macro_rules! cfg_unix {
($($item:item)*) => {
$(
#[cfg(any(unix, feature = "docs"))]
#[cfg_attr(feature = "docs", doc(cfg(unix)))]
$item
)*
}
}
#[doc(hidden)]
#[allow(unused_macros)]
macro_rules! cfg_windows {
($($item:item)*) => {
$(
#[cfg(any(windows, feature = "docs"))]
#[cfg_attr(feature = "docs", doc(cfg(windows)))]
$item
)*
}
}
#[doc(hidden)]
#[allow(unused_macros)]
macro_rules! cfg_docs {
($($item:item)*) => {
$(
#[cfg(feature = "docs")]
$item
)*
}
}
#[doc(hidden)]
#[allow(unused_macros)]
macro_rules! cfg_not_docs {
($($item:item)*) => {
$(
#[cfg(not(feature = "docs"))]
$item
)*
}
}
#[allow(unused_macros)]
#[doc(hidden)]
macro_rules! cfg_std {
($($item:item)*) => {
$(
#[cfg(feature = "std")]
$item
)*
}
}
#[allow(unused_macros)]
#[doc(hidden)]
macro_rules! cfg_alloc {
($($item:item)*) => {
$(
#[cfg(feature = "alloc")]
$item
)*
}
}
#[allow(unused_macros)]
#[doc(hidden)]
macro_rules! cfg_default {
($($item:item)*) => {
$(
#[cfg(feature = "default")]
$item
)*
}
}
#[allow(unused_macros)]
#[doc(hidden)]
macro_rules! extension_trait {
(
#[doc = $doc:tt]
pub trait $name:ident {
$($body_base:tt)*
}
#[doc = $doc_ext:tt]
pub trait $ext:ident: $base:path {
$($body_ext:tt)*
}
$($imp:item)*
) => {
#[allow(dead_code)]
mod owned {
#[doc(hidden)]
pub struct ImplFuture<T>(core::marker::PhantomData<T>);
}
#[allow(dead_code)]
mod borrowed {
#[doc(hidden)]
pub struct ImplFuture<'a, T>(core::marker::PhantomData<&'a T>);
}
#[cfg(feature = "docs")]
#[doc = $doc]
pub trait $name {
extension_trait!(@doc () $($body_base)* $($body_ext)*);
}
#[cfg(not(feature = "docs"))]
pub use $base as $name;
#[doc = $doc_ext]
pub trait $ext: $name {
extension_trait!(@ext () $($body_ext)*);
}
impl<T: $name + ?Sized> $ext for T {}
$(#[cfg(feature = "docs")] $imp)*
};
(@doc ($($head:tt)*) -> impl Future<Output = $out:ty> $(+ $lt:lifetime)? [$f:ty] $($tail:tt)*) => {
extension_trait!(@doc ($($head)* -> owned::ImplFuture<$out>) $($tail)*);
};
(@ext ($($head:tt)*) -> impl Future<Output = $out:ty> $(+ $lt:lifetime)? [$f:ty] $($tail:tt)*) => {
extension_trait!(@ext ($($head)* -> $f) $($tail)*);
};
(@doc ($($head:tt)*) -> impl Future<Output = $out:ty> + $lt:lifetime [$f:ty] $($tail:tt)*) => {
extension_trait!(@doc ($($head)* -> borrowed::ImplFuture<$lt, $out>) $($tail)*);
};
(@ext ($($head:tt)*) -> impl Future<Output = $out:ty> + $lt:lifetime [$f:ty] $($tail:tt)*) => {
extension_trait!(@ext ($($head)* -> $f) $($tail)*);
};
(@doc ($($head:tt)*) $token:tt $($tail:tt)*) => {
extension_trait!(@doc ($($head)* $token) $($tail)*);
};
(@ext ($($head:tt)*) $token:tt $($tail:tt)*) => {
extension_trait!(@ext ($($head)* $token) $($tail)*);
};
(@doc ($($head:tt)*)) => { $($head)* };
(@ext ($($head:tt)*)) => { $($head)* };
($import:item $($tail:tt)*) => {
#[cfg(feature = "docs")]
$import
extension_trait!($($tail)*);
};
}