Skip to content
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

[stable2412] Backport #6562 #6700

Open
wants to merge 7 commits into
base: stable2412
Choose a base branch
from
Open
Changes from all commits
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
16 changes: 14 additions & 2 deletions substrate/primitives/runtime/src/type_with_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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>);

Expand All @@ -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)
Expand Down
Loading