//! This crate implements `TypedArena`, a simple arena that can only hold
//! objects of a single type.
-#![doc(html_root_url = "https://doc.rust-lang.org/nightly/",
- test(no_crate_inject, attr(deny(warnings))))]
-
+#![doc(
+ html_root_url = "https://doc.rust-lang.org/nightly/",
+ test(no_crate_inject, attr(deny(warnings)))
+)]
#![feature(core_intrinsics)]
#![feature(dropck_eyepatch)]
#![feature(raw_vec_internals)]
#![cfg_attr(test, feature(test))]
-
#![allow(deprecated)]
extern crate alloc;
impl<T> TypedArenaChunk<T> {
#[inline]
unsafe fn new(capacity: usize) -> TypedArenaChunk<T> {
- TypedArenaChunk {
- storage: RawVec::with_capacity(capacity),
- entries: 0,
- }
+ TypedArenaChunk { storage: RawVec::with_capacity(capacity), entries: 0 }
}
/// Destroys this arena chunk.
unsafe {
if mem::size_of::<T>() == 0 {
- self.ptr
- .set(intrinsics::arith_offset(self.ptr.get() as *mut u8, 1)
- as *mut T);
+ self.ptr.set(intrinsics::arith_offset(self.ptr.get() as *mut u8, 1) as *mut T);
let ptr = mem::align_of::<T>() as *mut T;
// Don't drop the object. This `write` is equivalent to `forget`.
ptr::write(ptr, object);
#[inline]
pub fn alloc_from_iter<I: IntoIterator<Item = T>>(&self, iter: I) -> &mut [T] {
assert!(mem::size_of::<T>() != 0);
- let mut iter = iter.into_iter();
- let size_hint = iter.size_hint();
-
- match size_hint {
- (min, Some(max)) if min == max => {
- // We know the exact number of elements the iterator will produce here
- let len = min;
-
- if len == 0 {
- return &mut [];
- }
-
- self.ensure_capacity(len);
-
- let slice = self.ptr.get();
-
- unsafe {
- let mut ptr = self.ptr.get();
- for _ in 0..len {
- // Write into uninitialized memory.
- ptr::write(ptr, iter.next().unwrap());
- // Advance the pointer.
- ptr = ptr.offset(1);
- // Update the pointer per iteration so if `iter.next()` panics
- // we destroy the correct amount
- self.ptr.set(ptr);
- }
- slice::from_raw_parts_mut(slice, len)
- }
- }
- _ => {
- cold_path(move || -> &mut [T] {
- let mut vec: SmallVec<[_; 8]> = iter.collect();
- if vec.is_empty() {
- return &mut [];
- }
- // Move the content to the arena by copying it and then forgetting
- // the content of the SmallVec
- unsafe {
- let len = vec.len();
- let start_ptr = self.alloc_raw_slice(len);
- vec.as_ptr().copy_to_nonoverlapping(start_ptr, len);
- vec.set_len(0);
- slice::from_raw_parts_mut(start_ptr, len)
- }
- })
- }
+ let mut vec: SmallVec<[_; 8]> = iter.into_iter().collect();
+ if vec.is_empty() {
+ return &mut [];
+ }
+ // Move the content to the arena by copying it and then forgetting
+ // the content of the SmallVec
+ unsafe {
+ let len = vec.len();
+ let start_ptr = self.alloc_raw_slice(len);
+ vec.as_ptr().copy_to_nonoverlapping(start_ptr, len);
+ vec.set_len(0);
+ slice::from_raw_parts_mut(start_ptr, len)
}
}
self.clear_last_chunk(&mut last_chunk);
let len = chunks_borrow.len();
// If `T` is ZST, code below has no effect.
- for mut chunk in chunks_borrow.drain(..len-1) {
+ for mut chunk in chunks_borrow.drain(..len - 1) {
chunk.destroy(chunk.entries);
}
}
let (chunk, mut new_capacity);
if let Some(last_chunk) = chunks.last_mut() {
let used_bytes = self.ptr.get() as usize - last_chunk.start() as usize;
- if last_chunk
- .storage
- .reserve_in_place(used_bytes, needed_bytes)
- {
+ if last_chunk.storage.reserve_in_place(used_bytes, needed_bytes) {
self.end.set(last_chunk.end());
return;
} else {
let ptr = self.ptr.get();
// Set the pointer past ourselves
- self.ptr.set(
- intrinsics::arith_offset(self.ptr.get(), bytes as isize) as *mut u8,
- );
+ self.ptr.set(intrinsics::arith_offset(self.ptr.get(), bytes as isize) as *mut u8);
slice::from_raw_parts_mut(ptr, bytes)
}
}
pub fn alloc<T>(&self, object: T) -> &mut T {
assert!(!mem::needs_drop::<T>());
- let mem = self.alloc_raw(
- mem::size_of::<T>(),
- mem::align_of::<T>()) as *mut _ as *mut T;
+ let mem = self.alloc_raw(mem::size_of::<T>(), mem::align_of::<T>()) as *mut _ as *mut T;
unsafe {
// Write into uninitialized memory.
assert!(mem::size_of::<T>() != 0);
assert!(!slice.is_empty());
- let mem = self.alloc_raw(
- slice.len() * mem::size_of::<T>(),
- mem::align_of::<T>()) as *mut _ as *mut T;
+ let mem = self.alloc_raw(slice.len() * mem::size_of::<T>(), mem::align_of::<T>()) as *mut _
+ as *mut T;
unsafe {
let arena_slice = slice::from_raw_parts_mut(mem, slice.len());
let len = min;
if len == 0 {
- return &mut []
+ return &mut [];
}
let size = len.checked_mul(mem::size_of::<T>()).unwrap();
let mem = self.alloc_raw(size, mem::align_of::<T>()) as *mut _ as *mut T;
- unsafe {
- self.write_from_iter(iter, len, mem)
- }
+ unsafe { self.write_from_iter(iter, len, mem) }
}
(_, _) => {
cold_path(move || -> &mut [T] {
// the content of the SmallVec
unsafe {
let len = vec.len();
- let start_ptr = self.alloc_raw(
- len * mem::size_of::<T>(),
- mem::align_of::<T>()
- ) as *mut _ as *mut T;
+ let start_ptr = self
+ .alloc_raw(len * mem::size_of::<T>(), mem::align_of::<T>())
+ as *mut _ as *mut T;
vec.as_ptr().copy_to_nonoverlapping(start_ptr, len);
vec.set_len(0);
slice::from_raw_parts_mut(start_ptr, len)