-
Notifications
You must be signed in to change notification settings - Fork 710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hide nonce implementation details in metadata #6562
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1767a82
Add custom `TypeInfo` implementation for `TypeWithDefault`
re-gius ba4c7fc
Merge branch 'master' into re-gius/hide-type-info-for-nonce
re-gius 46bbfbe
fix import
re-gius 62a70fd
Merge branch 'master' into re-gius/hide-type-info-for-nonce
re-gius a868839
add `prdoc`
re-gius 7f0d0fe
add more tests
re-gius 2ca0651
Merge branch 'master' into re-gius/hide-type-info-for-nonce
re-gius 37efdff
Merge branch 'master' into re-gius/hide-type-info-for-nonce
re-gius 1e84e01
Merge branch 'master' into re-gius/hide-type-info-for-nonce
re-gius d328a23
Update substrate/primitives/runtime/src/type_with_default.rs
re-gius b4c048c
Merge branch 'master' into re-gius/hide-type-info-for-nonce
re-gius 06212c9
Add type info change in docs
re-gius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 | ||
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json | ||
|
||
title: Hide nonce implementation details in metadata | ||
|
||
doc: | ||
- audience: Runtime Dev | ||
description: | | ||
Use custom implementation of TypeInfo for TypeWithDefault to show inner value's type info. | ||
This should bring back nonce to u64 in metadata. | ||
|
||
crates: | ||
- name: sp-runtime | ||
bump: minor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ use num_traits::{ | |
CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl, CheckedShr, CheckedSub, | ||
Num, NumCast, PrimInt, Saturating, ToPrimitive, | ||
}; | ||
use scale_info::TypeInfo; | ||
use scale_info::{StaticTypeInfo, TypeInfo}; | ||
use sp_core::Get; | ||
|
||
#[cfg(feature = "serde")] | ||
|
@@ -40,7 +40,8 @@ use serde::{Deserialize, Serialize}; | |
/// A type that wraps another type and provides a default value. | ||
/// | ||
/// Passes through arithmetical and many other operations to the inner value. | ||
#[derive(Encode, Decode, TypeInfo, Debug, MaxEncodedLen)] | ||
/// Type information for metadata is the same as the inner value's type. | ||
#[derive(Encode, Decode, Debug, MaxEncodedLen)] | ||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
pub struct TypeWithDefault<T, D: Get<T>>(T, PhantomData<D>); | ||
|
||
|
@@ -50,6 +51,17 @@ impl<T, D: Get<T>> TypeWithDefault<T, D> { | |
} | ||
} | ||
|
||
// Hides implementation details from the outside (for metadata type information). | ||
// | ||
// The type info showed in metadata is the one of the inner value's type. | ||
impl<T: StaticTypeInfo, D: Get<T> + 'static> TypeInfo for TypeWithDefault<T, D> { | ||
type Identity = Self; | ||
|
||
fn type_info() -> scale_info::Type { | ||
T::type_info() | ||
} | ||
} | ||
|
||
impl<T: Clone, D: Get<T>> Clone for TypeWithDefault<T, D> { | ||
fn clone(&self) -> Self { | ||
Self(self.0.clone(), PhantomData) | ||
|
@@ -511,6 +523,7 @@ impl<T: HasCompact, D: Get<T>> CompactAs for TypeWithDefault<T, D> { | |
#[cfg(test)] | ||
mod tests { | ||
use super::TypeWithDefault; | ||
use scale_info::TypeInfo; | ||
use sp_arithmetic::traits::{AtLeast16Bit, AtLeast32Bit, AtLeast8Bit}; | ||
use sp_core::Get; | ||
|
||
|
@@ -565,5 +578,11 @@ mod tests { | |
} | ||
type U128WithDefault = TypeWithDefault<u128, Getu128>; | ||
impl WrapAtLeast32Bit for U128WithDefault {} | ||
|
||
assert_eq!(U8WithDefault::type_info(), <u8 as TypeInfo>::type_info()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be clear, this was not passing before this PR, right There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly, I just double-checked: it does not pass with the derived |
||
assert_eq!(U16WithDefault::type_info(), <u16 as TypeInfo>::type_info()); | ||
assert_eq!(U32WithDefault::type_info(), <u32 as TypeInfo>::type_info()); | ||
assert_eq!(U64WithDefault::type_info(), <u64 as TypeInfo>::type_info()); | ||
assert_eq!(U128WithDefault::type_info(), <u128 as TypeInfo>::type_info()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should update the doc of the type to specify this nuance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in 06212c9