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
//! Defines the [`maybe_const_fn`] macro.

/// Mark a function as `const` if a `cfg!(...)` attribute evaluates to true.
///
/// This has the same effect as using `#[rustversion::attr(version_test, const)]`,
/// but is implemented as a declarative macro instead of a procedural one.
///
/// It is subject to some minor limitations (see below).
///
/// If these are unacceptable,
/// please use the `rustversion` procedural macro.
/// That is more robust and supports other markers like `unsafe` and `async`.
///
/// ## Example
/// ```
/// # use rustversion_detect::maybe_const_fn;
///
/// maybe_const_fn! {
///     #[cfg_const(all())] // always true
///     /// Example documentation
///     #[inline] // Another attribute
///     const fn example() -> u32 {
///         3 + 7
///     }
///
///     #[cfg_const(any())] // always false
///     const fn not_const() -> Vec<u32> {
///         vec![3, 7, 8]
///     }
/// }
///
/// const FOO: u32 = example();
/// ```
///
/// ## Limitations
/// Please remember to always place `const` before the `fn` declaration.
/// Otherwise, the macro will give a value error.
///
/// The `#[cfg_const(...)]` marker must be the first attitude declaration.
/// All other attributes and documentation comments must come after the `#[cfg_const(..)]` declartion.
///
/// The following two examples are **broken**:
/// ```compile_fail
/// # use rustversion_detect::maybe_const_fn;
///
/// maybe_const_fn! {
///     /// doc comment first
///     #[cfg_const(all())]
///     /// Example documentation
///     #[inline] // Another attribute
///     const unsafe fn example() -> u32 {
///         3 + 7
///     }
/// }
/// ```
///
/// ```compile_fail
/// # use rustversion_detect::maybe_const_fn;
///
/// maybe_const_fn! {
///     #[inline] // attribute first
///     #[cfg_const(all())]
///     /// Example documentation
///     #[inline] // Another attribute
///     const unsafe fn example() -> u32 {
///         3 + 7
///     }
/// }
/// ```
///
/// ## Additional markers (`unsafe`, `async`, etc..)
/// Additional markers like `async`, `unsafe`, and `extern "C"`
/// must be surrounded by `{...}` due to macro limitations.
///
/// This is **correct**:
/// ```
/// # use rustversion_detect::maybe_const_fn;
///
/// maybe_const_fn! {
///     #[cfg_const(all())] // always true
///     /// Example documentation
///     #[inline] // Another attribute
///     const {unsafe} fn example() -> u32 {
///         3 + 7
///     }
/// }
///
/// const FOO: u32 = unsafe { example() };
/// ```
///
/// This is **broken**:
/// ```compile_fail
/// # use rustversion_detect::maybe_const_fn;
///
/// maybe_const_fn! {
///     #[cfg_const(all())] // always true
///     /// Example documentation
///     #[inline] // Another attribute
///     const unsafe fn example() -> u32 {
///         3 + 7
///     }
/// }
///
/// const FOO: u32 = unsafe { example() };
/// ```
///
/// ### Macro Forwarding
/// When [forwarding a matched fragment] inside another macro,
/// the outer macro cannot use fragment specifiers like `item`
/// for the constant function declaration.
/// As explained in the docs,
/// using the `tt` and `ident` fragment specifiers are a special exception.
///
/// The following is **broken**:
/// ```compile_fail
/// # use rustversion_detect::maybe_const_fn;
///
/// macro_rules! item_spec {
///     ($bad:item) => {
///         maybe_const_fn! {
///             #[cfg_const(all())] // always true
///             $bad
///         }
///     }
/// }
///
/// // will give the following error message:
/// // > captured metavariables except for `:tt`, `:ident` and `:lifetime` cannot be compared to other tokens
/// item_spec! {
///     const fn foo() {}
/// }
/// ```
///
/// [forwarding a matched fragment]: https://doc.rust-lang.org/reference/macros-by-example.html#forwarding-a-matched-fragment
#[macro_export]
macro_rules! maybe_const_fn {
    ($(
        #[cfg_const($cond:meta)]
        $(#[$attr:meta])*
        $visibility:vis const
        // extra "specifiers" like `async` `unsafe` `extern "C"`
        // needs to be surrounded with {...} due to macro limitations
        $({$($extra_spec:tt)*})?
        fn $name:ident ($($args:tt)*) $( -> $return_tp:ty)? $code:block
    )*) => {$(
        #[cfg($cond)]
        $(#[$attr])*
        $visibility const $($($extra_spec)*)* fn $name ( $($args)* ) $(-> $return_tp)* $code

        #[cfg(not($cond))]
        $(#[$attr])*
        $visibility $($($extra_spec)*)* fn $name ( $($args)* ) $(-> $return_tp)* $code
    )*};
}