Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use fold
fmt
  • Loading branch information
dvdplm committed Mar 1, 2021
commit fcf59dd4e0687e4309f79b8771527f119362036f
32 changes: 12 additions & 20 deletions derive/src/trait_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.


use alloc::vec::Vec;
use hashbrown::HashSet;
use proc_macro2::Ident;
Expand All @@ -21,8 +20,8 @@ use syn::{
punctuated::Punctuated,
spanned::Spanned,
visit::Visit,
Generics,
GenericArgument,
Generics,
Result,
Type,
WhereClause,
Expand Down Expand Up @@ -180,25 +179,18 @@ fn collect_types_to_bind(
) -> Result<Vec<(Type, bool, Option<Ident>)>> {
let types_from_fields =
|fields: &Punctuated<syn::Field, _>| -> Vec<(Type, bool, Option<Ident>)> {
fields
.iter()
// TODO: Rewrite this as a `fold` to remove multiple calls to `type_contains_ident`.
.filter(|field| {
// Only add a bound if the type uses a generic.
type_contains_idents(&field.ty, &ty_params).is_some()
&&
// Remove all remaining types that start/contain the input ident
fields.iter().fold(Vec::new(), |mut acc, field| {
// Only add a bound if the type uses a generic.
let uses_generic = type_contains_idents(&field.ty, &ty_params);
// Find remaining types that start with/contain the input ident
// to not have them in the where clause.
type_contains_idents(&field.ty, &[input_ident.clone()]).is_none()
})
.map(|f| {
(
f.ty.clone(),
super::is_compact(f),
type_contains_idents(&f.ty, &ty_params),
)
})
.collect()
let uses_input_ident =
type_contains_idents(&field.ty, &[input_ident.clone()]);
if uses_generic.is_some() && uses_input_ident.is_none() {
acc.push((field.ty.clone(), super::is_compact(field), uses_generic));
}
acc
})
};

let types = match *data {
Expand Down
15 changes: 9 additions & 6 deletions test_suite/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,9 @@ fn scale_compact_types_work_in_enums() {

#[test]
fn scale_compact_types_complex() {
trait Boo { type B: TypeInfo; }
trait Boo {
type B: TypeInfo;
}
impl Boo for u8 {
type B = bool;
}
Expand All @@ -299,11 +301,12 @@ fn scale_compact_types_complex() {
let ty = Type::builder()
.path(Path::new("A", "derive"))
.type_params(tuple_meta_type![u8, u16])
.composite(Fields::named()
.field_of::<PhantomData<u8>>("one", "PhantomData<T>")
.field_of::<PhantomData<u16>>("two", "PhantomData<U>")
.compact_of::<u8>("three", "T")
.field_of::<bool>("four", "T::B")
.composite(
Fields::named()
.field_of::<PhantomData<u8>>("one", "PhantomData<T>")
.field_of::<PhantomData<u16>>("two", "PhantomData<U>")
.compact_of::<u8>("three", "T")
.field_of::<bool>("four", "T::B"),
);

assert_type!(A<u8, u16>, ty);
Expand Down