const_dispatch/
primitive.rs

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
//! The "`impl`s" for primitive types such as `bool` and `u8`
//!
//! ```rust, ignore
//! /// Pseudo-code
//! mod ::const_dispatch::primitive {
//!     impl ConstDispatch for bool { /* magic */ }
//!     impl ConstDispatch for u8 { /* magic */ }
//! }
//! ```
//!
//! ## Example
//!
//! ```rust
//! use ::const_dispatch::{const_dispatch, primitive::bool};
//!
//! fn inner<const VERBOSE: bool>() {
//!     // ...
//! }
//!
//! fn main() {
//!     let verbose = ::std::env::var("VERBOSE").map_or(false, |s| s == "1");
//!     const_dispatch!(verbose, |const VERBOSE: bool| {
//!         inner::<VERBOSE>()
//!     })
//! }
//! ```
//!
//! Make sure to import these (either individually, or as a blob import from the
//! <code>[crate::prelude]::*</code>) in order for [`const_dispatch!`][crate::const_dispatch] to
//! work with `u8` and `bool` properly.

#[doc(hidden)]
#[macro_export]
macro_rules! bool {
    (
        $scrutinee:expr, |const $C:ident| $body:block
    ) => (
        match $scrutinee {
            | true => {
                const $C: ::core::primitive::bool = true;
                $body
            },
            | false => {
                const $C: ::core::primitive::bool = false;
                $body
            },
        }
    );

    (
        $scrutinee:expr,
        $macro_input:tt => $macro_output:tt
    ) => ({
        macro_rules! __emit__ { $macro_input => $macro_output }
        match $scrutinee {
            | true => {
                emit! {
                    true
                }
            },
            | false => {
                emit! {
                    false
                }
            },
        }
    });
}

#[doc(no_inline)]
pub use bool;

impl crate::ඞ::MacroDerived for bool {} /* as bool */

::const_dispatch_proc_macros::ඞimpl_for_u8!();

#[doc(no_inline)]
pub use u8;

impl crate::ඞ::MacroDerived for u8 {} /* as u8 */