From 637144810bfc85438b12a03e70eddf95cdc306fd Mon Sep 17 00:00:00 2001 From: Jason Carver Date: Tue, 24 Sep 2024 20:18:31 -0700 Subject: [PATCH 1/2] docs: Bitfield::with_capacity returns Result not Option --- ssz/src/bitfield.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ssz/src/bitfield.rs b/ssz/src/bitfield.rs index 7bac6bd..ff34299 100644 --- a/ssz/src/bitfield.rs +++ b/ssz/src/bitfield.rs @@ -125,7 +125,7 @@ impl Bitfield> { /// /// All bits are initialized to `false`. /// - /// Returns `None` if `num_bits > N`. + /// Returns `Err` if `num_bits > N`. pub fn with_capacity(num_bits: usize) -> Result { if num_bits <= N::to_usize() { Ok(Self { From 90ed13086b74feecaaa24e36032647edba374f14 Mon Sep 17 00:00:00 2001 From: Jason Carver Date: Tue, 24 Sep 2024 21:06:20 -0700 Subject: [PATCH 2/2] fix: Bitfield::with_capacity OutOfBounds field bug It was returning the max length twice, and now returns the actual length and maximum length. --- ssz/src/bitfield.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ssz/src/bitfield.rs b/ssz/src/bitfield.rs index ff34299..15c9eba 100644 --- a/ssz/src/bitfield.rs +++ b/ssz/src/bitfield.rs @@ -135,7 +135,7 @@ impl Bitfield> { }) } else { Err(Error::OutOfBounds { - i: Self::max_len(), + i: num_bits, len: Self::max_len(), }) } @@ -1429,4 +1429,10 @@ mod bitlist { // Can't extend a BitList to a smaller BitList resized_bit_list.resize::().unwrap_err(); } + + #[test] + fn over_capacity_err() { + let e = BitList8::with_capacity(9).expect_err("over-sized bit list"); + assert_eq!(e, Error::OutOfBounds { i: 9, len: 8 }); + } }