forked from launchbadge/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathfeature_combinations.rs
More file actions
34 lines (31 loc) · 1.67 KB
/
feature_combinations.rs
File metadata and controls
34 lines (31 loc) · 1.67 KB
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
// Shared recursive macro to generate all non-empty combinations of feature flags.
// Pass a list of entries with a feature name and an arbitrary payload which is
// forwarded to the callback when that feature is selected.
//
// Usage:
// for_all_feature_combinations!{
// entries: [("postgres", Postgres), ("mysql", MySql)],
// callback: my_callback
// }
// will expand to (for the active feature configuration):
// #[cfg(all(feature="postgres"), not(feature="mysql"))] my_callback!(Postgres);
// #[cfg(all(feature="mysql"), not(feature="postgres"))] my_callback!(MySql);
// #[cfg(all(feature="postgres", feature="mysql"))] my_callback!(Postgres, MySql);
// and so on for all non-empty subsets.
#[macro_export]
macro_rules! for_all_feature_combinations {
( entries: [ $( ( $feat:literal, $payload:tt ) ),* $(,)? ], callback: $callback:ident ) => {
$crate::for_all_feature_combinations!(@recurse [] [] [ $( ( $feat, $payload ) )* ] $callback);
};
(@recurse [$($yes:tt)*] [$($no:tt)*] [ ( $feat:literal, $payload:tt ) $($rest:tt)* ] $callback:ident ) => {
$crate::for_all_feature_combinations!(@recurse [ $($yes)* ( $feat, $payload ) ] [ $($no)* ] [ $($rest)* ] $callback);
$crate::for_all_feature_combinations!(@recurse [ $($yes)* ] [ $($no)* $feat ] [ $($rest)* ] $callback);
};
// Base case: at least one selected
(@recurse [ $( ( $yfeat:literal, $ypayload:tt ) )+ ] [ $( $nfeat:literal )* ] [] $callback:ident ) => {
#[cfg(all( $( feature = $yfeat ),+ $(, not(feature = $nfeat ))* ))]
$callback!( $( $ypayload ),+ );
};
// Base case: none selected (skip)
(@recurse [] [ $( $nfeat:literal )* ] [] $callback:ident ) => {};
}