@@ -10,6 +10,11 @@ use std::slice::SliceIndex;
1010/// An ordered, heap-allocated, variable-length, homogeneous collection of `T`, with no more than
1111/// `max_len` values.
1212///
13+ /// In cases where the `max_length` of the container is unknown at time of initialization, we provide
14+ /// a `Self::empty_uninitialized` constructor that initializes a runtime list without setting the max_len.
15+ ///
16+ /// To ensure there are no inconsistent states, we do not allow any mutating operation if `max_len` is not set.
17+ ///
1318/// ## Example
1419///
1520/// ```
@@ -35,6 +40,16 @@ use std::slice::SliceIndex;
3540///
3641/// // Push a value to if it _does_ exceed the maximum.
3742/// assert!(long.push(6).is_err());
43+ ///
44+ /// let mut uninit = RuntimeVariableList::empty_unitialized();
45+ /// assert!(uninit.push(5).is_err());
46+ ///
47+ /// // Set max_len to allow mutation.
48+ /// uninit.set_max_len(5usize);
49+ ///
50+ /// uninit.push(5).unwrap();
51+ /// assert_eq!(&uninit[..], &[5]);
52+ ///
3853/// ```
3954#[ derive( Debug , Clone , Serialize , Deserialize , Derivative ) ]
4055#[ derivative( PartialEq , Eq , Hash ( bound = "T: std::hash::Hash" ) ) ]
@@ -73,7 +88,7 @@ impl<T> RuntimeVariableList<T> {
7388 }
7489 }
7590
76- /// Create an empty list.
91+ /// Create an empty list with the given `max_len` .
7792 pub fn empty ( max_len : usize ) -> Self {
7893 Self {
7994 vec : vec ! [ ] ,
@@ -95,7 +110,7 @@ impl<T> RuntimeVariableList<T> {
95110 /// Returns an instance of `Self` with max_len = None.
96111 ///
97112 /// No mutating operation can be performed on an uninitialized instance
98- /// without first setting max_len.
113+ /// without first setting ` max_len` .
99114 pub fn empty_uninitialized ( ) -> Self {
100115 Self {
101116 vec : vec ! [ ] ,
@@ -129,7 +144,7 @@ impl<T> RuntimeVariableList<T> {
129144 /// Returns `Err(())` when appending `value` would exceed the maximum length.
130145 pub fn push ( & mut self , value : T ) -> Result < ( ) , Error > {
131146 let Some ( max_len) = self . max_len else {
132- // TODO(pawan): set a better error
147+ // TODO(pawan): set a better error?
133148 return Err ( Error :: MissingLengthInformation ) ;
134149 } ;
135150 if self . vec . len ( ) < max_len {
@@ -247,6 +262,7 @@ where
247262 }
248263}
249264
265+ /// Emulates a SSZ `Vector`.
250266#[ derive( Clone , Debug ) ]
251267pub struct RuntimeFixedList < T > {
252268 vec : Vec < T > ,
@@ -267,6 +283,7 @@ impl<T: Clone + Default> RuntimeFixedList<T> {
267283 self . vec . as_slice ( )
268284 }
269285
286+ #[ allow( clippy:: len_without_is_empty) ]
270287 pub fn len ( & self ) -> usize {
271288 self . len
272289 }
0 commit comments