Skip to content

Commit 7133c28

Browse files
bkchractions-user
authored andcommitted
sp-runtime: Be a little bit more functional :D (#6526)
Co-authored-by: GitHub Action <[email protected]> (cherry picked from commit b71bd53)
1 parent dba2dd5 commit 7133c28

File tree

2 files changed

+25
-38
lines changed

2 files changed

+25
-38
lines changed

prdoc/pr_6526.prdoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
title: 'sp-runtime: Be a little bit more functional :D'
2+
doc:
3+
- audience: Runtime Dev
4+
description:
5+
Some internal refactorings in the `Digest` code.
6+
crates:
7+
- name: sp-runtime
8+
bump: patch

substrate/primitives/runtime/src/generic/digest.rs

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#[cfg(all(not(feature = "std"), feature = "serde"))]
2121
use alloc::format;
2222
use alloc::vec::Vec;
23+
use codec::DecodeAll;
2324
#[cfg(feature = "serde")]
2425
use serde::{Deserialize, Serialize};
2526

@@ -256,8 +257,7 @@ impl DigestItem {
256257
self.dref().try_as_raw(id)
257258
}
258259

259-
/// Returns the data contained in the item if `Some` if this entry has the id given, decoded
260-
/// to the type provided `T`.
260+
/// Returns the data decoded as `T`, if the `id` is matching.
261261
pub fn try_to<T: Decode>(&self, id: OpaqueDigestItemId) -> Option<T> {
262262
self.dref().try_to::<T>(id)
263263
}
@@ -367,69 +367,48 @@ impl<'a> DigestItemRef<'a> {
367367
/// Try to match this digest item to the given opaque item identifier; if it matches, then
368368
/// try to cast to the given data type; if that works, return it.
369369
pub fn try_to<T: Decode>(&self, id: OpaqueDigestItemId) -> Option<T> {
370-
self.try_as_raw(id).and_then(|mut x| Decode::decode(&mut x).ok())
370+
self.try_as_raw(id).and_then(|mut x| DecodeAll::decode_all(&mut x).ok())
371371
}
372372

373373
/// Try to match this to a `Self::Seal`, check `id` matches and decode it.
374374
///
375375
/// Returns `None` if this isn't a seal item, the `id` doesn't match or when the decoding fails.
376376
pub fn seal_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
377-
match self {
378-
Self::Seal(v, s) if *v == id => Decode::decode(&mut &s[..]).ok(),
379-
_ => None,
380-
}
377+
self.as_seal()
378+
.filter(|s| s.0 == *id)
379+
.and_then(|mut d| DecodeAll::decode_all(&mut d.1).ok())
381380
}
382381

383382
/// Try to match this to a `Self::Consensus`, check `id` matches and decode it.
384383
///
385384
/// Returns `None` if this isn't a consensus item, the `id` doesn't match or
386385
/// when the decoding fails.
387386
pub fn consensus_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
388-
match self {
389-
Self::Consensus(v, s) if *v == id => Decode::decode(&mut &s[..]).ok(),
390-
_ => None,
391-
}
387+
self.as_consensus()
388+
.filter(|s| s.0 == *id)
389+
.and_then(|mut d| DecodeAll::decode_all(&mut d.1).ok())
392390
}
393391

394392
/// Try to match this to a `Self::PreRuntime`, check `id` matches and decode it.
395393
///
396394
/// Returns `None` if this isn't a pre-runtime item, the `id` doesn't match or
397395
/// when the decoding fails.
398396
pub fn pre_runtime_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
399-
match self {
400-
Self::PreRuntime(v, s) if *v == id => Decode::decode(&mut &s[..]).ok(),
401-
_ => None,
402-
}
397+
self.as_pre_runtime()
398+
.filter(|s| s.0 == *id)
399+
.and_then(|mut d| DecodeAll::decode_all(&mut d.1).ok())
403400
}
404401
}
405402

406403
impl<'a> Encode for DigestItemRef<'a> {
407404
fn encode(&self) -> Vec<u8> {
408-
let mut v = Vec::new();
409-
410405
match *self {
411-
Self::Consensus(val, data) => {
412-
DigestItemType::Consensus.encode_to(&mut v);
413-
(val, data).encode_to(&mut v);
414-
},
415-
Self::Seal(val, sig) => {
416-
DigestItemType::Seal.encode_to(&mut v);
417-
(val, sig).encode_to(&mut v);
418-
},
419-
Self::PreRuntime(val, data) => {
420-
DigestItemType::PreRuntime.encode_to(&mut v);
421-
(val, data).encode_to(&mut v);
422-
},
423-
Self::Other(val) => {
424-
DigestItemType::Other.encode_to(&mut v);
425-
val.encode_to(&mut v);
426-
},
427-
Self::RuntimeEnvironmentUpdated => {
428-
DigestItemType::RuntimeEnvironmentUpdated.encode_to(&mut v);
429-
},
406+
Self::Consensus(val, data) => (DigestItemType::Consensus, val, data).encode(),
407+
Self::Seal(val, sig) => (DigestItemType::Seal, val, sig).encode(),
408+
Self::PreRuntime(val, data) => (DigestItemType::PreRuntime, val, data).encode(),
409+
Self::Other(val) => (DigestItemType::Other, val).encode(),
410+
Self::RuntimeEnvironmentUpdated => DigestItemType::RuntimeEnvironmentUpdated.encode(),
430411
}
431-
432-
v
433412
}
434413
}
435414

0 commit comments

Comments
 (0)