• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Alorel / impl_index-rs / 14553724254

19 Apr 2025 11:13PM UTC coverage: 96.479% (-1.0%) from 97.512%
14553724254

push

github

Alorel
ci: Add `unexpected_cfgs` lint config

137 of 142 relevant lines covered (96.48%)

5.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.3
/src/tokenise.rs
1
use proc_macro2::{Ident, Punct, Spacing, Span, TokenStream};
2
use quote::{quote, ToTokens, TokenStreamExt};
3
use syn::Token;
4

5
use crate::{IndexMacro, IndexedPairing, MaybeIdent, Pairing, Pattern};
6

7
impl IndexMacro {
8
    pub fn into_token_stream(self) -> TokenStream {
2✔
9
        let Self {
2✔
10
            impl_mut,
2✔
11
            generics,
2✔
12
            idx_by,
2✔
13
            impl_for,
2✔
14
            output,
2✔
15
            pairings,
2✔
16
        } = self;
2✔
17

18
        let (g1, g2, g3) = if let Some(ref generics) = generics {
2✔
19
            let (g1, g2, g3) = generics.split_for_impl();
×
20
            (Some(g1), Some(g2), Some(g3))
×
21
        } else {
22
            (None, None, None)
2✔
23
        };
24

25
        let inline = if pairings.len() < 3 {
2✔
26
            Some(quote!(#[inline]))
1✔
27
        } else {
28
            None
1✔
29
        };
30

31
        let impl_mut = if impl_mut {
2✔
32
            let mut_pairings = pairings.iter().map(|pairing| IndexedPairing {
4✔
33
                pairing,
4✔
34
                idx_by: &idx_by,
4✔
35
                mut_ref: Some(<Token![mut]>::default()),
4✔
36
            });
4✔
37

38
            Some(quote! {
1✔
39
                #[automatically_derived]
40
                impl #g1 ::core::ops::IndexMut<#idx_by> for #impl_for #g2 #g3 {
41
                    #inline
42
                    fn index_mut(&mut self, index_macro_derived_index_input: #idx_by) -> &mut Self::Output {
43
                        match index_macro_derived_index_input {
44
                            #(#mut_pairings),*
45
                        }
46
                    }
47
                }
48
            })
49
        } else {
50
            None
1✔
51
        };
52

53
        let immut_pairings = pairings.into_iter().map(|pairing| IndexedPairing {
5✔
54
            pairing,
5✔
55
            idx_by: &idx_by,
5✔
56
            mut_ref: None,
5✔
57
        });
5✔
58

59
        quote! {
2✔
60
            #[automatically_derived]
61
            impl #g1 ::core::ops::Index<#idx_by> for #impl_for #g2 #g3 {
62
                type Output = #output;
63

64
                #inline
65
                fn index(&self, index_macro_derived_index_input: #idx_by) -> &Self::Output {
66
                    match index_macro_derived_index_input {
67
                        #(#immut_pairings),*
68
                    }
69
                }
70
            }
71

72
            #impl_mut
73
        }
74
    }
2✔
75
}
76

77
impl<P: AsRef<Pairing>, T: ToTokens> ToTokens for IndexedPairing<P, T> {
78
    fn to_tokens(&self, tokens: &mut TokenStream) {
9✔
79
        let Pairing {
9✔
80
            enum_variant,
9✔
81
            struct_field,
9✔
82
        } = self.pairing.as_ref();
9✔
83
        let idx_by = &self.idx_by;
9✔
84
        let mut_ref = self.mut_ref;
9✔
85

9✔
86
        enum_variant.to_tokens(tokens, |variant, tokens| {
9✔
87
            idx_by.to_tokens(tokens);
3✔
88
            <Token![::]>::default().to_tokens(tokens);
3✔
89
            variant.to_tokens(tokens);
3✔
90
        });
3✔
91

92
        <Token![=>]>::default().to_tokens(tokens);
9✔
93

9✔
94
        tokens.append(Punct::new('&', Spacing::Joint));
9✔
95
        mut_ref.to_tokens(tokens);
9✔
96

9✔
97
        tokens.append(Ident::new("self", Span::call_site()));
9✔
98
        tokens.append(Punct::new('.', Spacing::Joint));
9✔
99
        struct_field.to_tokens(tokens, |ident, tokens| {
9✔
100
            ident.to_tokens(tokens);
7✔
101
        });
7✔
102
    }
9✔
103
}
104

105
impl<Other: ToTokens> MaybeIdent<Other> {
106
    pub fn to_tokens<F>(&self, tokens: &mut TokenStream, mut fmt_field: F)
18✔
107
    where
18✔
108
        F: FnMut(&Ident, &mut TokenStream),
18✔
109
    {
18✔
110
        match self {
18✔
111
            MaybeIdent::Ident(ident) => {
10✔
112
                fmt_field(ident, tokens);
10✔
113
            }
10✔
114
            MaybeIdent::Other(other) => {
8✔
115
                other.to_tokens(tokens);
8✔
116
            }
8✔
117
        }
118
    }
18✔
119
}
120

121
impl ToTokens for Pattern {
122
    fn to_tokens(&self, tokens: &mut TokenStream) {
6✔
123
        self.pat.to_tokens(tokens);
6✔
124
        if let Some((ref if_token, ref ty)) = self.guard {
6✔
125
            if_token.to_tokens(tokens);
2✔
126
            ty.to_tokens(tokens);
2✔
127
        }
4✔
128
    }
6✔
129
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc