Move RuntimeFixedVector into module and rename

This commit is contained in:
Michael Sproul
2025-01-06 17:24:50 +11:00
parent f66e179a40
commit 440e854199
5 changed files with 102 additions and 106 deletions

View File

@@ -1,11 +1,8 @@
use crate::test_utils::TestRandom;
use crate::{
beacon_block_body::BLOB_KZG_COMMITMENTS_INDEX, BeaconBlockHeader, BeaconStateError, Blob,
Epoch, EthSpec, FixedVector, Hash256, SignedBeaconBlockHeader, Slot, VariableList,
};
use crate::{
runtime_var_list::RuntimeFixedList, ForkVersionDeserialize, KzgProofs, RuntimeVariableList,
SignedBeaconBlock,
Epoch, EthSpec, FixedVector, ForkVersionDeserialize, Hash256, KzgProofs, RuntimeFixedVector,
RuntimeVariableList, SignedBeaconBlock, SignedBeaconBlockHeader, Slot, VariableList,
};
use crate::{ChainSpec, ForkName};
use bls::Signature;
@@ -297,7 +294,7 @@ impl<E: EthSpec> BlobSidecar<E> {
pub type BlobSidecarList<E> = RuntimeVariableList<Arc<BlobSidecar<E>>>;
/// Alias for a non length-constrained list of `BlobSidecar`s.
pub type FixedBlobSidecarList<E> = RuntimeFixedList<Option<Arc<BlobSidecar<E>>>>;
pub type FixedBlobSidecarList<E> = RuntimeFixedVector<Option<Arc<BlobSidecar<E>>>>;
pub type BlobsList<E> = VariableList<Blob<E>, <E as EthSpec>::MaxBlobCommitmentsPerBlock>;
impl<E: EthSpec> ForkVersionDeserialize for BlobSidecarList<E> {

View File

@@ -108,6 +108,7 @@ pub mod data_column_sidecar;
pub mod data_column_subnet_id;
pub mod light_client_header;
pub mod non_zero_usize;
pub mod runtime_fixed_vector;
pub mod runtime_var_list;
pub use crate::activation_queue::ActivationQueue;
@@ -219,6 +220,7 @@ pub use crate::preset::{
pub use crate::proposer_preparation_data::ProposerPreparationData;
pub use crate::proposer_slashing::ProposerSlashing;
pub use crate::relative_epoch::{Error as RelativeEpochError, RelativeEpoch};
pub use crate::runtime_fixed_vector::RuntimeFixedVector;
pub use crate::runtime_var_list::RuntimeVariableList;
pub use crate::selection_proof::SelectionProof;
pub use crate::shuffling_id::AttestationShufflingId;

View File

@@ -0,0 +1,78 @@
/// Emulates a SSZ `Vector`.
#[derive(Clone, Debug)]
pub struct RuntimeFixedVector<T> {
vec: Vec<T>,
len: usize,
}
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()
}
}

View File

@@ -262,85 +262,6 @@ where
}
}
/// Emulates a SSZ `Vector`.
#[derive(Clone, Debug)]
pub struct RuntimeFixedList<T> {
vec: Vec<T>,
len: usize,
}
impl<T: Clone + Default> RuntimeFixedList<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 RuntimeFixedList<T> {
type Target = [T];
fn deref(&self) -> &[T] {
&self.vec[..]
}
}
impl<T> std::ops::DerefMut for RuntimeFixedList<T> {
fn deref_mut(&mut self) -> &mut [T] {
&mut self.vec[..]
}
}
impl<T> IntoIterator for RuntimeFixedList<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 RuntimeFixedList<T> {
type Item = &'a T;
type IntoIter = std::slice::Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.vec.iter()
}
}
#[cfg(test)]
mod test {
use super::*;