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
#[cfg(feature = "alloc")]
pub use alloc::borrow;
#[doc(hidden)]
pub use futures_util::{join, try_join};
pub mod boxed {
#[cfg(feature = "alloc")]
pub use alloc::boxed::Box;
}
pub mod collections {
#[cfg(feature = "alloc")]
pub use alloc::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
pub use hashbrown::{HashMap, HashSet};
}
pub mod error {
#[cfg(not(feature = "std"))]
pub trait Error: core::fmt::Debug + core::fmt::Display {
fn source(&self) -> Option<&(dyn Error + 'static)> {
None
}
}
#[cfg(feature = "std")]
pub use std::error::Error;
}
#[cfg(feature = "alloc")]
pub use alloc::format;
#[cfg(not(feature = "std"))]
pub use core2::io;
#[cfg(feature = "std")]
pub use std::io;
#[cfg(feature = "std")]
pub use std::net;
#[cfg(all(not(feature = "std"), feature = "alloc"))]
pub mod println {
#[macro_export]
macro_rules! println {
($($arg:tt)*) => {{
tracing::info!($($arg)*);
}};
}
}
pub mod rand {
pub use rand::distributions;
pub use rand::prelude;
pub use rand::CryptoRng;
pub use rand::Error;
pub use rand::Rng;
pub use rand::RngCore;
#[cfg(not(feature = "std"))]
pub use not_random::thread_rng;
#[cfg(feature = "std")]
pub use rand::thread_rng;
#[cfg(not(feature = "std"))]
pub use not_random::random;
#[cfg(feature = "std")]
pub use rand::random;
#[cfg(feature = "std")]
pub use rand::rngs;
#[cfg(not(feature = "std"))]
pub mod rngs {
pub use super::not_random::OsRng;
}
#[cfg(not(feature = "std"))]
mod not_random {
use super::*;
#[derive(Clone)]
pub struct FakeRng(rand_pcg::Lcg64Xsh32);
impl CryptoRng for FakeRng {}
impl RngCore for FakeRng {
fn next_u32(&mut self) -> u32 {
self.0.gen()
}
fn next_u64(&mut self) -> u64 {
self.0.gen()
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
if let Err(e) = self.0.try_fill_bytes(dest) {
panic!("Error: {}", e);
}
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill(dest)
}
}
#[allow(unsafe_code)]
pub fn thread_rng() -> FakeRng {
use rand::SeedableRng;
static mut RNG: Option<rand_pcg::Lcg64Xsh32> = None;
unsafe {
if RNG.is_none() {
RNG = Some(rand_pcg::Pcg32::seed_from_u64(1234));
}
}
let lcg = unsafe { rand_pcg::Pcg32::seed_from_u64(RNG.as_mut().unwrap().gen()) };
FakeRng(lcg)
}
pub fn random<T>() -> T
where
rand::distributions::Standard: rand::prelude::Distribution<T>,
{
let mut rng = thread_rng();
rng.gen()
}
pub struct OsRng;
impl CryptoRng for OsRng {}
impl RngCore for OsRng {
fn next_u32(&mut self) -> u32 {
let mut rng = thread_rng();
rng.gen()
}
fn next_u64(&mut self) -> u64 {
let mut rng = thread_rng();
rng.gen()
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
if let Err(e) = self.try_fill_bytes(dest) {
panic!("Error: {}", e);
}
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
let mut rng = thread_rng();
rng.try_fill(dest)
}
}
}
}
pub mod string {
#[cfg(feature = "alloc")]
pub use alloc::string::{String, ToString};
#[cfg(not(feature = "alloc"))]
use heapless::String as ByteString;
}
#[cfg(not(feature = "std"))]
pub mod sync {
use core::convert::Infallible;
pub use alloc::sync::Arc;
pub use spin::RwLock;
pub struct Mutex<T>(spin::Mutex<T>);
impl<T> Mutex<T> {
pub fn new(value: T) -> Self {
Mutex(spin::Mutex::new(value))
}
pub fn lock(&self) -> Result<spin::MutexGuard<'_, T>, Infallible> {
Ok(self.0.lock())
}
}
impl<T> core::ops::Deref for Mutex<T> {
type Target = spin::Mutex<T>;
fn deref(&self) -> &spin::Mutex<T> {
&self.0
}
}
impl<T> core::ops::DerefMut for Mutex<T> {
fn deref_mut(&mut self) -> &mut spin::Mutex<T> {
&mut self.0
}
}
}
#[cfg(feature = "std")]
pub mod sync {
pub use std::sync::Arc;
pub use std::sync::{Mutex, RwLock};
}
#[cfg(not(feature = "std"))]
pub mod task {
#[cfg(feature = "alloc")]
pub use alloc::task::*;
pub use core::task::*;
}
#[cfg(feature = "std")]
pub use std::task;
pub mod vec {
#[cfg(feature = "alloc")]
pub use alloc::vec::*;
#[cfg(not(feature = "alloc"))]
pub type Vec<T> = heapless::Vec<T, 64>;
}
pub mod fmt {
pub use alloc::fmt::*;
#[cfg(feature = "alloc")]
pub use alloc::vec::*;
#[cfg(not(feature = "alloc"))]
pub type Vec<T> = heapless::Vec<T, 64>;
}