Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
37c0a57
Parameterize CompactForm String for optional SCALE impl
ascjones Dec 7, 2020
643f09d
Merge remote-tracking branch 'origin/master' into aj-compact-string
dvdplm Dec 14, 2020
9a7ccbf
Fix no-std compilation
dvdplm Dec 14, 2020
254fee1
Obey the fmt
dvdplm Dec 14, 2020
e74e4f9
Introduce String trait for Form
ascjones Dec 16, 2020
7860c79
Rename "Compact" to "Frozen" (and associated fallout)
dvdplm Dec 16, 2020
579f958
Docs cleanup and more renames
dvdplm Dec 17, 2020
8333e5a
Cleanup
dvdplm Dec 17, 2020
2818f7b
More cleanup
dvdplm Dec 17, 2020
7706a38
Merge branch 'aj-compact-string' into dp-rename-compact-to-frozen
dvdplm Dec 17, 2020
e03a2cd
obey the fmt
dvdplm Dec 17, 2020
3a95663
Add a `compact` flag to `Field` to indicate that this type is to be e…
dvdplm Dec 17, 2020
004e107
Clippy warnings
dvdplm Dec 17, 2020
93a9aeb
Acommodate older clippy
dvdplm Dec 17, 2020
6569e50
Derive (scale) compact fields
dvdplm Dec 28, 2020
f098101
Merge branch 'master' into dp-flag-types-as-compact
dvdplm Jan 4, 2021
eda2769
Merge remote-tracking branch 'origin/master' into dp-flag-types-as-co…
dvdplm Jan 5, 2021
09c1241
WIP
dvdplm Jan 8, 2021
55f1413
Sort out the TypeInfo impl for Compact<T> (ty @ascjones!)
dvdplm Jan 8, 2021
e2397bf
Clean up conditionals a bit
dvdplm Jan 11, 2021
d15bf25
Add Compact to prelude
dvdplm Jan 11, 2021
1f4f8d3
Cleanup
dvdplm Jan 11, 2021
02ed1a3
fmt
dvdplm Jan 11, 2021
80a8b27
Merge branch 'master' into dp-add-TypeDef-to-handle-Compact-types
dvdplm Jan 18, 2021
7389297
fmt
dvdplm Jan 18, 2021
d8506dc
Sort out TODOs
dvdplm Jan 18, 2021
ab56118
Remove unused top-level way of adding a Compact
dvdplm Jan 19, 2021
5436755
Merge remote-tracking branch 'origin/master' into dp-add-TypeDef-to-h…
dvdplm Jan 27, 2021
c70f31c
Merge remote-tracking branch 'origin/master' into dp-add-TypeDef-to-h…
dvdplm Jan 29, 2021
e656489
Merge remote-tracking branch 'origin/master' into dp-add-TypeDef-to-h…
dvdplm Jan 29, 2021
1c0c05d
Remove unneeded bound
dvdplm Jan 29, 2021
693b23a
Remove `Compact` from the prelude
dvdplm Jan 29, 2021
c2a38f5
Resolve TODO
dvdplm Jan 29, 2021
5649a66
fmt
dvdplm Jan 29, 2021
aad3277
Use <T as HasCompact>::Type rather than Compact<T> (ty @thiolliere!)
dvdplm Jan 29, 2021
4b18500
Split long comment
dvdplm Jan 31, 2021
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 <T as HasCompact>::Type rather than Compact<T> (ty @thiolliere!)
  • Loading branch information
dvdplm committed Jan 29, 2021
commit aad32771bc4b5d8aa853de5dd2fb5c135781367d
28 changes: 19 additions & 9 deletions derive/src/trait_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,20 @@ pub fn make_where_clause<'a>(

let types = collect_types_to_bind(input_ident, data, &ty_params_ids)?;

