|
20 | 20 | #[cfg(all(not(feature = "std"), feature = "serde"))]
|
21 | 21 | use alloc::format;
|
22 | 22 | use alloc::vec::Vec;
|
| 23 | +use codec::DecodeAll; |
23 | 24 | #[cfg(feature = "serde")]
|
24 | 25 | use serde::{Deserialize, Serialize};
|
25 | 26 |
|
@@ -256,8 +257,7 @@ impl DigestItem {
|
256 | 257 | self.dref().try_as_raw(id)
|
257 | 258 | }
|
258 | 259 |
|
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. |
261 | 261 | pub fn try_to<T: Decode>(&self, id: OpaqueDigestItemId) -> Option<T> {
|
262 | 262 | self.dref().try_to::<T>(id)
|
263 | 263 | }
|
@@ -367,69 +367,48 @@ impl<'a> DigestItemRef<'a> {
|
367 | 367 | /// Try to match this digest item to the given opaque item identifier; if it matches, then
|
368 | 368 | /// try to cast to the given data type; if that works, return it.
|
369 | 369 | 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()) |
371 | 371 | }
|
372 | 372 |
|
373 | 373 | /// Try to match this to a `Self::Seal`, check `id` matches and decode it.
|
374 | 374 | ///
|
375 | 375 | /// Returns `None` if this isn't a seal item, the `id` doesn't match or when the decoding fails.
|
376 | 376 | 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()) |
381 | 380 | }
|
382 | 381 |
|
383 | 382 | /// Try to match this to a `Self::Consensus`, check `id` matches and decode it.
|
384 | 383 | ///
|
385 | 384 | /// Returns `None` if this isn't a consensus item, the `id` doesn't match or
|
386 | 385 | /// when the decoding fails.
|
387 | 386 | 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()) |
392 | 390 | }
|
393 | 391 |
|
394 | 392 | /// Try to match this to a `Self::PreRuntime`, check `id` matches and decode it.
|
395 | 393 | ///
|
396 | 394 | /// Returns `None` if this isn't a pre-runtime item, the `id` doesn't match or
|
397 | 395 | /// when the decoding fails.
|
398 | 396 | 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()) |
403 | 400 | }
|
404 | 401 | }
|
405 | 402 |
|
406 | 403 | impl<'a> Encode for DigestItemRef<'a> {
|
407 | 404 | fn encode(&self) -> Vec<u8> {
|
408 |
| - let mut v = Vec::new(); |
409 |
| - |
410 | 405 | 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(), |
430 | 411 | }
|
431 |
| - |
432 |
| - v |
433 | 412 | }
|
434 | 413 | }
|
435 | 414 |
|
|
0 commit comments