@@ -16,12 +16,10 @@ want to use `empty` because there's no real allocation to talk about but
1616So:
1717
1818``` rust,ignore 
19- #![feature(alloc, heap_api)] 
20- 
2119use std::mem; 
2220
2321impl<T> Vec<T> { 
24-     fn new() -> Self { 
22+     pub  fn new() -> Self { 
2523        assert!(mem::size_of::<T>() != 0, "We're not ready to handle ZSTs"); 
2624        Vec { ptr: Unique::empty(), len: 0, cap: 0 } 
2725    } 
@@ -157,46 +155,40 @@ such we will guard against this case explicitly.
157155Ok with all the nonsense out of the way, let's actually allocate some memory:
158156
159157``` rust,ignore 
160- use std::alloc::oom ; 
158+ use std::alloc; 
161159
162- fn grow(&mut self) { 
160+ pub  fn grow(&mut self) {
163161    // this is all pretty delicate, so let's say it's all unsafe 
164162    unsafe { 
165-         // current API requires us to specify size and alignment manually. 
166-         let align = mem::align_of::<T>(); 
167-         let elem_size = mem::size_of::<T>(); 
163+         let layout = alloc::Layout::new::<T>(); 
168164
169165        let (new_cap, ptr) = if self.cap == 0 { 
170-             let ptr = heap::allocate(elem_size, align ); 
166+             let ptr = alloc::alloc(layout ); 
171167            (1, ptr) 
172168        } else { 
173169            // as an invariant, we can assume that `self.cap < isize::MAX`, 
174170            // so this doesn't need to be checked. 
175171            let new_cap = self.cap * 2; 
176172            // Similarly this can't overflow due to previously allocating this 
177-             let old_num_bytes = self.cap * elem_size ; 
173+             let old_num_bytes = self.cap * mem::size_of::<T>() ; 
178174
179175            // check that the new allocation doesn't exceed `isize::MAX` at all 
180176            // regardless of the actual size of the capacity. This combines the 
181177            // `new_cap <= isize::MAX` and `new_num_bytes <= usize::MAX` checks 
182178            // we need to make. We lose the ability to allocate e.g. 2/3rds of 
183179            // the address space with a single Vec of i16's on 32-bit though. 
184180            // Alas, poor Yorick -- I knew him, Horatio. 
185-             assert!(old_num_bytes <= (::std::isize::MAX as usize) / 2, 
186-                     "capacity overflow"); 
181+             assert!(old_num_bytes <= (std::isize::MAX as usize) / 2, "capacity overflow"); 
187182
188183            let new_num_bytes = old_num_bytes * 2; 
189-             let ptr = heap::reallocate(self.ptr.as_ptr() as *mut _, 
190-                                         old_num_bytes, 
191-                                         new_num_bytes, 
192-                                         align); 
184+             let ptr = alloc::realloc(self.ptr.as_ptr() as *mut u8, layout, new_num_bytes); 
193185            (new_cap, ptr) 
194186        }; 
195187
196188        // If allocate or reallocate fail, we'll get `null` back 
197-         if ptr.is_null() { oom( ); } 
189+         if ptr.is_null() { alloc::handle_alloc_error(layout ); } 
198190
199-         self.ptr = Unique::new (ptr as *mut _ ); 
191+         self.ptr = Unique::new_unchecked (ptr as *mut u8 ); 
200192        self.cap = new_cap; 
201193    } 
202194} 
0 commit comments