mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-18 04:13:00 +00:00
Impl ssz enc/dec for NonZeroUsize
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use super::*;
|
||||
use ethereum_types::{H256, U128, U256};
|
||||
use core::num::NonZeroUsize;
|
||||
|
||||
macro_rules! impl_decodable_for_uint {
|
||||
($type: ident, $bit_size: expr) => {
|
||||
@@ -62,6 +63,28 @@ impl Decode for bool {
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode for NonZeroUsize {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
<usize as Decode>::is_ssz_fixed_len()
|
||||
}
|
||||
|
||||
fn ssz_fixed_len() -> usize {
|
||||
<usize as Decode>::ssz_fixed_len()
|
||||
}
|
||||
|
||||
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
|
||||
let x = usize::from_ssz_bytes(bytes)?;
|
||||
|
||||
if x == 0 {
|
||||
Err(DecodeError::BytesInvalid("NonZeroUsize cannot be zero.".to_string()))
|
||||
} else {
|
||||
// `unwrap` is safe here as `NonZeroUsize::new()` succeeds if `x > 0` and this path
|
||||
// never executes when `x == 0`.
|
||||
Ok(NonZeroUsize::new(x).unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The SSZ union type.
|
||||
impl<T: Decode> Decode for Option<T> {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
|
||||
Reference in New Issue
Block a user