Skip to content

Commit 691af72

Browse files
authored
fix: update to_raw_capacity to return Result instead of panic (#787)
1 parent 8be6742 commit 691af72

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

src/header/map.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,8 @@ impl<T> HeaderMap<T> {
552552
if capacity == 0 {
553553
Ok(Self::default())
554554
} else {
555-
let raw_cap = match to_raw_capacity(capacity).checked_next_power_of_two() {
555+
let raw_cap = to_raw_capacity(capacity)?;
556+
let raw_cap = match raw_cap.checked_next_power_of_two() {
556557
Some(c) => c,
557558
None => return Err(MaxSizeReached { _priv: () }),
558559
};
@@ -750,7 +751,7 @@ impl<T> HeaderMap<T> {
750751
.checked_add(additional)
751752
.ok_or_else(MaxSizeReached::new)?;
752753

753-
let raw_cap = to_raw_capacity(cap);
754+
let raw_cap = to_raw_capacity(cap)?;
754755

755756
if raw_cap > self.indices.len() {
756757
let raw_cap = raw_cap
@@ -3621,14 +3622,8 @@ fn usable_capacity(cap: usize) -> usize {
36213622
}
36223623

36233624
#[inline]
3624-
fn to_raw_capacity(n: usize) -> usize {
3625-
match n.checked_add(n / 3) {
3626-
Some(n) => n,
3627-
None => panic!(
3628-
"requested capacity {} too large: overflow while converting to raw capacity",
3629-
n
3630-
),
3631-
}
3625+
fn to_raw_capacity(n: usize) -> Result<usize, MaxSizeReached> {
3626+
n.checked_add(n / 3).ok_or_else(MaxSizeReached::new)
36323627
}
36333628

36343629
#[inline]

0 commit comments

Comments
 (0)