Skip to content

Commit

Permalink
feat: impl Encode Decode alloy_primitves::Bloom
Browse files Browse the repository at this point in the history
  • Loading branch information
yash-atreya committed Aug 22, 2024
1 parent c0476cb commit 37a57a8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
23 changes: 22 additions & 1 deletion ssz/src/decode/impls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use crate::decode::try_from_iter::{TryCollect, TryFromIter};
use alloy_primitives::{Address, Bytes, FixedBytes, U128, U256};
use alloy_primitives::{Address, Bloom, Bytes, FixedBytes, U128, U256};
use core::num::NonZeroUsize;
use itertools::process_results;
use smallvec::SmallVec;
Expand Down Expand Up @@ -320,6 +320,27 @@ impl<const N: usize> Decode for FixedBytes<N> {
}
}

impl Decode for Bloom {
fn is_ssz_fixed_len() -> bool {
true
}

fn ssz_fixed_len() -> usize {
256
}

fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
let len = bytes.len();
let expected = <Self as Decode>::ssz_fixed_len();

Check warning on line 334 in ssz/src/decode/impls.rs

View check run for this annotation

Codecov / codecov/patch

ssz/src/decode/impls.rs#L332-L334

Added lines #L332 - L334 were not covered by tests

if len != expected {
Err(DecodeError::InvalidByteLength { len, expected })

Check warning on line 337 in ssz/src/decode/impls.rs

View check run for this annotation

Codecov / codecov/patch

ssz/src/decode/impls.rs#L336-L337

Added lines #L336 - L337 were not covered by tests
} else {
Ok(Self::from_slice(bytes))

Check warning on line 339 in ssz/src/decode/impls.rs

View check run for this annotation

Codecov / codecov/patch

ssz/src/decode/impls.rs#L339

Added line #L339 was not covered by tests
}
}
}

impl Decode for U256 {
fn is_ssz_fixed_len() -> bool {
true
Expand Down
29 changes: 28 additions & 1 deletion ssz/src/encode/impls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use alloy_primitives::{Address, Bytes, FixedBytes, U128, U256};
use alloy_primitives::{Address, Bloom, Bytes, FixedBytes, U128, U256};
use core::num::NonZeroUsize;
use smallvec::SmallVec;
use std::collections::{BTreeMap, BTreeSet};
Expand Down Expand Up @@ -454,6 +454,33 @@ impl<const N: usize> Encode for FixedBytes<N> {
}
}

impl Encode for Bloom {
#[inline]
fn is_ssz_fixed_len() -> bool {
true

Check warning on line 460 in ssz/src/encode/impls.rs

View check run for this annotation

Codecov / codecov/patch

ssz/src/encode/impls.rs#L459-L460

Added lines #L459 - L460 were not covered by tests
}

#[inline]
fn ssz_bytes_len(&self) -> usize {
256

Check warning on line 465 in ssz/src/encode/impls.rs

View check run for this annotation

Codecov / codecov/patch

ssz/src/encode/impls.rs#L464-L465

Added lines #L464 - L465 were not covered by tests
}

#[inline]
fn ssz_fixed_len() -> usize {
256

Check warning on line 470 in ssz/src/encode/impls.rs

View check run for this annotation

Codecov / codecov/patch

ssz/src/encode/impls.rs#L469-L470

Added lines #L469 - L470 were not covered by tests
}

#[inline]
fn ssz_append(&self, buf: &mut Vec<u8>) {
buf.extend_from_slice(&self.0 .0);

Check warning on line 475 in ssz/src/encode/impls.rs

View check run for this annotation

Codecov / codecov/patch

ssz/src/encode/impls.rs#L474-L475

Added lines #L474 - L475 were not covered by tests
}

#[inline]
fn as_ssz_bytes(&self) -> Vec<u8> {
self.0.to_vec()

Check warning on line 480 in ssz/src/encode/impls.rs

View check run for this annotation

Codecov / codecov/patch

ssz/src/encode/impls.rs#L479-L480

Added lines #L479 - L480 were not covered by tests
}
}

impl Encode for Bytes {
#[inline]
fn is_ssz_fixed_len() -> bool {
Expand Down

0 comments on commit 37a57a8

Please sign in to comment.