Skip to content

Commit

Permalink
Fix shrink and capacity_from_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
a1phyr committed Feb 12, 2022
1 parent 6027182 commit d3e2ffc
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions library/alloc/src/raw_vec.rs
Expand Up @@ -425,10 +425,11 @@ impl<T, A: Allocator> RawVec<T, A> {
assert!(cap <= self.capacity(), "Tried to shrink to a larger capacity");

let (ptr, layout) = if let Some(mem) = self.current_memory() { mem } else { return Ok(()) };
let new_size = cap * mem::size_of::<T>();

let ptr = unsafe {
let new_layout = Layout::from_size_align_unchecked(new_size, layout.align());
// `Layout::array` cannot overflow here because it would have
// owerflown earlier when capacity was larger.
let new_layout = Layout::array::<T>(cap).unwrap_unchecked();
self.alloc
.shrink(ptr, layout, new_layout)
.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () })?
Expand Down

0 comments on commit d3e2ffc

Please sign in to comment.