Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 2 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions src/rawsmallvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ impl<T, const N: usize> RawSmallVec<T, N> {
/// The vector must be on the heap
#[inline]
pub const unsafe fn as_ptr_heap(&self) -> *const T {
self.heap.0.as_ptr()
unsafe { self.heap.0.as_ptr() }
}

/// # Safety
///
/// The vector must be on the heap
#[inline]
pub const unsafe fn as_mut_ptr_heap(&mut self) -> *mut T {
self.heap.0.as_ptr()
unsafe { self.heap.0.as_ptr() }
}

/// # Safety
Expand All @@ -100,7 +100,7 @@ impl<T, const N: usize> RawSmallVec<T, N> {

let was_on_heap = len.on_heap();
let ptr = if was_on_heap {
self.as_mut_ptr_heap()
unsafe { self.as_mut_ptr_heap() }
} else {
self.as_mut_ptr_inline()
};
Expand All @@ -114,27 +114,29 @@ impl<T, const N: usize> RawSmallVec<T, N> {

let new_ptr = if !was_on_heap {
// get a fresh allocation
let new_ptr = alloc(new_layout) as *mut T; // `new_layout` has nonzero size.
let new_ptr = unsafe { alloc(new_layout) } as *mut T; // `new_layout` has nonzero size.
let new_ptr = NonNull::new(new_ptr).ok_or(CollectionAllocErr::AllocErr {
layout: new_layout
})?;
copy_nonoverlapping(ptr, new_ptr.as_ptr(), len);
unsafe { copy_nonoverlapping(ptr, new_ptr.as_ptr(), len) };
new_ptr
} else {
// use realloc

// this can't overflow since we already constructed an equivalent
// layout during the previous allocation
let old_layout =
Layout::from_size_align_unchecked(self.heap.1 * size_of::<T>(), align_of::<T>());
let old_layout = unsafe {
Layout::from_size_align_unchecked(self.heap.1 * size_of::<T>(), align_of::<T>())
};

// SAFETY: ptr was allocated with this allocator
// old_layout is the same as the layout used to allocate the
// previous memory block new_layout.size() is greater
// than zero does not overflow when rounded up to
// alignment. since it was constructed
// with Layout::array
let new_ptr = realloc(ptr as *mut u8, old_layout, new_layout.size()) as *mut T;
let new_ptr =
unsafe { realloc(ptr as *mut u8, old_layout, new_layout.size()) } as *mut T;
NonNull::new(new_ptr).ok_or(CollectionAllocErr::AllocErr {
layout: new_layout
})?
Expand Down
Loading