|
| 1 | +// Copyright 2019-2020 Parity Technologies (UK) Ltd. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use alloc::vec::Vec; |
| 16 | +use proc_macro2::Ident; |
| 17 | +use syn::{ |
| 18 | + parse_quote, |
| 19 | + punctuated::Punctuated, |
| 20 | + spanned::Spanned, |
| 21 | + visit::Visit, |
| 22 | + Generics, |
| 23 | + Result, |
| 24 | + Type, |
| 25 | +}; |
| 26 | + |
| 27 | +/// Adds a `TypeInfo + 'static` bound to all relevant generic types including |
| 28 | +/// associated types (e.g. `T::A: TypeInfo`), correctly dealing with |
| 29 | +/// self-referential types. |
| 30 | +pub fn add(input_ident: &Ident, generics: &mut Generics, data: &syn::Data) -> Result<()> { |
| 31 | + let ty_params = generics.type_params_mut().fold(Vec::new(), |mut acc, p| { |
| 32 | + p.bounds.push(parse_quote!(::scale_info::TypeInfo)); |
| 33 | + p.bounds.push(parse_quote!('static)); |
| 34 | + acc.push(p.ident.clone()); |
| 35 | + acc |
| 36 | + }); |
| 37 | + |
| 38 | + if ty_params.is_empty() { |
| 39 | + return Ok(()) |
| 40 | + } |
| 41 | + |
| 42 | + let types = collect_types_to_bind(input_ident, data, &ty_params)?; |
| 43 | + |
| 44 | + if !types.is_empty() { |
| 45 | + let where_clause = generics.make_where_clause(); |
| 46 | + |
| 47 | + types.into_iter().for_each(|ty| { |
| 48 | + where_clause |
| 49 | + .predicates |
| 50 | + .push(parse_quote!(#ty : ::scale_info::TypeInfo + 'static)) |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + Ok(()) |
| 55 | +} |
| 56 | + |
| 57 | +/// Visits the ast and checks if the given type contains one of the given |
| 58 | +/// idents. |
| 59 | +fn type_contains_idents(ty: &Type, idents: &[Ident]) -> bool { |
| 60 | + struct ContainIdents<'a> { |
| 61 | + result: bool, |
| 62 | + idents: &'a [Ident], |
| 63 | + } |
| 64 | + |
| 65 | + impl<'a, 'ast> Visit<'ast> for ContainIdents<'a> { |
| 66 | + fn visit_ident(&mut self, i: &'ast Ident) { |
| 67 | + if self.idents.iter().any(|id| id == i) { |
| 68 | + self.result = true; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + let mut visitor = ContainIdents { |
| 74 | + result: false, |
| 75 | + idents, |
| 76 | + }; |
| 77 | + visitor.visit_type(ty); |
| 78 | + visitor.result |
| 79 | +} |
| 80 | + |
| 81 | +/// Returns all types that must be added to the where clause with the respective |
| 82 | +/// trait bound. |
| 83 | +fn collect_types_to_bind( |
| 84 | + input_ident: &Ident, |
| 85 | + data: &syn::Data, |
| 86 | + ty_params: &[Ident], |
| 87 | +) -> Result<Vec<Type>> { |
| 88 | + let types_from_fields = |fields: &Punctuated<syn::Field, _>| -> Vec<syn::Type> { |
| 89 | + fields |
| 90 | + .iter() |
| 91 | + .filter(|field| { |
| 92 | + // Only add a bound if the type uses a generic. |
| 93 | + type_contains_idents(&field.ty, &ty_params) |
| 94 | + && |
| 95 | + // Remove all remaining types that start/contain the input ident |
| 96 | + // to not have them in the where clause. |
| 97 | + !type_contains_idents(&field.ty, &[input_ident.clone()]) |
| 98 | + }) |
| 99 | + .map(|f| f.ty.clone()) |
| 100 | + .collect() |
| 101 | + }; |
| 102 | + |
| 103 | + let types = match *data { |
| 104 | + syn::Data::Struct(ref data) => { |
| 105 | + match &data.fields { |
| 106 | + syn::Fields::Named(syn::FieldsNamed { named: fields, .. }) |
| 107 | + | syn::Fields::Unnamed(syn::FieldsUnnamed { |
| 108 | + unnamed: fields, .. |
| 109 | + }) => types_from_fields(fields), |
| 110 | + syn::Fields::Unit => Vec::new(), |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + syn::Data::Enum(ref data) => { |
| 115 | + data.variants |
| 116 | + .iter() |
| 117 | + .flat_map(|variant| { |
| 118 | + match &variant.fields { |
| 119 | + syn::Fields::Named(syn::FieldsNamed { |
| 120 | + named: fields, .. |
| 121 | + }) |
| 122 | + | syn::Fields::Unnamed(syn::FieldsUnnamed { |
| 123 | + unnamed: fields, |
| 124 | + .. |
| 125 | + }) => types_from_fields(fields), |
| 126 | + syn::Fields::Unit => Vec::new(), |
| 127 | + } |
| 128 | + }) |
| 129 | + .collect() |
| 130 | + } |
| 131 | + |
| 132 | + syn::Data::Union(ref data) => { |
| 133 | + return Err(syn::Error::new( |
| 134 | + data.union_token.span(), |
| 135 | + "Union types are not supported.", |
| 136 | + )) |
| 137 | + } |
| 138 | + }; |
| 139 | + |
| 140 | + Ok(types) |
| 141 | +} |
0 commit comments