• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use proc_macro2::TokenStream;
2 use quote::ToTokens;
3 use std::collections::btree_map::Entry;
4 use std::collections::{BTreeMap as Map, BTreeSet as Set};
5 use syn::punctuated::Punctuated;
6 use syn::{parse_quote, GenericArgument, Generics, Ident, PathArguments, Token, Type, WhereClause};
7 
8 pub struct ParamsInScope<'a> {
9     names: Set<&'a Ident>,
10 }
11 
12 impl<'a> ParamsInScope<'a> {
new(generics: &'a Generics) -> Self13     pub fn new(generics: &'a Generics) -> Self {
14         ParamsInScope {
15             names: generics.type_params().map(|param| &param.ident).collect(),
16         }
17     }
18 
intersects(&self, ty: &Type) -> bool19     pub fn intersects(&self, ty: &Type) -> bool {
20         let mut found = false;
21         crawl(self, ty, &mut found);
22         found
23     }
24 }
25 
crawl(in_scope: &ParamsInScope, ty: &Type, found: &mut bool)26 fn crawl(in_scope: &ParamsInScope, ty: &Type, found: &mut bool) {
27     if let Type::Path(ty) = ty {
28         if let Some(qself) = &ty.qself {
29             crawl(in_scope, &qself.ty, found);
30         } else {
31             let front = ty.path.segments.first().unwrap();
32             if front.arguments.is_none() && in_scope.names.contains(&front.ident) {
33                 *found = true;
34             }
35         }
36         for segment in &ty.path.segments {
37             if let PathArguments::AngleBracketed(arguments) = &segment.arguments {
38                 for arg in &arguments.args {
39                     if let GenericArgument::Type(ty) = arg {
40                         crawl(in_scope, ty, found);
41                     }
42                 }
43             }
44         }
45     }
46 }
47 
48 pub struct InferredBounds {
49     bounds: Map<String, (Set<String>, Punctuated<TokenStream, Token![+]>)>,
50     order: Vec<TokenStream>,
51 }
52 
53 impl InferredBounds {
new() -> Self54     pub fn new() -> Self {
55         InferredBounds {
56             bounds: Map::new(),
57             order: Vec::new(),
58         }
59     }
60 
insert(&mut self, ty: impl ToTokens, bound: impl ToTokens)61     pub fn insert(&mut self, ty: impl ToTokens, bound: impl ToTokens) {
62         let ty = ty.to_token_stream();
63         let bound = bound.to_token_stream();
64         let entry = self.bounds.entry(ty.to_string());
65         if let Entry::Vacant(_) = entry {
66             self.order.push(ty);
67         }
68         let (set, tokens) = entry.or_default();
69         if set.insert(bound.to_string()) {
70             tokens.push(bound);
71         }
72     }
73 
augment_where_clause(&self, generics: &Generics) -> WhereClause74     pub fn augment_where_clause(&self, generics: &Generics) -> WhereClause {
75         let mut generics = generics.clone();
76         let where_clause = generics.make_where_clause();
77         for ty in &self.order {
78             let (_set, bounds) = &self.bounds[&ty.to_string()];
79             where_clause.predicates.push(parse_quote!(#ty: #bounds));
80         }
81         generics.where_clause.unwrap()
82     }
83 }
84