Skip to content

Commit d825a64

Browse files
committed
Add runtime_types
1 parent 2d4eb64 commit d825a64

File tree

6 files changed

+501
-5
lines changed

6 files changed

+501
-5
lines changed

.github/workflows/test-suite.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
- name: Get latest version of stable Rust
3131
run: rustup update stable
3232
- name: Run tests
33-
run: cargo test --release
33+
run: cargo test --release --all-features
3434
coverage:
3535
name: cargo-tarpaulin
3636
runs-on: ubuntu-latest

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,19 @@ smallvec = "1.8.0"
2121
arbitrary = { version = "1.0", features = ["derive"], optional = true }
2222
itertools = "0.14.0"
2323

24+
# Optional dependencies
25+
educe = { version = "0.6", optional = true }
26+
# Use crates.io version once published.
27+
context_deserialize = { git = "https://github.com/sigp/lighthouse", tag = "v8.0.0-rc.0", optional = true }
28+
2429
[dev-dependencies]
2530
criterion = "0.7.0"
2631
serde_json = "1.0.0"
2732
tree_hash_derive = "0.10.0"
2833

34+
[features]
35+
runtime-types = ["dep:context_deserialize", "dep:educe"]
36+
2937
[[bench]]
3038
harness = false
3139
name = "encode_decode"

src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@
3737
//!
3838
//! ```
3939
40+
pub mod serde_utils;
41+
pub mod length {
42+
pub use ssz::{Fixed, Variable};
43+
}
44+
4045
#[macro_use]
4146
mod fixed_vector;
42-
pub mod serde_utils;
4347
mod tree_hash;
4448
mod variable_list;
4549

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

51-
pub mod length {
52-
pub use ssz::{Fixed, Variable};
53-
}
55+
#[cfg(feature = "runtime-types")]
56+
mod runtime_types;
57+
58+
#[cfg(feature = "runtime-types")]
59+
pub use runtime_types::{RuntimeFixedVector, RuntimeVariableList};
5460

5561
/// Returned when an item encounters an error.
5662
#[derive(PartialEq, Debug, Clone)]

src/runtime_types/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod runtime_fixed_vector;
2+
mod runtime_variable_list;
3+
4+
pub use runtime_fixed_vector::RuntimeFixedVector;
5+
pub use runtime_variable_list::RuntimeVariableList;
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//! Emulates a fixed size array but with the length set at runtime.
2+
//!
3+
//! The length of the list cannot be changed once it is set.
4+
5+
use std::fmt;
6+
use std::fmt::Debug;
7+
8+
#[derive(Clone)]
9+
pub struct RuntimeFixedVector<T> {
10+
vec: Vec<T>,
11+
len: usize,
12+
}
13+
14+
impl<T: Debug> Debug for RuntimeFixedVector<T> {
15+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16+
write!(f, "{:?} (len={})", self.vec, self.len)
17+
}
18+
}
19+
20+
impl<T: Clone + Default> RuntimeFixedVector<T> {
21+
pub fn new(vec: Vec<T>) -> Self {
22+
let len = vec.len();
23+
Self { vec, len }
24+
}
25+
26+
pub fn to_vec(&self) -> Vec<T> {
27+
self.vec.clone()
28+
}
29+
30+
pub fn as_slice(&self) -> &[T] {
31+
self.vec.as_slice()
32+
}
33+
34+
#[allow(clippy::len_without_is_empty)]
35+
pub fn len(&self) -> usize {
36+
self.len
37+
}
38+
39+
pub fn into_vec(self) -> Vec<T> {
40+
self.vec
41+
}
42+
43+
pub fn default(max_len: usize) -> Self {
44+
Self {
45+
vec: vec![T::default(); max_len],
46+
len: max_len,
47+
}
48+
}
49+
50+
pub fn take(&mut self) -> Self {
51+
let new = std::mem::take(&mut self.vec);
52+
*self = Self::new(vec![T::default(); self.len]);
53+
Self {
54+
vec: new,
55+
len: self.len,
56+
}
57+
}
58+
}
59+
60+
impl<T> std::ops::Deref for RuntimeFixedVector<T> {
61+
type Target = [T];
62+
63+
fn deref(&self) -> &[T] {
64+
&self.vec[..]
65+
}
66+
}
67+
68+
impl<T> std::ops::DerefMut for RuntimeFixedVector<T> {
69+
fn deref_mut(&mut self) -> &mut [T] {
70+
&mut self.vec[..]
71+
}
72+
}
73+
74+
impl<T> IntoIterator for RuntimeFixedVector<T> {
75+
type Item = T;
76+
type IntoIter = std::vec::IntoIter<T>;
77+
78+
fn into_iter(self) -> Self::IntoIter {
79+
self.vec.into_iter()
80+
}
81+
}
82+
83+
impl<'a, T> IntoIterator for &'a RuntimeFixedVector<T> {
84+
type Item = &'a T;
85+
type IntoIter = std::slice::Iter<'a, T>;
86+
87+
fn into_iter(self) -> Self::IntoIter {
88+
self.vec.iter()
89+
}
90+
}

0 commit comments

Comments
 (0)