Impl ssz enc/dec for NonZeroUsize
This commit is contained in:
parent
7a2ab2e9aa
commit
45fb11b208
@ -1,5 +1,6 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
use ethereum_types::{H256, U128, U256};
|
use ethereum_types::{H256, U128, U256};
|
||||||
|
use core::num::NonZeroUsize;
|
||||||
|
|
||||||
macro_rules! impl_decodable_for_uint {
|
macro_rules! impl_decodable_for_uint {
|
||||||
($type: ident, $bit_size: expr) => {
|
($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.
|
/// The SSZ union type.
|
||||||
impl<T: Decode> Decode for Option<T> {
|
impl<T: Decode> Decode for Option<T> {
|
||||||
fn is_ssz_fixed_len() -> bool {
|
fn is_ssz_fixed_len() -> bool {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
use core::num::NonZeroUsize;
|
||||||
use ethereum_types::{H256, U128, U256};
|
use ethereum_types::{H256, U128, U256};
|
||||||
|
|
||||||
macro_rules! impl_encodable_for_uint {
|
macro_rules! impl_encodable_for_uint {
|
||||||
@ -80,6 +81,20 @@ impl Encode for bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Encode for NonZeroUsize {
|
||||||
|
fn is_ssz_fixed_len() -> bool {
|
||||||
|
<usize as Encode>::is_ssz_fixed_len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ssz_fixed_len() -> usize {
|
||||||
|
<usize as Encode>::ssz_fixed_len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ssz_append(&self, buf: &mut Vec<u8>) {
|
||||||
|
self.get().ssz_append(buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Encode for H256 {
|
impl Encode for H256 {
|
||||||
fn is_ssz_fixed_len() -> bool {
|
fn is_ssz_fixed_len() -> bool {
|
||||||
true
|
true
|
||||||
|
Loading…
Reference in New Issue
Block a user