Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.
Merged
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
Fix tests
  • Loading branch information
ascjones committed May 6, 2021
commit 12611a6d166fca7da19504737ecf21d9669d688a
45 changes: 24 additions & 21 deletions core/src/generate_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ impl<'a> ToTokens for Module<'a> {
tokens.extend(quote! {
pub mod #name {
use super::#root_mod;
// todo: [AJ] maybe make a prelude to make no_std compatible
use std::collections::BTreeMap;

#( #modules )*
#( #types )*
Expand Down Expand Up @@ -294,12 +292,26 @@ impl<'a> ModuleType<'a> {
ty.parent_type_params(&mut used_type_params)
}
let type_params_set: HashSet<_> = type_params.iter().cloned().collect();
type_params_set
let mut unused = type_params_set
.difference(&used_type_params)
.cloned()
.collect()
.collect::<Vec<_>>();
unused.sort();
unused
}

let ty_toks = |ty_name: &str, ty_path: &TypePath| {
if ty_name.contains("Box<") {
// todo [AJ] remove this hack once scale-info can represent Box somehow
quote! { std::boxed::Box<#ty_path> }
} else if ty_name.contains("BTreeMap<") {
// todo [AJ] remove this hack and add namespaces or import prelude types
quote! { std::collections::#ty_path }
} else {
quote! { #ty_path }
}
};

if named {
let fields = fields
.iter()
Expand All @@ -316,12 +328,7 @@ impl<'a> ModuleType<'a> {
let mut fields_tokens = fields
.iter()
.map(|(name, ty, ty_name)| {
// todo [AJ] remove this hack once scale-info can represent Box somehow
let ty = if ty_name.contains("Box<") {
quote! { std::boxed::Box<#ty>}
} else {
quote! { #ty }
};
let ty = ty_toks(ty_name, ty);
if is_struct {
quote! { pub #name: #ty }
} else {
Expand Down Expand Up @@ -358,12 +365,8 @@ impl<'a> ModuleType<'a> {
.collect::<Vec<_>>();
let mut fields_tokens = type_paths
.iter()
.map(|(ty, type_name)| {
let ty = if type_name.contains("Box<") {
quote! { std::boxed::Box<#ty>}
} else {
quote! { #ty }
};
.map(|(ty, ty_name)| {
let ty = ty_toks(ty_name, ty);
if is_struct {
quote! { pub #ty }
} else {
Expand All @@ -378,7 +381,7 @@ impl<'a> ModuleType<'a> {
if is_struct {
if !unused_params.is_empty() {
fields_tokens
.push(quote! { pub core::marker::PhantomData<(#( #unused_params, )*)> })
.push(quote! { pub core::marker::PhantomData<(#( #unused_params ),*)> })
}
}

Expand Down Expand Up @@ -551,7 +554,7 @@ impl TypePathType {
}
}

#[derive(Clone, Debug, Eq, PartialEq, Hash)]
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct TypeParameter {
concrete_type_id: NonZeroU32,
name: proc_macro2::Ident,
Expand Down Expand Up @@ -901,7 +904,7 @@ mod tests {
#[derive(Debug, ::codec::Encode, ::codec::Decode)]
pub struct Foo<_0, _1> {
pub a: _0,
pub b: Option<(_0, _1)>,
pub b: Option<(_0, _1,)>,
}
}
}
Expand Down Expand Up @@ -953,8 +956,8 @@ mod tests {
}
#[derive(Debug, ::codec::Encode, ::codec::Decode)]
pub struct UnnamedFields<_0, _1> (
pub (u32, u32),
pub core::marker::PhantomData<(_0, _1,)>,
pub (u32, u32,),
pub core::marker::PhantomData<(_0, _1)>,
);
}
}
Expand Down