Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
39ee6b4
Add new top-level attribute `scale_info(bounds(T: SomeTrait + OtherTr…
dvdplm May 1, 2021
b0eda68
Fmt
ascjones May 4, 2021
bd6dd5d
cleanup
dvdplm May 6, 2021
171cd4c
Merge branch 'dp-custom-bound' of github.com:paritytech/scale-info in…
dvdplm May 6, 2021
030d407
Merge branch 'master' into dp-custom-bound
ascjones May 27, 2021
b937c9a
Skip type params prototype
ascjones May 28, 2021
725ceca
Merge branch 'master' into aj-skip-type-params
ascjones Jun 7, 2021
56edb9c
Fmt
ascjones Jun 7, 2021
f503781
Clippy
ascjones Jun 7, 2021
1743b48
Satisfy clippy
ascjones Jun 7, 2021
bf9cf6d
Fix skip type params
ascjones Jun 7, 2021
2d4f1e8
Fix UI test
ascjones Jun 7, 2021
9c4f6d6
Add some more tests
ascjones Jun 11, 2021
100dfcf
Add failing test for type params with default values
ascjones Jun 11, 2021
c92041b
Fix for type params with defaults, compare on ident
ascjones Jun 11, 2021
a23cd47
Merge branch 'master' into aj-skip-type-params
ascjones Jun 14, 2021
3678114
Merge branch 'master' into aj-skip-type-params
ascjones Jun 14, 2021
4cdf0c5
WIP use bounds attribute for skipping type params
ascjones Jun 14, 2021
99cb9d3
Add required 'static bounds to all type params
ascjones Jun 15, 2021
c39167d
Fmt
ascjones Jun 15, 2021
835efe3
Satisfy clippy
ascjones Jun 15, 2021
75d25ad
Add UI test for skipping bounds
ascjones Jun 15, 2021
3012932
WIP docs
ascjones Jun 15, 2021
20ff837
Revert "Use bounds attribute for skipping type params"
ascjones Jun 15, 2021
7cdd440
WIP dual attribute parsing
ascjones Jun 15, 2021
6d97c49
Use new attribute parsing
ascjones Jun 16, 2021
7cab3e3
Fix up attribute parsing
ascjones Jun 16, 2021
1f84f41
Fmt
ascjones Jun 16, 2021
0716333
Reorder impls
ascjones Jun 16, 2021
a2d239b
Refactor attribute handling
ascjones Jun 16, 2021
ecf21f3
Fmt
ascjones Jun 16, 2021
f876d22
Add docs to attributes
ascjones Jun 16, 2021
b58904f
Add `'static` bounds for all type params, add some ui tests
ascjones Jun 16, 2021
9f64f74
Merge remote-tracking branch 'origin/master' into aj-skip-type-params
ascjones Jun 16, 2021
d61f75d
Check for duplicate attributes
ascjones Jun 16, 2021
a1741d2
Add helpful error message for type params in not in cuatom bounds and…
ascjones Jun 16, 2021
2da8e3f
Improve error message where a type param is missing from the bounds, …
ascjones Jun 16, 2021
2dc9484
Fix test and fmt
ascjones Jun 16, 2021
39e40b1
Refactor and validate missing skip type params
ascjones Jun 17, 2021
44c2af2
Update validation UI test
ascjones Jun 17, 2021
91799c7
Error message formatting
ascjones Jun 17, 2021
e4d570d
Merge remote-tracking branch 'origin/master' into aj-skip-type-params
ascjones Jun 22, 2021
271c2b5
Fix compilation after merge
ascjones Jun 22, 2021
a7e1e6b
Merge remote-tracking branch 'origin/master' into aj-skip-type-params
ascjones Jun 23, 2021
411e50c
Add TypeParameter struct and optional type
ascjones Jun 24, 2021
c5f2bd1
Add ui test for skipping type params
ascjones Jun 25, 2021
d453e69
Add example to named_type_params macro
ascjones Jun 25, 2021
b228318
Type hint for named_type_params with MetaForm
ascjones Jun 25, 2021
f060e59
Add bounds attribute docs
ascjones Jun 25, 2021
7bfa773
Add some docs attribute usage
ascjones Jun 25, 2021
eb41e0b
Fmt
ascjones Jun 25, 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
Add some docs attribute usage
  • Loading branch information
