const_dispatch/primitive.rs
1//! The "`impl`s" for primitive types such as `bool` and `u8`
2//!
3//! ```rust, ignore
4//! /// Pseudo-code
5//! mod ::const_dispatch::primitive {
6//! impl ConstDispatch for bool { /* magic */ }
7//! impl ConstDispatch for u8 { /* magic */ }
8//! }
9//! ```
10//!
11//! ## Example
12//!
13//! ```rust
14//! use ::const_dispatch::{const_dispatch, primitive::bool};
15//!
16//! fn inner<const VERBOSE: bool>() {
17//! // ...
18//! }
19//!
20//! fn main() {
21//! let verbose = ::std::env::var("VERBOSE").map_or(false, |s| s == "1");
22//! const_dispatch!(verbose, |const VERBOSE: bool| {
23//! inner::<VERBOSE>()
24//! })
25//! }
26//! ```
27//!
28//! Make sure to import these (either individually, or as a blob import from the
29//! <code>[crate::prelude]::*</code>) in order for [`const_dispatch!`][crate::const_dispatch] to
30//! work with `u8` and `bool` properly.
31
32#[doc(hidden)]
33#[macro_export]
34macro_rules! bool {
35 (
36 $scrutinee:expr, |const $C:ident| $body:block
37 ) => (
38 match $scrutinee {
39 | true => {
40 const $C: ::core::primitive::bool = true;
41 {
42 $crate::ඞ::try_hide!($scrutinee);
43 $body
44 }
45 },
46 | false => {
47 const $C: ::core::primitive::bool = false;
48 {
49 $crate::ඞ::try_hide!($scrutinee);
50 $body
51 }
52 },
53 }
54 );
55
56 (
57 $scrutinee:expr,
58 $macro_input:tt => $macro_output:tt
59 ) => ({
60 macro_rules! __emit__ { $macro_input => $macro_output }
61 match $scrutinee {
62 | true => {
63 $crate::ඞ::try_hide!($scrutinee);
64 emit! {
65 true
66 }
67 },
68 | false => {
69 $crate::ඞ::try_hide!($scrutinee);
70 emit! {
71 false
72 }
73 },
74 }
75 });
76}
77
78#[doc(no_inline)]
79pub use bool;
80
81impl crate::ඞ::MacroDerived for bool {} /* as bool */
82
83::const_dispatch_proc_macros::ඞimpl_for_u8!();
84
85#[doc(no_inline)]
86pub use u8;
87
88impl crate::ඞ::MacroDerived for u8 {} /* as u8 */