Skip to content

Commit 3fd2178

Browse files
c410-f3rmbrubeck
authored andcommitted
Make use of NonNull
1 parent 18f13df commit 3fd2178

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

lib.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use std::marker::PhantomData;
6666
use std::mem;
6767
use std::mem::MaybeUninit;
6868
use std::ops;
69-
use std::ptr;
69+
use std::ptr::{self, NonNull};
7070
use std::slice::{self, SliceIndex};
7171

7272
/// Creates a [`SmallVec`] containing the arguments.
@@ -223,7 +223,7 @@ impl<'a, T: 'a> Drop for Drain<'a, T> {
223223
#[cfg(feature = "union")]
224224
union SmallVecData<A: Array> {
225225
inline: MaybeUninit<A>,
226-
heap: (*mut A::Item, usize),
226+
heap: (NonNull<A::Item>, usize),
227227
}
228228

229229
#[cfg(feature = "union")]
@@ -246,37 +246,39 @@ impl<A: Array> SmallVecData<A> {
246246
}
247247
#[inline]
248248
unsafe fn heap(&self) -> (*mut A::Item, usize) {
249-
self.heap
249+
(self.heap.0.as_ptr(), self.heap.1)
250250
}
251251
#[inline]
252-
unsafe fn heap_mut(&mut self) -> &mut (*mut A::Item, usize) {
253-
&mut self.heap
252+
unsafe fn heap_mut(&mut self) -> (*mut A::Item, &mut usize) {
253+
(self.heap.0.as_ptr(), &mut self.heap.1)
254254
}
255255
#[inline]
256256
fn from_heap(ptr: *mut A::Item, len: usize) -> SmallVecData<A> {
257-
SmallVecData { heap: (ptr, len) }
257+
SmallVecData {
258+
heap: (NonNull::new(ptr).unwrap(), len),
259+
}
258260
}
259261
}
260262

261263
#[cfg(not(feature = "union"))]
262264
enum SmallVecData<A: Array> {
263265
Inline(MaybeUninit<A>),
264-
Heap((*mut A::Item, usize)),
266+
Heap((NonNull<A::Item>, usize)),
265267
}
266268

267269
#[cfg(not(feature = "union"))]
268270
impl<A: Array> SmallVecData<A> {
269271
#[inline]
270272
unsafe fn inline(&self) -> *const A::Item {
271-
match *self {
272-
SmallVecData::Inline(ref a) => a.as_ptr() as *const A::Item,
273+
match self {
274+
SmallVecData::Inline(a) => a.as_ptr() as *const A::Item,
273275
_ => debug_unreachable!(),
274276
}
275277
}
276278
#[inline]
277279
unsafe fn inline_mut(&mut self) -> *mut A::Item {
278-
match *self {
279-
SmallVecData::Inline(ref mut a) => a.as_mut_ptr() as *mut A::Item,
280+
match self {
281+
SmallVecData::Inline(a) => a.as_mut_ptr() as *mut A::Item,
280282
_ => debug_unreachable!(),
281283
}
282284
}
@@ -293,21 +295,21 @@ impl<A: Array> SmallVecData<A> {
293295
}
294296
#[inline]
295297
unsafe fn heap(&self) -> (*mut A::Item, usize) {
296-
match *self {
297-
SmallVecData::Heap(data) => data,
298+
match self {
299+
SmallVecData::Heap(data) => (data.0.as_ptr(), data.1),
298300
_ => debug_unreachable!(),
299301
}
300302
}
301303
#[inline]
302-
unsafe fn heap_mut(&mut self) -> &mut (*mut A::Item, usize) {
303-
match *self {
304-
SmallVecData::Heap(ref mut data) => data,
304+
unsafe fn heap_mut(&mut self) -> (*mut A::Item, &mut usize) {
305+
match self {
306+
SmallVecData::Heap(data) => (data.0.as_ptr(), &mut data.1),
305307
_ => debug_unreachable!(),
306308
}
307309
}
308310
#[inline]
309311
fn from_heap(ptr: *mut A::Item, len: usize) -> SmallVecData<A> {
310-
SmallVecData::Heap((ptr, len))
312+
SmallVecData::Heap((NonNull::new(ptr).unwrap(), len))
311313
}
312314
}
313315

@@ -534,7 +536,7 @@ impl<A: Array> SmallVec<A> {
534536
fn triple_mut(&mut self) -> (*mut A::Item, &mut usize, usize) {
535537
unsafe {
536538
if self.spilled() {
537-
let &mut (ptr, ref mut len_ptr) = self.data.heap_mut();
539+
let (ptr, len_ptr) = self.data.heap_mut();
538540
(ptr, len_ptr, self.capacity)
539541
} else {
540542
(self.data.inline_mut(), &mut self.capacity, A::size())

0 commit comments

Comments
 (0)