aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
blob: d96c4f435e21322ca1646bb22b12eded45fddd19 (plain)
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
macro_rules! trait_alias {
    (
        $(#[$attr: meta])*
        $visibility: vis $name: ident$(<$($type_param: ident),*>)?
            $(: $first_bound: tt $(+ $bound: tt)*)?;
    ) => {
        $(#[$attr])*
        $visibility trait $name $(<$($type_param),*>)? $(: $first_bound $(+ $bound)*)? {}

        impl<
            T: $($first_bound $(+ $bound)*)?,
            $($($type_param),*)?
        > $name$(<$($type_param),*>)? for T {}
    };

    // Rule where the trait bounds will only be applied if the specified feature is enabled
    (
        bounds_when_feature = $bounds_feature: literal,
        $(#[$attr: meta])*
        $visibility: vis $name: ident$(<$($type_param: ident),*>)?
            $(: $first_bound: tt $(+ $bound: tt)*)?;
    ) => {
        #[cfg(feature = $bounds_feature)]
        trait_alias! {
            $(#[$attr])*
            $visibility $name$(<$($type_param),*>)?
                $(: $first_bound $(+ $bound)*)?;
        }

        #[cfg(not(feature = $bounds_feature))]
        trait_alias! {
            $(#[$attr])*
            $visibility $name$(<$($type_param),*>)?;
        }
    }
}

macro_rules! feature_alternate {
    (
        feature = $feature: literal,
        $(#[doc = $doc: literal])*
        when_enabled = $when_enabled: item,
        when_disabled = $when_disabled: item
    ) => {
        $(#[doc = $doc])*
        #[cfg(feature = $feature)]
        $when_enabled

        $(#[doc = $doc])*
        #[cfg(not(feature = $feature))]
        $when_disabled
    };
}

pub(crate) use {feature_alternate, trait_alias};