Skip to content

Commit 960d610

Browse files
vec/raw: Simplify RawVec::grow (#392)
1 parent 03fa5be commit 960d610

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

src/vec/vec-raw.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,13 @@ impl<T> RawVec<T> {
2828
}
2929
3030
fn grow(&mut self) {
31-
let (new_cap, new_layout) = if self.cap == 0 {
32-
(1, Layout::array::<T>(1).unwrap())
33-
} else {
34-
// This can't overflow because we ensure self.cap <= isize::MAX.
35-
let new_cap = 2 * self.cap;
36-
37-
// Layout::array checks that the number of bytes is <= usize::MAX,
38-
// but this is redundant since old_layout.size() <= isize::MAX,
39-
// so the `unwrap` should never fail.
40-
let new_layout = Layout::array::<T>(new_cap).unwrap();
41-
(new_cap, new_layout)
42-
};
31+
// This can't overflow because we ensure self.cap <= isize::MAX.
32+
let new_cap = if self.cap == 0 { 1 } else { 2 * self.cap };
33+
34+
// Layout::array checks that the number of bytes is <= usize::MAX,
35+
// but this is redundant since old_layout.size() <= isize::MAX,
36+
// so the `unwrap` should never fail.
37+
let new_layout = Layout::array::<T>(new_cap).unwrap();
4338
4439
// Ensure that the new allocation doesn't exceed `isize::MAX` bytes.
4540
assert!(new_layout.size() <= isize::MAX as usize, "Allocation too large");

0 commit comments

Comments
 (0)