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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//! A facade around the various collections and primitives needed to
//! support `std`, `no_std + alloc` or `no_std` targets.
//!
//! When importing from the standard library:
//!
//!   1. always prefer `core::<mod>` over `std::<mod>` where it's
//!      available. (e.g. `std::fmt::Result` -> `core::fmt::Result`)
//!   2. use `ockam_core::compat::<mod>` equivalents where
//!      possible. (e.g. `std::sync::Arc` -> `ockam_core::compat::sync::Arc`)
//!   3. if you need to add new items to compat, follow the originating
//!      namespace. (e.g. `compat::vec::Vec` and not `compat::Vec`)

/// Provides `std::borrow` for `alloc` targets.
#[cfg(feature = "alloc")]
pub use alloc::borrow;

#[doc(hidden)]
pub use futures_util::{join, try_join};

/// Provides `std::boxed` for `alloc` targets.
pub mod boxed {
    #[cfg(feature = "alloc")]
    pub use alloc::boxed::Box;
}

/// Provides `std::collections` and alternate `hashbrown` map and set
/// implementations.
pub mod collections {
    #[cfg(feature = "alloc")]
    pub use alloc::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};

    pub use hashbrown::{HashMap, HashSet};
}

/// Provides a `std::error::Error` trait.
pub mod error {
    #[cfg(not(feature = "std"))]
    /// A `no_std` compatible definition of the `std::error::Error` trait.
    pub trait Error: core::fmt::Debug + core::fmt::Display {
        /// The source of this error.
        fn source(&self) -> Option<&(dyn Error + 'static)> {
            None
        }
    }
    #[cfg(feature = "std")]
    pub use std::error::Error;
}

/// Provides `std::format` for `alloc` targets.
#[cfg(feature = "alloc")]
pub use alloc::format;

/// Provides `std::io`.
#[cfg(not(feature = "std"))]
pub use core2::io;
#[cfg(feature = "std")]
pub use std::io;

/// Provides `std::net`.
#[cfg(feature = "std")]
pub use std::net;

/// Provides a `println!` wrapper around `tracing::info!` for `no_std` targets
#[cfg(all(not(feature = "std"), feature = "alloc"))]
pub mod println {
    #[macro_export]
    /// Implementation of println for `no_std` by wrapping the `tracing::info!` macro.
    macro_rules! println {
        ($($arg:tt)*) => {{
            tracing::info!($($arg)*);
        }};
    }
}

/// Provides `rand`.
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;

    /// rngs
    #[cfg(feature = "std")]
    pub use rand::rngs;
    #[cfg(not(feature = "std"))]
    /// A placeholder implementation of the `rand::rngs` generators module.
    ///
    /// WARNING: This implementation does NOT generate true random
    /// values, please do not try to use it in production.
    pub mod rngs {
        pub use super::not_random::OsRng;
    }

    /// Generates a random String of length 16.
    #[cfg(feature = "std")]
    pub fn random_string() -> String {
        use rand::distributions::{Alphanumeric, DistString};
        Alphanumeric.sample_string(&mut thread_rng(), 16)
    }

    /// Placeholders for various features from 'rand' that are not
    /// supported on no_std targets.
    ///
    /// WARNING: This implementation does NOT generate true random
    /// values, please do not try to use any of these in production.
    #[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)
            }
        }

        /// An implementation of `rand::thread_rng()` not intended for
        /// production use.
        ///
        /// WARNING: This implementation is neither random nor
        /// thread-local.
        #[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)
        }

        /// An implementation of `rand::random()` not intended for
        /// production use.
        pub fn random<T>() -> T
        where
            rand::distributions::Standard: rand::prelude::Distribution<T>,
        {
            let mut rng = thread_rng();
            rng.gen()
        }

        /// `rand::OsRng`
        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)
            }
        }
    }
}

/// Provides `std::string`.
pub mod string {
    #[cfg(feature = "alloc")]
    pub use alloc::string::{String, ToString};
    #[cfg(not(feature = "alloc"))]
    use heapless::String as ByteString;
}

/// Provides `std::str`.
pub mod str {
    #[cfg(feature = "alloc")]
    pub use alloc::str::from_utf8;
    #[cfg(feature = "alloc")]
    pub use alloc::str::FromStr;
}

/// Provides `std::sync` for `no_std` targets.
#[cfg(not(feature = "std"))]
pub mod sync {
    use core::convert::Infallible;

    pub use alloc::sync::Arc;

