refactor: define trait HashOutput for some Hash associate type (#14220)

* define trait `HashOutput`

* improve

* improve

* Update primitives/runtime/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* remove `Block::Hash: Ord`

* fmt

* add `MaybeFromStr`

* cleanup

* fix

* remove useless `HashOutput`

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
yjh
2023-06-08 10:02:13 +08:00
committed by GitHub
parent c31c6a1a73
commit ddb46a8aa0
19 changed files with 85 additions and 146 deletions
+54 -49
View File
@@ -932,19 +932,7 @@ pub trait Hash:
+ Hasher<Out = <Self as Hash>::Output>
{
/// The hash type produced.
type Output: Member
+ MaybeSerializeDeserialize
+ Debug
+ sp_std::hash::Hash
+ AsRef<[u8]>
+ AsMut<[u8]>
+ Copy
+ Default
+ Encode
+ Decode
+ EncodeLike
+ MaxEncodedLen
+ TypeInfo;
type Output: HashOutput;
/// Produce the hash of some byte-slice.
fn hash(s: &[u8]) -> Self::Output {
@@ -963,6 +951,47 @@ pub trait Hash:
fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>, state_version: StateVersion) -> Self::Output;
}
/// Super trait with all the attributes for a hashing output.
pub trait HashOutput:
Member
+ MaybeSerializeDeserialize
+ MaybeDisplay
+ MaybeFromStr
+ Debug
+ sp_std::hash::Hash
+ AsRef<[u8]>
+ AsMut<[u8]>
+ Copy
+ Ord
+ Default
+ Encode
+ Decode
+ EncodeLike
+ MaxEncodedLen
+ TypeInfo
{
}
impl<T> HashOutput for T where
T: Member
+ MaybeSerializeDeserialize
+ MaybeDisplay
+ MaybeFromStr
+ Debug
+ sp_std::hash::Hash
+ AsRef<[u8]>
+ AsMut<[u8]>
+ Copy
+ Ord
+ Default
+ Encode
+ Decode
+ EncodeLike
+ MaxEncodedLen
+ TypeInfo
{
}
/// Blake2-256 Hash implementation.
#[derive(PartialEq, Eq, Clone, RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@@ -981,13 +1010,13 @@ impl Hasher for BlakeTwo256 {
impl Hash for BlakeTwo256 {
type Output = sp_core::H256;
fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>, version: StateVersion) -> Self::Output {
sp_io::trie::blake2_256_root(input, version)
}
fn ordered_trie_root(input: Vec<Vec<u8>>, version: StateVersion) -> Self::Output {
sp_io::trie::blake2_256_ordered_root(input, version)
}
fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>, version: StateVersion) -> Self::Output {
sp_io::trie::blake2_256_root(input, version)
}
}
/// Keccak-256 Hash implementation.
@@ -1008,13 +1037,13 @@ impl Hasher for Keccak256 {
impl Hash for Keccak256 {
type Output = sp_core::H256;
fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>, version: StateVersion) -> Self::Output {
sp_io::trie::keccak_256_root(input, version)
}
fn ordered_trie_root(input: Vec<Vec<u8>>, version: StateVersion) -> Self::Output {
sp_io::trie::keccak_256_ordered_root(input, version)
}
fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>, version: StateVersion) -> Self::Output {
sp_io::trie::keccak_256_root(input, version)
}
}
/// Something that can be checked for equality and printed out to a debug channel if bad.
@@ -1102,27 +1131,15 @@ pub trait Header: Clone + Send + Sync + Codec + Eq + MaybeSerialize + Debug + 's
/// Header number.
type Number: Member
+ MaybeSerializeDeserialize
+ MaybeFromStr
+ Debug
+ sp_std::hash::Hash
+ Copy
+ MaybeDisplay
+ AtLeast32BitUnsigned
+ Codec
+ sp_std::str::FromStr;
+ Codec;
/// Header hash type
type Hash: Member
+ MaybeSerializeDeserialize
+ Debug
+ sp_std::hash::Hash
+ Ord
+ Copy
+ MaybeDisplay
+ Default
+ SimpleBitOps
+ Codec
+ AsRef<[u8]>
+ AsMut<[u8]>
+ TypeInfo;
type Hash: HashOutput;
/// Hashing algorithm
type Hashing: Hash<Output = Self::Hash>;
@@ -1176,19 +1193,7 @@ pub trait Block: Clone + Send + Sync + Codec + Eq + MaybeSerialize + Debug + 'st
/// Header type.
type Header: Header<Hash = Self::Hash>;
/// Block hash type.
type Hash: Member
+ MaybeSerializeDeserialize
+ Debug
+ sp_std::hash::Hash
+ Ord
+ Copy
+ MaybeDisplay
+ Default
+ SimpleBitOps
+ Codec
+ AsRef<[u8]>
+ AsMut<[u8]>
+ TypeInfo;
type Hash: HashOutput;
/// Returns a reference to the header.
fn header(&self) -> &Self::Header;