Skip to content

Commit

Permalink
Bitfield::with_capacity fix OutOfBounds i field, plus doc typo fix (#33)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
carver authored Sep 26, 2024
1 parent dc04526 commit 46e266c
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions ssz/src/bitfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl<N: Unsigned + Clone> Bitfield<Variable<N>> {
///
/// 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<Self, Error> {
if num_bits <= N::to_usize() {
Ok(Self {
Expand All @@ -135,7 +135,7 @@ impl<N: Unsigned + Clone> Bitfield<Variable<N>> {
})
} else {
Err(Error::OutOfBounds {
i: Self::max_len(),
i: num_bits,
len: Self::max_len(),
})
}
Expand Down Expand Up @@ -1429,4 +1429,10 @@ mod bitlist {
// Can't extend a BitList to a smaller BitList
resized_bit_list.resize::<typenum::U16>().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 });
}
}

0 comments on commit 46e266c

Please sign in to comment.