Skip to content

Commit 3ec778f

Browse files
committed
Early returns
1 parent bdfc429 commit 3ec778f

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

lib.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -796,15 +796,14 @@ impl<A: Array> SmallVec<A> {
796796
// so that the optimizer removes duplicated calls to it
797797
// from callers like insert()
798798
let (_, &mut len, cap) = self.triple_mut();
799-
if cap - len < additional {
800-
let new_cap = len
801-
.checked_add(additional)
802-
.and_then(usize::checked_next_power_of_two)
803-
.ok_or(CollectionAllocErr::CapacityOverflow)?;
804-
self.try_grow(new_cap)
805-
} else {
806-
Ok(())
799+
if cap - len >= additional {
800+
return Ok(());
807801
}
802+
let new_cap = len
803+
.checked_add(additional)
804+
.and_then(usize::checked_next_power_of_two)
805+
.ok_or(CollectionAllocErr::CapacityOverflow)?;
806+
self.try_grow(new_cap)
808807
}
809808

810809
/// Reserve the minimum capacity for `additional` more elements to be inserted.
@@ -817,14 +816,13 @@ impl<A: Array> SmallVec<A> {
817816
/// Reserve the minimum capacity for `additional` more elements to be inserted.
818817
pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), CollectionAllocErr> {
819818
let (_, &mut len, cap) = self.triple_mut();
820-
if cap - len < additional {
821-
let new_cap = len
822-
.checked_add(additional)
823-
.ok_or(CollectionAllocErr::CapacityOverflow)?;
824-
self.try_grow(new_cap)
825-
} else {
826-
Ok(())
819+
if cap - len >= additional {
820+
return Ok(());
827821
}
822+
let new_cap = len
823+
.checked_add(additional)
824+
.ok_or(CollectionAllocErr::CapacityOverflow)?;
825+
self.try_grow(new_cap)
828826
}
829827

830828
/// Shrink the capacity of the vector as much as possible.

0 commit comments

Comments
 (0)