instability/
item_like.rs

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
use syn::Visibility;

pub trait Stability {
    #[allow(unused)]
    fn attrs(&self) -> &[syn::Attribute];

    fn push_attr(&mut self, attr: syn::Attribute);
}

pub trait ItemLike: Stability {
    fn visibility(&self) -> &Visibility;

    fn set_visibility(&mut self, visibility: Visibility);

    fn is_public(&self) -> bool {
        matches!(self.visibility(), Visibility::Public(_))
    }

    fn allowed_lints(&self) -> Vec<syn::Ident>;
}

/// Implement `ItemLike` for the given type.
///
/// This makes each of the syn::Item* types implement our `ItemLike` trait to make it possible to
/// work with them in a more uniform way.
///
/// A single type can be passed to this macro, or multiple types can be passed at once.
/// Each type can be passed with a list of lints that are allowed for that type (defaulting to
/// `dead_code` if not specified).
macro_rules! impl_item_like {
    // run impl_item_like for each item in a list of items
    ($($(#[allow($($lint:ident),*)])? $ty:ty ),+ ,) => {
        $(
            impl_item_like!($(#[allow($($lint),*)])? $ty );
        )*
    };

    // run impl_item_like for a single item without any lints
    ($ty:ty) => {
        impl_item_like!(#[allow(dead_code)] $ty );
    };

    // Implement `ItemLike` for the given type.
    (#[allow($($lint:ident),*)] $ty:ty) => {
        impl Stability for $ty {
            fn attrs(&self) -> &[syn::Attribute] {
                &self.attrs
            }

            fn push_attr(&mut self, attr: syn::Attribute) {
                self.attrs.push(attr);
            }
        }

        impl ItemLike for $ty {
            fn visibility(&self) -> &Visibility {
                &self.vis
            }

            fn set_visibility(&mut self, visibility: Visibility) {
                self.vis = visibility;
            }

            fn allowed_lints(&self) -> Vec<syn::Ident> {
                vec![
                    $(syn::Ident::new(stringify!($lint), proc_macro2::Span::call_site()),)*
                ]
            }
        }
    };

}

impl_item_like!(
    syn::ItemType,
    syn::ItemEnum,
    syn::ItemFn,
    syn::ItemMod,
    syn::ItemTrait,
    syn::ItemConst,
    syn::ItemStatic,
    #[allow(unused_imports)]
    syn::ItemUse,
);

impl Stability for syn::ItemStruct {
    fn attrs(&self) -> &[syn::Attribute] {
        &self.attrs
    }

    fn push_attr(&mut self, attr: syn::Attribute) {
        self.attrs.push(attr);
    }
}

impl ItemLike for syn::ItemStruct {
    fn visibility(&self) -> &Visibility {
        &self.vis
    }

    fn set_visibility(&mut self, visibility: Visibility) {
        // Also constrain visibility of all fields to be at most the given
        // item visibility.
        self.fields
            .iter_mut()
            .filter(|field| matches!(&field.vis, Visibility::Public(_)))
            .for_each(|field| field.vis = visibility.clone());

        self.vis = visibility;
    }

    fn allowed_lints(&self) -> Vec<syn::Ident> {
        vec![syn::Ident::new("dead_code", proc_macro2::Span::call_site())]
    }
}

impl Stability for syn::ItemImpl {
    fn attrs(&self) -> &[syn::Attribute] {
        &self.attrs
    }

    fn push_attr(&mut self, attr: syn::Attribute) {
        self.attrs.push(attr);
    }
}