Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Get latest version of stable Rust
run: rustup update stable
- name: Run tests
run: cargo test --release
run: cargo test --release --all-features
coverage:
name: cargo-tarpaulin
runs-on: ubuntu-latest
Expand Down
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@ smallvec = "1.8.0"
arbitrary = { version = "1.0", features = ["derive"], optional = true }
itertools = "0.14.0"

# Optional dependencies
educe = { version = "0.6", optional = true }
# Use crates.io version once published.
context_deserialize = { git = "https://github.com/sigp/lighthouse", tag = "v8.0.0-rc.0", optional = true }

[dev-dependencies]
criterion = "0.7.0"
serde_json = "1.0.0"
tree_hash_derive = "0.10.0"

[features]
runtime-types = ["dep:context_deserialize", "dep:educe"]

[[bench]]
harness = false
name = "encode_decode"
14 changes: 10 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@
//!
//! ```

pub mod serde_utils;
pub mod length {
pub use ssz::{Fixed, Variable};
}

#[macro_use]
mod fixed_vector;
pub mod serde_utils;
mod tree_hash;
mod variable_list;

Expand All @@ -48,9 +52,11 @@ pub use ssz::{BitList, BitVector, Bitfield};
pub use typenum;
pub use variable_list::VariableList;

pub mod length {
pub use ssz::{Fixed, Variable};
}
#[cfg(feature = "runtime-types")]
mod runtime_types;

#[cfg(feature = "runtime-types")]
pub use runtime_types::{RuntimeFixedVector, RuntimeVariableList};

/// Returned when an item encounters an error.
#[derive(PartialEq, Debug, Clone)]
Expand Down
5 changes: 5 additions & 0 deletions src/runtime_types/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod runtime_fixed_vector;
mod runtime_variable_list;

pub use runtime_fixed_vector::RuntimeFixedVector;
pub use runtime_variable_list::RuntimeVariableList;
90 changes: 90 additions & 0 deletions src/runtime_types/runtime_fixed_vector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//! Emulates a fixed size array but with the length set at runtime.
//!
//! The length of the list cannot be changed once it is set.

use std::fmt;
use std::fmt::Debug;

#[derive(Clone)]
pub struct RuntimeFixedVector<T> {
vec: Vec<T>,
len: usize,
}

impl<T: Debug> Debug for RuntimeFixedVector<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?} (len={})", self.vec, self.len)
}
}

impl<T: Clone + Default> RuntimeFixedVector<T> {
pub fn new(vec: Vec<T>) -> Self {
let len = vec.len();
Self { vec, len }
}

pub fn to_vec(&self) -> Vec<T> {
self.vec.clone()
}

pub fn as_slice(&self) -> &[T] {
self.vec.as_slice()
}

#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
self.len
}

pub fn into_vec(self) -> Vec<T> {
self.vec
}

pub fn default(max_len: usize) -> Self {
Self {
vec: vec![T::default(); max_len],
len: max_len,
}
}

pub fn take(&mut self) -> Self {
let new = std::mem::take(&mut self.vec);
*self = Self::new(vec![T::default(); self.len]);
Self {
vec: new,
len: self.len,
}
}
}

impl<T> std::ops::Deref for RuntimeFixedVector<T> {
type Target = [T];

fn deref(&self) -> &[T] {
&self.vec[..]
}
}

impl<T> std::ops::DerefMut for RuntimeFixedVector<T> {
fn deref_mut(&mut self) -> &mut [T] {
&mut self.vec[..]
}
}

impl<T> IntoIterator for RuntimeFixedVector<T> {
type Item = T;
type IntoIter = std::vec::IntoIter<T>;

fn into_iter(self) -> Self::IntoIter {
self.vec.into_iter()
}
}

impl<'a, T> IntoIterator for &'a RuntimeFixedVector<T> {
type Item = &'a T;
type IntoIter = std::slice::Iter<'a, T>;

fn into_iter(self) -> Self::IntoIter {
self.vec.iter()
}
}
Loading
Loading