Skip to content

Commit

Permalink
Merge pull request #96 from ralexstokes/drop-default
Browse files Browse the repository at this point in the history
drop some unnecessary type constraints
  • Loading branch information
ralexstokes authored Jul 28, 2023
2 parents 6dc5133 + 0af7a6e commit e498c7a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ssz-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub trait Sized {

/// `SimpleSerialize` is a trait for types
/// conforming to the SSZ spec.
pub trait SimpleSerialize: Serialize + Deserialize + Sized + Merkleized + Default {}
pub trait SimpleSerialize: Serialize + Deserialize + Sized + Merkleized {}

mod exports {
pub use crate::{
Expand Down
2 changes: 1 addition & 1 deletion ssz-rs/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ where

impl<T, const N: usize> Default for List<T, N>
where
T: SimpleSerialize + Default,
T: SimpleSerialize,
{
fn default() -> Self {
let data = vec![];
Expand Down
9 changes: 6 additions & 3 deletions ssz-rs/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ where

impl<T, const N: usize> Default for Vector<T, N>
where
T: SimpleSerialize + Default + Clone,
T: SimpleSerialize + Default,
{
fn default() -> Self {
// SAFETY: there is currently no way to enforce statically
// that `N` is non-zero with const generics so panics are possible.
assert!(N > 0);

let data = vec![T::default(); N];
let mut data = Vec::with_capacity(N);
for _ in 0..N {
data.push(T::default());
}

// SAFETY: panic can't happen because data.len() == N != 0; qed
data.try_into()
Expand Down Expand Up @@ -216,7 +219,7 @@ where
}
}

impl<T, const N: usize> SimpleSerialize for Vector<T, N> where T: SimpleSerialize + Clone {}
impl<T, const N: usize> SimpleSerialize for Vector<T, N> where T: SimpleSerialize {}

#[cfg(feature = "serde")]
impl<T: SimpleSerialize + serde::Serialize, const N: usize> serde::Serialize for Vector<T, N> {
Expand Down

0 comments on commit e498c7a

Please sign in to comment.