no_std_compat/
lib.rs

1#![no_std]
2#![cfg_attr(all(not(feature = "std"), feature = "unstable"),
3            feature(core_intrinsics, core_panic, raw, unicode_internals))]
4#![cfg_attr(all(not(feature = "std"), feature = "alloc", feature = "unstable"),
5            feature(alloc_prelude, raw_vec_internals, wake_trait))]
6
7// Can't use cfg_if! because it does not allow nesting :(
8
9// Actually, can't even generate #[cfg]s any other way because of
10// https://github.com/rust-lang/rust/pull/52234#issuecomment-486810130
11
12// if #[cfg(feature = "std")] {
13    #[cfg(feature = "std")]
14    extern crate std;
15    #[cfg(feature = "std")]
16    pub mod prelude {
17        pub mod v1 {
18            pub use std::prelude::v1::*;
19            // Macros aren't included in the prelude for some reason
20            pub use std::{
21                format, vec,
22                print, println, eprint, eprintln, dbg
23            };
24        }
25    }
26    #[cfg(feature = "std")]
27    pub use std::*;
28// } else {
29    // The 2 underscores in the crate names are used to avoid
30    // ambiguity between whether the user wants to use the public
31    // module std::alloc or the private crate no_std_compat::alloc
32    // (see https://gitlab.com/jD91mZM2/no-std-compat/issues/1)
33
34    // if #[cfg(feature = "alloc")] {
35        #[cfg(all(not(feature = "std"), feature = "alloc"))]
36        extern crate alloc as __alloc;
37    // }
38
39    #[cfg(not(feature = "std"))]
40    extern crate core as __core;
41
42    #[cfg(not(feature = "std"))]
43    mod generated;
44
45    #[cfg(not(feature = "std"))]
46    pub use self::generated::*;
47
48    // if #[cfg(feature = "compat_macros")] {
49        #[cfg(all(not(feature = "std"), feature = "compat_macros"))]
50        #[macro_export]
51        macro_rules! print {
52            () => {{}};
53            ($($arg:tt)+) => {{
54                // Avoid unused arguments complaint. This surely must get
55                // optimized away? TODO: Verify that
56                let _ = format_args!($($arg)+);
57            }};
58        }
59        #[cfg(all(not(feature = "std"), feature = "compat_macros"))]
60        #[macro_export]
61        macro_rules! println {
62            ($($arg:tt)*) => { print!($($arg)*) }
63        }
64        #[cfg(all(not(feature = "std"), feature = "compat_macros"))]
65        #[macro_export]
66        macro_rules! eprint {
67            ($($arg:tt)*) => { print!($($arg)*) }
68        }
69        #[cfg(all(not(feature = "std"), feature = "compat_macros"))]
70        #[macro_export]
71        macro_rules! eprintln {
72            ($($arg:tt)*) => { print!($($arg)*) }
73        }
74
75        #[cfg(all(not(feature = "std"), feature = "compat_macros"))]
76        #[macro_export]
77        macro_rules! dbg {
78            () => {};
79            ($($val:expr),+) => { ($($val),+) }
80        }
81    // }
82// }