sqlx_core_oldapi/any/
feature_combinations.rs

1// Shared recursive macro to generate all non-empty combinations of feature flags.
2// Pass a list of entries with a feature name and an arbitrary payload which is
3// forwarded to the callback when that feature is selected.
4//
5// Usage:
6// for_all_feature_combinations!{
7//     entries: [("postgres", Postgres), ("mysql", MySql)],
8//     callback: my_callback
9// }
10// will expand to (for the active feature configuration):
11// #[cfg(all(feature="postgres"), not(feature="mysql"))] my_callback!(Postgres);
12// #[cfg(all(feature="mysql"), not(feature="postgres"))] my_callback!(MySql);
13// #[cfg(all(feature="postgres", feature="mysql"))] my_callback!(Postgres, MySql);
14// and so on for all non-empty subsets.
15#[macro_export]
16macro_rules! for_all_feature_combinations {
17    ( entries: [ $( ( $feat:literal, $payload:tt ) ),* $(,)? ], callback: $callback:ident ) => {
18        $crate::for_all_feature_combinations!(@recurse [] [] [ $( ( $feat, $payload ) )* ] $callback);
19    };
20
21    (@recurse [$($yes:tt)*] [$($no:tt)*] [ ( $feat:literal, $payload:tt ) $($rest:tt)* ] $callback:ident ) => {
22        $crate::for_all_feature_combinations!(@recurse [ $($yes)* ( $feat, $payload ) ] [ $($no)* ] [ $($rest)* ] $callback);
23        $crate::for_all_feature_combinations!(@recurse [ $($yes)* ] [ $($no)* $feat ] [ $($rest)* ] $callback);
24    };
25
26    // Base case: at least one selected
27    (@recurse [ $( ( $yfeat:literal, $ypayload:tt ) )+ ] [ $( $nfeat:literal )* ] [] $callback:ident ) => {
28        #[cfg(all( $( feature = $yfeat ),+ $(, not(feature = $nfeat ))* ))]
29        $callback!( $( $ypayload ),+ );
30    };
31
32    // Base case: none selected (skip)
33    (@recurse [] [ $( $nfeat:literal )* ] [] $callback:ident ) => {};
34}