Skip to content

Commit f868bc5

Browse files
committed
Add the cfg_match! macro
1 parent c4f2577 commit f868bc5

File tree

4 files changed

+211
-0
lines changed

4 files changed

+211
-0
lines changed

library/core/src/macros/mod.rs

+86
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,92 @@ pub macro debug_assert_matches($($arg:tt)*) {
321321
}
322322
}
323323

324+
/// A macro for defining `#[cfg]` match-like statements.
325+
///
326+
/// It is similar to the `if/elif` C preprocessor macro by allowing definition of a cascade of
327+
/// `#[cfg]` cases, emitting the implementation which matches first.
328+
///
329+
/// This allows you to conveniently provide a long list `#[cfg]`'d blocks of code
330+
/// without having to rewrite each clause multiple times.
331+
///
332+
/// # Example
333+
///
334+
/// ```
335+
/// #![feature(cfg_match)]
336+
///
337+
/// cfg_match! {
338+
/// cfg(unix) => {
339+
/// fn foo() { /* unix specific functionality */ }
340+
/// }
341+
/// cfg(target_pointer_width = "32") => {
342+
/// fn foo() { /* non-unix, 32-bit functionality */ }
343+
/// }
344+
/// _ => {
345+
/// fn foo() { /* fallback implementation */ }
346+
/// }
347+
/// }
348+
/// ```
349+
#[macro_export]
350+
#[unstable(feature = "cfg_match", issue = "none")]
351+
#[rustc_diagnostic_item = "cfg_match"]
352+
macro_rules! cfg_match {
353+
// with a final wildcard
354+
(
355+
$(cfg($initial_meta:meta) => { $($initial_tokens:item)* })+
356+
_ => { $($extra_tokens:item)* }
357+
) => {
358+
cfg_match! {
359+
@__items ();
360+
$((($initial_meta) ($($initial_tokens)*)),)+
361+
(() ($($extra_tokens)*)),
362+
}
363+
};
364+
365+
// without a final wildcard
366+
(
367+
$(cfg($extra_meta:meta) => { $($extra_tokens:item)* })*
368+
) => {
369+
cfg_match! {
370+
@__items ();
371+
$((($extra_meta) ($($extra_tokens)*)),)*
372+
}
373+
};
374+
375+
// Internal and recursive macro to emit all the items
376+
//
377+
// Collects all the previous cfgs in a list at the beginning, so they can be
378+
// negated. After the semicolon is all the remaining items.
379+
(@__items ($($_:meta,)*);) => {};
380+
(
381+
@__items ($($no:meta,)*);
382+
(($($yes:meta)?) ($($tokens:item)*)),
383+
$($rest:tt,)*
384+
) => {
385+
// Emit all items within one block, applying an appropriate #[cfg]. The
386+
// #[cfg] will require all `$yes` matchers specified and must also negate
387+
// all previous matchers.
388+
#[cfg(all(
389+
$($yes,)?
390+
not(any($($no),*))
391+
))]
392+
cfg_match! { @__identity $($tokens)* }
393+
394+
// Recurse to emit all other items in `$rest`, and when we do so add all
395+
// our `$yes` matchers to the list of `$no` matchers as future emissions
396+
// will have to negate everything we just matched as well.
397+
cfg_match! {
398+
@__items ($($no,)* $($yes,)?);
399+
$($rest,)*
400+
}
401+
};
402+
403+
// Internal macro to make __apply work out right for different match types,
404+
// because of how macros match/expand stuff.
405+
(@__identity $($tokens:item)*) => {
406+
$($tokens)*
407+
};
408+
}
409+
324410
/// Returns whether the given expression matches any of the given patterns.
325411
///
326412
/// Like in a `match` expression, the pattern can be optionally followed by `if`

library/core/tests/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
#![feature(const_option_ext)]
9797
#![feature(const_result)]
9898
#![cfg_attr(target_has_atomic = "128", feature(integer_atomics))]
99+
#![cfg_attr(test, feature(cfg_match))]
99100
#![feature(int_roundings)]
100101
#![feature(slice_group_by)]
101102
#![feature(split_array)]
@@ -139,6 +140,7 @@ mod hash;
139140
mod intrinsics;
140141
mod iter;
141142
mod lazy;
143+
#[cfg(test)]
142144
mod macros;
143145
mod manually_drop;
144146
mod mem;

library/core/tests/macros.rs

