diff --git a/src/vec-alloc.md b/src/vec-alloc.md index 694059d5..b19de4ab 100644 --- a/src/vec-alloc.md +++ b/src/vec-alloc.md @@ -182,7 +182,7 @@ fn grow(&mut self) { // we need to make. We lose the ability to allocate e.g. 2/3rds of // the address space with a single Vec of i16's on 32-bit though. // Alas, poor Yorick -- I knew him, Horatio. - assert!(old_num_bytes <= (::std::isize::MAX as usize) / 2, + assert!(old_num_bytes <= (isize::MAX as usize) / 2, "capacity overflow"); let new_num_bytes = old_num_bytes * 2; diff --git a/src/vec-final.md b/src/vec-final.md index a858547e..0c3f042d 100644 --- a/src/vec-final.md +++ b/src/vec-final.md @@ -9,7 +9,15 @@ use std::ptr::{Unique, NonNull, self}; use std::mem; use std::ops::{Deref, DerefMut}; use std::marker::PhantomData; -use std::alloc::{AllocRef, GlobalAlloc, Layout, Global, handle_alloc_error}; +use std::alloc::{ + AllocInit, + AllocRef, + Global, + GlobalAlloc, + Layout, + ReallocPlacement, + handle_alloc_error +}; struct RawVec { ptr: Unique, @@ -34,14 +42,16 @@ impl RawVec { assert!(elem_size != 0, "capacity overflow"); let (new_cap, ptr) = if self.cap == 0 { - let ptr = Global.alloc(Layout::array::(1).unwrap()); + let ptr = Global.alloc(Layout::array::(1).unwrap(), AllocInit::Uninitialized); (1, ptr) } else { let new_cap = 2 * self.cap; let c: NonNull = self.ptr.into(); - let ptr = Global.realloc(c.cast(), - Layout::array::(self.cap).unwrap(), - Layout::array::(new_cap).unwrap().size()); + let ptr = Global.grow(c.cast(), + Layout::array::(self.cap).unwrap(), + Layout::array::(new_cap).unwrap().size(), + ReallocPlacement::MayMove, + AllocInit::Uninitialized); (new_cap, ptr) }; @@ -52,7 +62,7 @@ impl RawVec { mem::align_of::(), )) } - let (ptr, _) = ptr.unwrap(); + let ptr = ptr.unwrap().ptr; self.ptr = Unique::new_unchecked(ptr.as_ptr() as *mut _); self.cap = new_cap;