Skip to content

Commit

Permalink
Begin stable container impl
Browse files Browse the repository at this point in the history
  • Loading branch information
macladson committed May 17, 2024
1 parent 1b151e5 commit 4f0b708
Show file tree
Hide file tree
Showing 6 changed files with 501 additions and 27 deletions.
2 changes: 1 addition & 1 deletion ssz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ categories = ["cryptography::cryptocurrencies"]
name = "ssz"

[dev-dependencies]
ethereum_ssz_derive = { version = "0.5.3", path = "../ssz_derive" }
ethereum_ssz_derive = { git = "https://github.com/macladson/ssz_derive", branch = "stable-container" }

[dependencies]
ethereum-types = "0.14.1"
Expand Down
16 changes: 10 additions & 6 deletions ssz/src/decode/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,18 @@ impl Decode for NonZeroUsize {

impl<T: Decode> Decode for Option<T> {
fn is_ssz_fixed_len() -> bool {
false
T::is_ssz_fixed_len()
}

fn ssz_fixed_len() -> usize {
T::ssz_fixed_len()
}

fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
let (selector, body) = split_union_bytes(bytes)?;
match selector.into() {
0u8 => Ok(None),
1u8 => <T as Decode>::from_ssz_bytes(body).map(Option::Some),
other => Err(DecodeError::UnionSelectorInvalid(other)),
if bytes.is_empty() {
Ok(None)
} else {
T::from_ssz_bytes(bytes).map(Some)
}
}
}
Expand Down
30 changes: 13 additions & 17 deletions ssz/src/encode/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,28 +205,24 @@ impl_encode_for_tuples! {

impl<T: Encode> Encode for Option<T> {
fn is_ssz_fixed_len() -> bool {
false
T::is_ssz_fixed_len()
}

fn ssz_fixed_len() -> usize {
T::ssz_fixed_len()
}

fn ssz_append(&self, buf: &mut Vec<u8>) {
match self {
Option::None => {
let union_selector: u8 = 0u8;
buf.push(union_selector);
}
Option::Some(ref inner) => {
let union_selector: u8 = 1u8;
buf.push(union_selector);
inner.ssz_append(buf);
}
match &self {
None => {},
Some(inner) => inner.ssz_append(buf),
}
}

fn ssz_bytes_len(&self) -> usize {
match self {
Option::None => 1usize,
Option::Some(ref inner) => inner
.ssz_bytes_len()
.checked_add(1)
.expect("encoded length must be less than usize::max_value"),
match &self {
None => 0,
Some(inner) => inner.ssz_bytes_len(),
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion ssz_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ syn = "1.0.42"
proc-macro2 = "1.0.23"
quote = "1.0.7"
darling = "0.13.0"
ssz_types = { git = "https://github.com/macladson/ssz_types", branch = "stable-container" }

[dev-dependencies]
ethereum_ssz = { path = "../ssz" }
ethereum_ssz = { git = "https://github.com/macladson/ethereum_ssz", branch = "stable-container" }
Loading

0 comments on commit 4f0b708

Please sign in to comment.