    /// Wrap `spin::RwLock` as it does not return LockResult<Guard> like `std::sync::Mutex`.
    #[derive(Debug)]
    pub struct RwLock<T>(spin::RwLock<T>);
    impl<T> RwLock<T> {
        /// Creates a new spinlock wrapping the supplied data.
        pub fn new(value: T) -> Self {
            RwLock(spin::RwLock::new(value))
        }
        /// Locks this rwlock with shared read access, blocking the current thread
        /// until it can be acquired.
        pub fn read(&self) -> Result<spin::RwLockReadGuard<'_, T>, Infallible> {
            Ok(self.0.read())
        }
        /// Lock this rwlock with exclusive write access, blocking the current
        /// thread until it can be acquired.
        pub fn write(&self) -> Result<spin::RwLockWriteGuard<'_, T>, Infallible> {
            Ok(self.0.write())
        }
    }
    impl<T: Default> Default for RwLock<T> {
        fn default() -> Self {
            Self::new(Default::default())
        }
    }
    impl<T> core::ops::Deref for RwLock<T> {
        type Target = spin::RwLock<T>;
        fn deref(&self) -> &spin::RwLock<T> {
            &self.0
        }
    }
    impl<T> core::ops::DerefMut for RwLock<T> {
        fn deref_mut(&mut self) -> &mut spin::RwLock<T> {
            &mut self.0
        }
    }

    /// Wrap `spin::Mutex.lock()` as it does not return LockResult<Guard> like `std::sync::Mutex`.
    pub struct Mutex<T>(spin::Mutex<T>);
    impl<T> Mutex<T> {
        /// Creates a new mutex in an unlocked state ready for use.
        pub const fn new(value: T) -> Self {
            Mutex(spin::Mutex::new(value))
        }
        /// Acquires a mutex, blocking the current thread until it is able to do so.
        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
        }
    }
}
/// Provides `std::sync` for `std` targets.
#[cfg(feature = "std")]
pub mod sync {
    pub use std::sync::Arc;
    pub use std::sync::{Mutex, RwLock};
}

/// Provides `std::task` for `no_std` targets.
#[cfg(not(feature = "std"))]
pub mod task {
    // Include both `alloc::task::*` and `core::task::*` for a better
    // approximation of `std::task::*` (which contains both).
    #[cfg(feature = "alloc")]
    pub use alloc::task::*;
    pub use core::task::*;
}

/// Provides `std::task` for `std` targets.
#[cfg(feature = "std")]
pub use std::task;

/// Provides `std::vec`.
pub mod vec {
    #[cfg(feature = "alloc")]
    pub use alloc::vec;
    #[cfg(feature = "alloc")]
    pub use alloc::vec::*;
    #[cfg(not(feature = "alloc"))]
    pub type Vec<T> = heapless::Vec<T, 64>;
}

/// Provides `core::fmt`
pub mod fmt {
    #[cfg(feature = "alloc")]
    pub use alloc::fmt::*;
    #[cfg(not(feature = "alloc"))]
    pub use core::fmt::*;
}

/// Provides `future::poll_once`
pub mod future {
    use crate::{
        errcode::{Kind, Origin},
        Error, Result,
    };
    use futures_util::future::{Future, FutureExt};

    /// Polls a future just once and returns the Result
    ///
    /// This is only used for some tests and it is hoped that we can
    /// remove it if, at some point, this makes it into `core::future`
    pub fn poll_once<'a, F, T>(future: F) -> Result<T>
    where
        F: Future<Output = Result<T>> + Send + 'a,
    {
        use core::task::{Context, Poll};
        use core::task::{RawWaker, RawWakerVTable, Waker};

        fn dummy_raw_waker() -> RawWaker {
            fn no_op(_: *const ()) {}
            fn clone(_: *const ()) -> RawWaker {
                dummy_raw_waker()
            }
            let vtable = &RawWakerVTable::new(clone, no_op, no_op, no_op);
            RawWaker::new(core::ptr::null(), vtable)
        }

        fn dummy_waker() -> Waker {
            // The RawWaker's vtable only contains safe no-op
            // functions which do not refer to the data field.
            #[allow(unsafe_code)]
            unsafe {
                Waker::from_raw(dummy_raw_waker())
            }
        }

        let waker = dummy_waker();
        let mut context = Context::from_waker(&waker);
        let result = future.boxed().poll_unpin(&mut context);
        assert!(
            result.is_ready(),
            "poll_once() only accepts futures that resolve after being polled once"
        );
        match result {
            Poll::Ready(value) => value,
            Poll::Pending => Err(Error::new_without_cause(Origin::Core, Kind::Invalid)),
        }
    }
}