Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ tree_hash_derive = "0.12.0"
[[bench]]
harness = false
name = "encode_decode"

[patch.crates-io]
tree_hash = { git = "https://github.com/sigp/tree_hash", branch = "progressive" }
tree_hash_derive = { git = "https://github.com/sigp/tree_hash", branch = "progressive" }
ethereum_ssz = { git = "https://github.com/sigp/ethereum_ssz", branch = "progressive" }
16 changes: 15 additions & 1 deletion src/context_deserialize.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{typenum::Unsigned, FixedVector};
use crate::{typenum::Unsigned, FixedVector, ProgressiveVariableList};
use context_deserialize::ContextDeserialize;
use serde::de::{Deserializer, Error};

Expand All @@ -16,3 +16,17 @@ where
FixedVector::new(vec).map_err(|e| D::Error::custom(format!("{:?}", e)))
}
}

impl<'de, C, T> ContextDeserialize<'de, C> for ProgressiveVariableList<T>
where
T: ContextDeserialize<'de, C>,
C: Clone,
{
fn context_deserialize<D>(deserializer: D, context: C) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let vec = Vec::<T>::context_deserialize(deserializer, context)?;
Ok(ProgressiveVariableList::new(vec))
}
}
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ pub mod length {

#[macro_use]
mod fixed_vector;
mod progressive_variable_list;
mod tree_hash;
mod variable_list;

#[cfg(feature = "context_deserialize")]
mod context_deserialize;

pub use fixed_vector::FixedVector;
pub use progressive_variable_list::ProgressiveVariableList;
pub use ssz::{BitList, BitVector, Bitfield};
pub use typenum;
pub use variable_list::VariableList;
Expand All @@ -65,6 +67,20 @@ mod list_encoded_option;

pub use list_encoded_option::ListEncodedOption;

/// Reinterpret `bytes` as a `Vec<T>`, for the common `T == u8` SSZ fast-path.
///
/// Safety: the caller must have verified `TypeId::of::<T>() == TypeId::of::<u8>()`, so that
/// `Vec<u8>` and `Vec<T>` have identical layout.
pub(crate) fn u8_bytes_to_vec<T: 'static>(bytes: &[u8]) -> Vec<T> {
debug_assert_eq!(
core::any::TypeId::of::<T>(),
core::any::TypeId::of::<u8>(),
"u8_bytes_to_vec called with T != u8",
);
// Safety: caller verified `T == u8`, so the transmute is a no-op reinterpretation.
unsafe { core::mem::transmute::<Vec<u8>, Vec<T>>(bytes.to_vec()) }
}

/// Returned when an item encounters an error.
#[derive(PartialEq, Debug, Clone)]
pub enum Error {
Expand Down
Loading
Loading