From 46e266c70a77a9ecebe6829aefc62218fb4ee52e Mon Sep 17 00:00:00 2001 From: Jason Carver Date: Wed, 25 Sep 2024 20:27:42 -0700 Subject: [PATCH] Bitfield::with_capacity fix OutOfBounds i field, plus doc typo fix (#33) * docs: Bitfield::with_capacity returns Result not Option * 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 | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ssz/src/bitfield.rs b/ssz/src/bitfield.rs index 7bac6bd..15c9eba 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 { @@ -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 }); + } }