ascjones committed Jun 25, 2021
commit 7bfa773e7b33e8abd7f5a2f8151120b6543ace30
82 changes: 82 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,88 @@
//! the programmer control of the `where` clause on the generated `impl`, to solve this and other
//! issues that can't be foreseen by the auto-generated bounds heuristic.
//!
//! #### `#[scale_info(skip_type_params(..))]`
//!
//! Remove the requirement for the specified type params to implement `TypeInfo`.
//!
//! Consider a simple example of a type parameter which is used for associated types, but the type
//! itself does not carry any type information. Consider this common pattern:
//!
//! ```ignore
//! trait Config {
//! type Balance;
//! }
//!
//! struct Runtime; // doesn't implement `TypeInfo`
//!
//! impl Config for Runtime {
//! type Balance = u64;
//! }
//!
//! #[allow(unused)]
//! #[derive(TypeInfo)]
//! #[scale_info(skip_type_params(T))]
//! struct A<T: Config> {
//! balance: T::Balance,
//! marker: core::marker::PhantomData<T>,
//! }
//!
//! fn assert_type_info<T: scale_info::TypeInfo + 'static>() {}
//!
//! fn main() {
//! // without the `skip_type_params` attribute this will fail.
//! assert_type_info::<A<Runtime>>();
//! }
//! ```
//!
//! By default, the derived `TypeInfo` implementation will add the type of all type parameters to
//! the `TypeParameter` specification e.g.
//!
//! `type_params(vec![TypeParameter::new("T", Some(MetaType::new::<T>()))])`
//!
//! In the example above, this will cause a compiler error because `Runtime` is the concrete tyoe
//! for `T`, which does not satisfy the `TypeInfo` requirement of `MetaType::new::<T>()`.
//!
//! Simply adding a `TypeInfo` derive to `Runtime` is one way of solving this, but that could be
//! misleading (why does it need `TypeInfo` if a value of that type is never encoded?), and can
//! sometimes require adding `TypeInfo` bounds in other impl blocks.
//!
//! The `skip_type_params` attribute solves this, providing an additional "escape hatch" which
//! prevents the given type parameter's type information being required:
//!
//! `type_params(vec![TypeParameter::new("T", None)])`
//!
//! The generated type params do not now require `T` to implement `TypeInfo`, so the auto-generated
//! bound is not added to the generated `TypeInfo` `where` clause.
//!
//! #### Combining `bounds` and `skip_type_params`
//!
//! These two attributes can complement one another, particularly in the case where using `bounds`
//! would still require manually adding a `TypeInfo` bound for the type parameter:
//!
//! ```ignore
//! #[derive(TypeInfo)]
//! #[scale_info(bounds(), skip_type_params(T))]
//! struct A<T> {
//! marker: core::marker::PhantomData<T>,
//! }
//! ```
//!
//! Without `skip_type_params` in the example above, it would require the `TypeInfo` bounds for `T`
//! to be added manually e.g. `#[scale_info(bounds(T: TypeInfo + 'static))]`. Since the intention of
//! the empty bounds is to **remove** all type bounds, then the addition of `skip_type_params`
//! allows this to compile successfully.
//!
//! ##### Precedence
//!
//! When used independently, both attributes modify the `where` clause of the derived `TypeInfo`
//! impl. When used together, the predicates supplied in the `bounds` attribute replaces *all*
//! auto-generated bounds, and `skip_type_params` will have no effect on the resulting `where`
//! clause.
//!
//! **Note:** When using `bounds` without `skip_type_params`, it is therefore required to manually
//! add a `TypeInfo` bound for any non skipped type parameters. The compiler will let you know.
//!
//! # Forms
//!
//! To bridge between compile-time type information and runtime the
Expand Down