types.into_iter().for_each(|ty| {
where_clause
.predicates
.push(parse_quote!(#ty : ::scale_info::TypeInfo + 'static))
types.into_iter().for_each(|(ty, is_compact)| {
// Compact types need extra bounds, T: HasCompact and <T as HasCompact>::Type: TypeInfo + 'static
if is_compact {
where_clause
.predicates
.push(parse_quote!(#ty : ::scale::HasCompact));
where_clause
.predicates
.push(parse_quote!(<#ty as ::scale::HasCompact>::Type : ::scale_info::TypeInfo + 'static));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think our use of ::scale is becoming a problem. ink! imports parity-scale-codec crate as scale but from what I remember Substrate imports it as codec. We need a solution that works for both. Probably Basti's https://crates.io/crates/proc-macro-crate could help here. For now I am fine and we can fix this later.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this also seem to break primitive-types: paritytech/parity-common#519

} else {
where_clause
.predicates
.push(parse_quote!(#ty : ::scale_info::TypeInfo + 'static));
}
});

generics.type_params().into_iter().for_each(|type_param| {
Expand Down Expand Up @@ -94,14 +104,14 @@ fn type_contains_idents(ty: &Type, idents: &[Ident]) -> bool {
visitor.result
}

/// Returns all types that must be added to the where clause with the respective
/// trait bound.
/// Returns all types that must be added to the where clause with a boolean
/// indicating if the field is [`scale::Compact`] or not.
fn collect_types_to_bind(
input_ident: &Ident,
data: &syn::Data,
ty_params: &[Ident],
) -> Result<Vec<Type>> {
let types_from_fields = |fields: &Punctuated<syn::Field, _>| -> Vec<syn::Type> {
) -> Result<Vec<(Type, bool)>> {
let types_from_fields = |fields: &Punctuated<syn::Field, _>| -> Vec<(Type, bool)> {
fields
.iter()
.filter(|field| {
Expand All @@ -112,7 +122,7 @@ fn collect_types_to_bind(
// to not have them in the where clause.
!type_contains_idents(&field.ty, &[input_ident.clone()])
})
.map(|f| f.ty.clone())
.map(|f| (f.ty.clone(), super::is_compact(f)))
.collect()
};

Expand Down
6 changes: 4 additions & 2 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ impl FieldsBuilder<NamedFields> {
/// Add a named, [`Compact`] field of type `T`.
pub fn compact_of<T>(mut self, name: &'static str, type_name: &'static str) -> Self
where
T: TypeInfo + 'static,
T: scale::HasCompact,
<T as scale::HasCompact>::Type: TypeInfo + 'static,
{
self.fields
.push(Field::compact_of::<T>(Some(name), type_name));
Expand All @@ -284,7 +285,8 @@ impl FieldsBuilder<UnnamedFields> {
/// Add an unnamed, [`Compact`] field of type `T`.
pub fn compact_of<T>(mut self, type_name: &'static str) -> Self
where
T: TypeInfo + 'static,
T: scale::HasCompact,
<T as scale::HasCompact>::Type: TypeInfo + 'static,
{
self.fields.push(Field::compact_of::<T>(None, type_name));
self
Expand Down
4 changes: 4 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ fn prelude_items() {
)
);
assert_type!(PhantomData<i32>, TypeDefPhantom::new(meta_type::<i32>()));
}

#[test]
fn scale_compact_types() {
assert_type!(Compact<i32>, TypeDefCompact::new(meta_type::<i32>()))
}

Expand Down
6 changes: 4 additions & 2 deletions src/ty/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::{
use scale::{
Decode,
Encode,
HasCompact,
};
#[cfg(feature = "serde")]
use serde::{
Expand Down Expand Up @@ -133,9 +134,10 @@ impl Field {
/// Creates a new [`Compact`] field.
pub fn compact_of<T>(name: Option<&'static str>, type_name: &'static str) -> Field
where
T: TypeInfo + 'static,
T: HasCompact,
<T as HasCompact>::Type: TypeInfo + 'static,
{
Self::new(name, MetaType::new::<scale::Compact<T>>(), type_name)
Self::new(name, MetaType::new::<<T as HasCompact>::Type>(), type_name)
}
}

Expand Down