Skip to content

Commit 6eb24d6

Browse files
authored
Merge pull request #207 from faern/use-assoc-int-consts
Use isize::MAX directly on type instead of module
2 parents 411197b + 1d517f8 commit 6eb24d6

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/vec-alloc.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ fn grow(&mut self) {
182182
// we need to make. We lose the ability to allocate e.g. 2/3rds of
183183
// the address space with a single Vec of i16's on 32-bit though.
184184
// Alas, poor Yorick -- I knew him, Horatio.
185-
assert!(old_num_bytes <= (::std::isize::MAX as usize) / 2,
185+
assert!(old_num_bytes <= (isize::MAX as usize) / 2,
186186
"capacity overflow");
187187
188188
let new_num_bytes = old_num_bytes * 2;

src/vec-final.md

+16-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ use std::ptr::{Unique, NonNull, self};
99
use std::mem;
1010
use std::ops::{Deref, DerefMut};
1111
use std::marker::PhantomData;
12-
use std::alloc::{AllocRef, GlobalAlloc, Layout, Global, handle_alloc_error};
12+
use std::alloc::{
13+
AllocInit,
14+
AllocRef,
15+
Global,
16+
GlobalAlloc,
17+
Layout,
18+
ReallocPlacement,
19+
handle_alloc_error
20+
};
1321

1422
struct RawVec<T> {
1523
ptr: Unique<T>,
@@ -34,14 +42,16 @@ impl<T> RawVec<T> {
3442
assert!(elem_size != 0, "capacity overflow");
3543

3644
let (new_cap, ptr) = if self.cap == 0 {
37-
let ptr = Global.alloc(Layout::array::<T>(1).unwrap());
45+
let ptr = Global.alloc(Layout::array::<T>(1).unwrap(), AllocInit::Uninitialized);
3846
(1, ptr)
3947
} else {
4048
let new_cap = 2 * self.cap;
4149
let c: NonNull<T> = self.ptr.into();
42-
let ptr = Global.realloc(c.cast(),
43-
Layout::array::<T>(self.cap).unwrap(),
44-
Layout::array::<T>(new_cap).unwrap().size());
50+
let ptr = Global.grow(c.cast(),
51+
Layout::array::<T>(self.cap).unwrap(),
52+
Layout::array::<T>(new_cap).unwrap().size(),
53+
ReallocPlacement::MayMove,
54+
AllocInit::Uninitialized);
4555
(new_cap, ptr)
4656
};
4757

@@ -52,7 +62,7 @@ impl<T> RawVec<T> {
5262
mem::align_of::<T>(),
5363
))
5464
}
55-
let (ptr, _) = ptr.unwrap();
65+
let ptr = ptr.unwrap().ptr;
5666

5767
self.ptr = Unique::new_unchecked(ptr.as_ptr() as *mut _);
5868
self.cap = new_cap;

0 commit comments

Comments
 (0)