Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use NonNull instead of a raw pointer #171

Merged
merged 1 commit into from
Oct 28, 2019
Merged
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
36 changes: 19 additions & 17 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl<'a, T: 'a + Array> Drop for Drain<'a, T> {
#[cfg(feature = "union")]
union SmallVecData<A: Array> {
inline: MaybeUninit<A>,
heap: (*mut A::Item, usize),
heap: (NonNull<A::Item>, usize),
}

#[cfg(feature = "union")]
Expand All @@ -283,37 +283,39 @@ impl<A: Array> SmallVecData<A> {
}
#[inline]
unsafe fn heap(&self) -> (*mut A::Item, usize) {
self.heap
(self.heap.0.as_ptr(), self.heap.1)
}
#[inline]
unsafe fn heap_mut(&mut self) -> &mut (*mut A::Item, usize) {
&mut self.heap
unsafe fn heap_mut(&mut self) -> (*mut A::Item, &mut usize) {
(self.heap.0.as_ptr(), &mut self.heap.1)
}
#[inline]
fn from_heap(ptr: *mut A::Item, len: usize) -> SmallVecData<A> {
SmallVecData { heap: (ptr, len) }
SmallVecData {
heap: (NonNull::new(ptr).unwrap(), len),
}
}
}

#[cfg(not(feature = "union"))]
enum SmallVecData<A: Array> {
Inline(MaybeUninit<A>),
Heap((*mut A::Item, usize)),
Heap((NonNull<A::Item>, usize)),
}

#[cfg(not(feature = "union"))]
impl<A: Array> SmallVecData<A> {
#[inline]
unsafe fn inline(&self) -> *const A::Item {
match *self {
SmallVecData::Inline(ref a) => a.as_ptr() as *const A::Item,
match self {
SmallVecData::Inline(a) => a.as_ptr() as *const A::Item,
_ => debug_unreachable!(),
}
}
#[inline]
unsafe fn inline_mut(&mut self) -> *mut A::Item {
match *self {
SmallVecData::Inline(ref mut a) => a.as_mut_ptr() as *mut A::Item,
match self {
SmallVecData::Inline(a) => a.as_mut_ptr() as *mut A::Item,
_ => debug_unreachable!(),
}
}
Expand All @@ -330,21 +332,21 @@ impl<A: Array> SmallVecData<A> {
}
#[inline]
unsafe fn heap(&self) -> (*mut A::Item, usize) {
match *self {
SmallVecData::Heap(data) => data,
match self {
SmallVecData::Heap(data) => (data.0.as_ptr(), data.1),
_ => debug_unreachable!(),
}
}
#[inline]
unsafe fn heap_mut(&mut self) -> &mut (*mut A::Item, usize) {
match *self {
SmallVecData::Heap(ref mut data) => data,
unsafe fn heap_mut(&mut self) -> (*mut A::Item, &mut usize) {
match self {
SmallVecData::Heap(data) => (data.0.as_ptr(), &mut data.1),
_ => debug_unreachable!(),
}
}
#[inline]
fn from_heap(ptr: *mut A::Item, len: usize) -> SmallVecData<A> {
SmallVecData::Heap((ptr, len))
SmallVecData::Heap((NonNull::new(ptr).unwrap(), len))
}
}

Expand Down Expand Up @@ -571,7 +573,7 @@ impl<A: Array> SmallVec<A> {
fn triple_mut(&mut self) -> (*mut A::Item, &mut usize, usize) {
unsafe {
if self.spilled() {
let &mut (ptr, ref mut len_ptr) = self.data.heap_mut();
let (ptr, len_ptr) = self.data.heap_mut();
(ptr, len_ptr, self.capacity)
} else {
(self.data.inline_mut(), &mut self.capacity, A::size())
Expand Down