use core::ptr::{self, NonNull, Unique};
use core::slice;
-use alloc::{Alloc, Layout, Global};
+use alloc::{Alloc, Layout, Global, oom};
use alloc::CollectionAllocErr;
use alloc::CollectionAllocErr::*;
use boxed::Box;
}
}
+ /// Like `empty` but parametrized over the choice of allocator for the returned `RawVec`.
+ pub const fn empty_in(a: A) -> Self {
+ // Unique::empty() doubles as "unallocated" and "zero-sized allocation"
+ RawVec {
+ ptr: Unique::empty(),
+ cap: 0,
+ a,
+ }
+ }
+
/// Like `with_capacity` but parameterized over the choice of
/// allocator for the returned RawVec.
#[inline]
};
match result {
Ok(ptr) => ptr,
- Err(_) => a.oom(),
+ Err(_) => oom(),
}
};
Self::new_in(Global)
}
+ /// Create a `RawVec` with capcity 0 (on the system heap), regardless of `T`, without
+ /// allocating.
+ pub fn empty() -> Self {
+ Self::empty_in(Global)
+ }
+
/// Creates a RawVec (on the system heap) with exactly the
/// capacity and alignment requirements for a `[T; cap]`. This is
/// equivalent to calling RawVec::new when `cap` is 0 or T is
new_size);
match ptr_res {
Ok(ptr) => (new_cap, ptr.cast().into()),
- Err(_) => self.a.oom(),
+ Err(_) => oom(),
}
}
None => {
let new_cap = if elem_size > (!0) / 8 { 1 } else { 4 };
match self.a.alloc_array::<T>(new_cap) {
Ok(ptr) => (new_cap, ptr.into()),
- Err(_) => self.a.oom(),
+ Err(_) => oom(),
}
}
};
pub fn reserve_exact(&mut self, used_cap: usize, needed_extra_cap: usize) {
match self.try_reserve_exact(used_cap, needed_extra_cap) {
Err(CapacityOverflow) => capacity_overflow(),
- Err(AllocErr) => self.a.oom(),
+ Err(AllocErr) => oom(),
Ok(()) => { /* yay */ }
}
}
pub fn reserve(&mut self, used_cap: usize, needed_extra_cap: usize) {
match self.try_reserve(used_cap, needed_extra_cap) {
Err(CapacityOverflow) => capacity_overflow(),
- Err(AllocErr) => self.a.oom(),
+ Err(AllocErr) => oom(),
Ok(()) => { /* yay */ }
}
}
old_layout,
new_size) {
Ok(p) => self.ptr = p.cast().into(),
- Err(_) => self.a.oom(),
+ Err(_) => oom(),
}
}
self.cap = amount;