Skip to content

Commit

Permalink
vec/raw: Simplify RawVec::grow (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbraud authored Jan 6, 2023
1 parent 03fa5be commit 960d610
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/vec/vec-raw.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,13 @@ impl<T> RawVec<T> {
}
fn grow(&mut self) {
let (new_cap, new_layout) = if self.cap == 0 {
(1, Layout::array::<T>(1).unwrap())
} else {
// This can't overflow because we ensure self.cap <= isize::MAX.
let new_cap = 2 * self.cap;
// Layout::array checks that the number of bytes is <= usize::MAX,
// but this is redundant since old_layout.size() <= isize::MAX,
// so the `unwrap` should never fail.
let new_layout = Layout::array::<T>(new_cap).unwrap();
(new_cap, new_layout)
};
// This can't overflow because we ensure self.cap <= isize::MAX.
let new_cap = if self.cap == 0 { 1 } else { 2 * self.cap };
// Layout::array checks that the number of bytes is <= usize::MAX,
// but this is redundant since old_layout.size() <= isize::MAX,
// so the `unwrap` should never fail.
let new_layout = Layout::array::<T>(new_cap).unwrap();
// Ensure that the new allocation doesn't exceed `isize::MAX` bytes.
assert!(new_layout.size() <= isize::MAX as usize, "Allocation too large");
Expand Down

0 comments on commit 960d610

Please sign in to comment.