From 1a8a550c145f7a6eb3501397cbd6534933cf09df Mon Sep 17 00:00:00 2001 From: Caio Date: Sat, 15 Jun 2019 20:53:33 -0300 Subject: [PATCH] Make use of NonNull --- lib.rs | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/lib.rs b/lib.rs index c9cddf4..20ec4f3 100644 --- a/lib.rs +++ b/lib.rs @@ -260,7 +260,7 @@ impl<'a, T: 'a + Array> Drop for Drain<'a, T> { #[cfg(feature = "union")] union SmallVecData { inline: MaybeUninit, - heap: (*mut A::Item, usize), + heap: (NonNull, usize), } #[cfg(feature = "union")] @@ -283,37 +283,39 @@ impl SmallVecData { } #[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 { - SmallVecData { heap: (ptr, len) } + SmallVecData { + heap: (NonNull::new(ptr).unwrap(), len), + } } } #[cfg(not(feature = "union"))] enum SmallVecData { Inline(MaybeUninit), - Heap((*mut A::Item, usize)), + Heap((NonNull, usize)), } #[cfg(not(feature = "union"))] impl SmallVecData { #[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!(), } } @@ -330,21 +332,21 @@ impl SmallVecData { } #[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 { - SmallVecData::Heap((ptr, len)) + SmallVecData::Heap((NonNull::new(ptr).unwrap(), len)) } } @@ -571,7 +573,7 @@ impl SmallVec { 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())