Skip to content

Commit

Permalink
[stable2412] Backport #6562 (#6700)
Browse files Browse the repository at this point in the history
Backport #6562 into `stable2412` from re-gius.

See the
[documentation](https://github.com/paritytech/polkadot-sdk/blob/master/docs/BACKPORT.md)
on how to use this bot.

<!--
  # To be used by other automation, do not modify:
  original-pr-number: #${pull_number}
-->

---------

Co-authored-by: Giuseppe Re <[email protected]>
Co-authored-by: GitHub Action <[email protected]>
Co-authored-by: Egor_P <[email protected]>
  • Loading branch information
4 people authored Dec 3, 2024
1 parent dbfe304 commit 2ea45ae
Showing 1 changed file with 14 additions and 2 deletions.
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

0 comments on commit 2ea45ae

Please sign in to comment.