Rollup merge of #47069 - Kagamihime:master, r=nrc
rustfmt libarena/lib.rs Note: it's my very first pull request. I'm trying to do something very simple to see how it works here, even if it's a tiny change or maybe it's not correct (sorry if it is the case). r? @nrc
This commit is contained in:
commit
30a35164ce
@ -69,7 +69,9 @@ struct TypedArenaChunk<T> {
|
||||
impl<T> TypedArenaChunk<T> {
|
||||
#[inline]
|
||||
unsafe fn new(capacity: usize) -> TypedArenaChunk<T> {
|
||||
TypedArenaChunk { storage: RawVec::with_capacity(capacity) }
|
||||
TypedArenaChunk {
|
||||
storage: RawVec::with_capacity(capacity),
|
||||
}
|
||||
}
|
||||
|
||||
/// Destroys this arena chunk.
|
||||
@ -132,7 +134,9 @@ impl<T> TypedArena<T> {
|
||||
|
||||
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);
|
||||
@ -157,7 +161,9 @@ impl<T> TypedArena<T> {
|
||||
/// - Zero-length slices
|
||||
#[inline]
|
||||
pub fn alloc_slice(&self, slice: &[T]) -> &mut [T]
|
||||
where T: Copy {
|
||||
where
|
||||
T: Copy,
|
||||
{
|
||||
assert!(mem::size_of::<T>() != 0);
|
||||
assert!(slice.len() != 0);
|
||||
|
||||
@ -321,7 +327,10 @@ impl DroplessArena {
|
||||
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 {
|
||||
@ -357,9 +366,9 @@ impl DroplessArena {
|
||||
|
||||
let ptr = self.ptr.get();
|
||||
// Set the pointer past ourselves
|
||||
self.ptr.set(intrinsics::arith_offset(
|
||||
self.ptr.get(), mem::size_of::<T>() as isize
|
||||
) as *mut u8);
|
||||
self.ptr.set(
|
||||
intrinsics::arith_offset(self.ptr.get(), mem::size_of::<T>() as isize) as *mut u8,
|
||||
);
|
||||
// Write into uninitialized memory.
|
||||
ptr::write(ptr as *mut T, object);
|
||||
&mut *(ptr as *mut T)
|
||||
@ -375,7 +384,9 @@ impl DroplessArena {
|
||||
/// - Zero-length slices
|
||||
#[inline]
|
||||
pub fn alloc_slice<T>(&self, slice: &[T]) -> &mut [T]
|
||||
where T: Copy {
|
||||
where
|
||||
T: Copy,
|
||||
{
|
||||
assert!(!mem::needs_drop::<T>());
|
||||
assert!(mem::size_of::<T>() != 0);
|
||||
assert!(slice.len() != 0);
|
||||
@ -391,7 +402,8 @@ impl DroplessArena {
|
||||
unsafe {
|
||||
let arena_slice = slice::from_raw_parts_mut(self.ptr.get() as *mut T, slice.len());
|
||||
self.ptr.set(intrinsics::arith_offset(
|
||||
self.ptr.get(), (slice.len() * mem::size_of::<T>()) as isize
|
||||
self.ptr.get(),
|
||||
(slice.len() * mem::size_of::<T>()) as isize,
|
||||
) as *mut u8);
|
||||
arena_slice.copy_from_slice(slice);
|
||||
arena_slice
|
||||
@ -456,8 +468,9 @@ mod tests {
|
||||
|
||||
let arena = Wrap(TypedArena::new());
|
||||
|
||||
let result =
|
||||
arena.alloc_outer(|| Outer { inner: arena.alloc_inner(|| Inner { value: 10 }) });
|
||||
let result = arena.alloc_outer(|| Outer {
|
||||
inner: arena.alloc_inner(|| Inner { value: 10 }),
|
||||
});
|
||||
|
||||
assert_eq!(result.inner.value, 10);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user