cxxbridge_macro/
attrs.rs

1use crate::syntax::attrs::OtherAttrs;
2use proc_macro2::TokenStream;
3use quote::ToTokens;
4use syn::Attribute;
5
6impl OtherAttrs {
7    pub(crate) fn all(&self) -> PrintOtherAttrs {
8        PrintOtherAttrs {
9            attrs: self,
10            cfg: true,
11            lint: true,
12            passthrough: true,
13        }
14    }
15
16    pub(crate) fn cfg(&self) -> PrintOtherAttrs {
17        PrintOtherAttrs {
18            attrs: self,
19            cfg: true,
20            lint: false,
21            passthrough: false,
22        }
23    }
24
25    pub(crate) fn cfg_and_lint(&self) -> PrintOtherAttrs {
26        PrintOtherAttrs {
27            attrs: self,
28            cfg: true,
29            lint: true,
30            passthrough: false,
31        }
32    }
33}
34
35pub(crate) struct PrintOtherAttrs<'a> {
36    attrs: &'a OtherAttrs,
37    cfg: bool,
38    lint: bool,
39    passthrough: bool,
40}
41
42impl<'a> ToTokens for PrintOtherAttrs<'a> {
43    fn to_tokens(&self, tokens: &mut TokenStream) {
44        if self.cfg {
45            print_attrs_as_outer(&self.attrs.cfg, tokens);
46        }
47        if self.lint {
48            print_attrs_as_outer(&self.attrs.lint, tokens);
49        }
50        if self.passthrough {
51            print_attrs_as_outer(&self.attrs.passthrough, tokens);
52        }
53    }
54}
55
56fn print_attrs_as_outer(attrs: &[Attribute], tokens: &mut TokenStream) {
57    for attr in attrs {
58        let Attribute {
59            pound_token,
60            style,
61            bracket_token,
62            meta,
63        } = attr;
64        pound_token.to_tokens(tokens);
65        let _ = style; // ignore; render outer and inner attrs both as outer
66        bracket_token.surround(tokens, |tokens| meta.to_tokens(tokens));
67    }
68}