diff --git a/eth2/utils/ssz_types/src/bitfield.rs b/eth2/utils/ssz_types/src/bitfield.rs index 8462b53dfa..9befbb8090 100644 --- a/eth2/utils/ssz_types/src/bitfield.rs +++ b/eth2/utils/ssz_types/src/bitfield.rs @@ -1,4 +1,4 @@ -use crate::FixedSizedError; +use crate::Error; use bit_reverse::LookupReverse; use bit_vec::BitVec as Bitfield; use serde::de::{Deserialize, Deserializer}; @@ -12,10 +12,10 @@ use typenum::Unsigned; /// Provides a common `impl` for structs that wrap a `$name`. macro_rules! common_impl { - ($name: ident, $error: ident) => { + ($name: ident) => { impl $name { /// Create a new BitList list with `initial_len` bits all set to `false`. - pub fn with_capacity(initial_len: usize) -> Result { + pub fn with_capacity(initial_len: usize) -> Result { Self::from_elem(initial_len, false) } @@ -23,7 +23,7 @@ macro_rules! common_impl { /// /// Note: if `initial_len` is not a multiple of 8, the remaining bits will be set to `false` /// regardless of `bit`. - pub fn from_elem(initial_len: usize, bit: bool) -> Result { + pub fn from_elem(initial_len: usize, bit: bool) -> Result { // BitVec can panic if we don't set the len to be a multiple of 8. let full_len = ((initial_len + 7) / 8) * 8; @@ -44,7 +44,7 @@ macro_rules! common_impl { } /// Create a new bitfield using the supplied `bytes` as input - pub fn from_bytes(bytes: &[u8]) -> Result { + pub fn from_bytes(bytes: &[u8]) -> Result { Self::validate_length(bytes.len().saturating_mul(8))?; Ok(Self { @@ -62,17 +62,17 @@ macro_rules! common_impl { /// If the index is in bounds, then result is Ok(value) where value is `true` if the /// bit is 1 and `false` if the bit is 0. If the index is out of bounds, we return an /// error to that extent. - pub fn get(&self, i: usize) -> Result { + pub fn get(&self, i: usize) -> Result { if i < N::to_usize() { match self.bitfield.get(i) { Some(value) => Ok(value), - None => Err($error::OutOfBounds { + None => Err(Error::OutOfBounds { i, len: self.bitfield.len(), }), } } else { - Err($error::InvalidLength { + Err(Error::InvalidLength { i, len: N::to_usize(), }) @@ -83,10 +83,10 @@ macro_rules! common_impl { /// /// If the index is out of bounds, we expand the size of the underlying set to include /// the new index. Returns the previous value if there was one. - pub fn set(&mut self, i: usize, value: bool) -> Result<(), $error> { + pub fn set(&mut self, i: usize, value: bool) -> Result<(), Error> { match self.get(i) { Ok(previous) => Some(previous), - Err($error::OutOfBounds { len, .. }) => { + Err(Error::OutOfBounds { len, .. }) => { let new_len = i - len + 1; self.bitfield.grow(new_len, false); None @@ -266,7 +266,7 @@ pub struct BitVector { _phantom: PhantomData, } -common_impl!(BitVector, FixedSizedError); +common_impl!(BitVector); impl BitVector { /// Create a new bitfield. @@ -278,11 +278,11 @@ impl BitVector { N::to_usize() } - fn validate_length(len: usize) -> Result<(), FixedSizedError> { + fn validate_length(len: usize) -> Result<(), Error> { let fixed_len = N::to_usize(); if len > fixed_len { - Err(FixedSizedError::InvalidLength { + Err(Error::InvalidLength { i: len, len: fixed_len, }) @@ -329,7 +329,7 @@ pub struct BitList { _phantom: PhantomData, } -common_impl!(BitList, FixedSizedError); +common_impl!(BitList); impl BitList { /// Create a new, empty BitList. @@ -340,11 +340,11 @@ impl BitList { } } - fn validate_length(len: usize) -> Result<(), FixedSizedError> { + fn validate_length(len: usize) -> Result<(), Error> { let max_len = Self::max_len(); if len > max_len { - Err(FixedSizedError::InvalidLength { + Err(Error::InvalidLength { i: len, len: max_len, }) diff --git a/eth2/utils/ssz_types/src/fixed_vector.rs b/eth2/utils/ssz_types/src/fixed_vector.rs index c4e101dc90..b5d4227607 100644 --- a/eth2/utils/ssz_types/src/fixed_vector.rs +++ b/eth2/utils/ssz_types/src/fixed_vector.rs @@ -1,4 +1,4 @@ -use crate::FixedSizedError as Error; +use crate::Error; use serde_derive::{Deserialize, Serialize}; use std::marker::PhantomData; use std::ops::{Deref, Index, IndexMut}; diff --git a/eth2/utils/ssz_types/src/lib.rs b/eth2/utils/ssz_types/src/lib.rs index 3bcf8ed1fe..9fba87bc45 100644 --- a/eth2/utils/ssz_types/src/lib.rs +++ b/eth2/utils/ssz_types/src/lib.rs @@ -7,30 +7,9 @@ pub use fixed_vector::FixedVector; pub use typenum; pub use variable_list::VariableList; -/// Returned when a variable-length item encounters an error. +/// Returned when an item encounters an error. #[derive(PartialEq, Debug)] -pub enum VariableSizedError { - /// The operation would cause the maximum length to be exceeded. - ExceedsMaxLength { - len: usize, - max_len: usize, - }, - OutOfBounds { - i: usize, - len: usize, - }, -} - -/// Returned when a fixed-length item encounters an error. -#[derive(PartialEq, Debug)] -pub enum FixedSizedError { - /// The operation would create an item of an invalid size. - InvalidLength { - i: usize, - len: usize, - }, - OutOfBounds { - i: usize, - len: usize, - }, +pub enum Error { + InvalidLength { i: usize, len: usize }, + OutOfBounds { i: usize, len: usize }, } diff --git a/eth2/utils/ssz_types/src/variable_list.rs b/eth2/utils/ssz_types/src/variable_list.rs index 3d0bf31c9b..34450be8a8 100644 --- a/eth2/utils/ssz_types/src/variable_list.rs +++ b/eth2/utils/ssz_types/src/variable_list.rs @@ -1,4 +1,4 @@ -use crate::VariableSizedError as Error; +use crate::Error; use serde_derive::{Deserialize, Serialize}; use std::marker::PhantomData; use std::ops::{Deref, Index, IndexMut}; @@ -61,9 +61,9 @@ impl VariableList { _phantom: PhantomData, }) } else { - Err(Error::ExceedsMaxLength { - len: vec.len(), - max_len: Self::max_len(), + Err(Error::InvalidLength { + i: vec.len(), + len: Self::max_len(), }) } } @@ -90,9 +90,9 @@ impl VariableList { if self.vec.len() < Self::max_len() { Ok(self.vec.push(value)) } else { - Err(Error::ExceedsMaxLength { - len: self.vec.len() + 1, - max_len: Self::max_len(), + Err(Error::InvalidLength { + i: self.vec.len() + 1, + len: Self::max_len(), }) } }