+120
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
trait Trait {
2+
fn blah(&self);
3+
}
4+
5+
#[allow(dead_code)]
6+
struct Struct;
7+
8+
impl Trait for Struct {
9+
cfg_match! {
10+
cfg(feature = "blah") => {
11+
fn blah(&self) {
12+
unimplemented!();
13+
}
14+
}
15+
_ => {
16+
fn blah(&self) {
17+
unimplemented!();
18+
}
19+
}
20+
}
21+
}
22+
123
#[test]
224
fn assert_eq_trailing_comma() {
325
assert_eq!(1, 1,);
@@ -18,3 +40,101 @@ fn assert_ne_trailing_comma() {
1840
fn matches_leading_pipe() {
1941
matches!(1, | 1 | 2 | 3);
2042
}
43+
#[test]
44+
fn cfg_match_basic() {
45+
cfg_match! {
46+
cfg(target_pointer_width = "64") => { fn f0_() -> bool { true }}
47+
}
48+
cfg_match! {
49+
cfg(unix) => { fn f1_() -> bool { true }}
50+
cfg(any(target_os = "macos", target_os = "linux")) => { fn f1_() -> bool { false }}
51+
}
52+
53+
cfg_match! {
54+
cfg(target_pointer_width = "64") => { fn f2_() -> bool { true }}
55+
cfg(target_pointer_width = "32") => { fn f2_() -> bool { false }}
56+
}
57+
58+
cfg_match! {
59+
cfg(target_pointer_width = "16") => { fn f3_() -> i32 { 1 }}
60+
_ => { fn f3_() -> i32 { 2 }}
61+
}
62+
63+
#[cfg(target_pointer_width = "64")]
64+
assert!(f0_());
65+
#[cfg(unix)]
66+
assert!(f1_());
67+
assert!(f2_());
68+
assert_eq!(f3_(), 2);
69+
}
70+
71+
#[test]
72+
fn cfg_match_options() {
73+
cfg_match! {
74+
cfg(test) => {
75+
use core::option::Option as Option2;
76+
fn works1() -> Option2<u32> { Some(1) }
77+
}
78+
_ => { fn works1() -> Option<u32> { None } }
79+
}
80+
81+
cfg_match! {
82+
cfg(feature = "foo") => { fn works2() -> bool { false } }
83+
cfg(test) => { fn works2() -> bool { true } }
84+
_ => { fn works2() -> bool { false } }
85+
}
86+
87+
cfg_match! {
88+
cfg(feature = "foo") => { fn works3() -> bool { false } }
89+
_ => { fn works3() -> bool { true } }
90+
}
91+
92+
cfg_match! {
93+
cfg(test) => {
94+
use core::option::Option as Option3;
95+
fn works4() -> Option3<u32> { Some(1) }
96+
}
97+
}
98+
99+
cfg_match! {
100+
cfg(feature = "foo") => { fn works5() -> bool { false } }
101+
cfg(test) => { fn works5() -> bool { true } }
102+
}
103+
104+
assert!(works1().is_some());
105+
assert!(works2());
106+
assert!(works3());
107+
assert!(works4().is_some());
108+
assert!(works5());
109+
}
110+
111+
#[cfg(target_pointer_width = "64")]
112+
#[test]
113+
fn no_duplication_on_64() {
114+
cfg_match! {
115+
cfg(windows) => {
116+
fn foo() {}
117+
}
118+
cfg(unix) => {
119+
fn foo() {}
120+
}
121+
cfg(target_pointer_width = "64") => {
122+
fn foo() {}
123+
}
124+
}
125+
foo();
126+
}
127+
128+
#[test]
129+
fn test_usage_within_a_function() {
130+
cfg_match! {
131+
cfg(debug_assertions) => {
132+
assert!(cfg!(debug_assertions));
133+
assert_eq!(4, 2+2);
134+
}
135+
_ => {
136+
assert!(cfg!(not(debug_assertions)));
137+
assert_eq!(10, 5+5);
138+
}
139+
}
140+
}

library/std/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,9 @@ pub use core::{
644644
)]
645645
pub use core::concat_bytes;
646646

647+
#[unstable(feature = "cfg_match", issue = "none")]
648+
pub use core::cfg_match;
649+
647650
#[stable(feature = "core_primitive", since = "1.43.0")]
648651
pub use core::primitive;
649652

0 commit comments

Comments
 (0)