Skip to content

Commit 0afb664

Browse files
author
bors-servo
authored
Auto merge of #176 - mbrubeck:cleanup, r=emilio
Some code cleanups Apply some clippy suggestions, and revert a failed optimization.
2 parents 34c2628 + 0e8f5e9 commit 0afb664

File tree

1 file changed

+42
-48
lines changed

1 file changed

+42
-48
lines changed

lib.rs

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ impl<'a, T: 'a + Array> Drop for Drain<'a, T> {
256256
#[cfg(feature = "union")]
257257
union SmallVecData<A: Array> {
258258
inline: MaybeUninit<A>,
259-
heap: (NonNull<A::Item>, usize),
259+
heap: (*mut A::Item, usize),
260260
}
261261

262262
#[cfg(feature = "union")]
@@ -279,24 +279,22 @@ impl<A: Array> SmallVecData<A> {
279279
}
280280
#[inline]
281281
unsafe fn heap(&self) -> (*mut A::Item, usize) {
282-
(self.heap.0.as_ptr(), self.heap.1)
282+
self.heap
283283
}
284284
#[inline]
285-
unsafe fn heap_mut(&mut self) -> (*mut A::Item, &mut usize) {
286-
(self.heap.0.as_ptr(), &mut self.heap.1)
285+
unsafe fn heap_mut(&mut self) -> &mut (*mut A::Item, usize) {
286+
&mut self.heap
287287
}
288288
#[inline]
289289
fn from_heap(ptr: *mut A::Item, len: usize) -> SmallVecData<A> {
290-
SmallVecData {
291-
heap: (NonNull::new(ptr).unwrap(), len),
292-
}
290+
SmallVecData { heap: (ptr, len) }
293291
}
294292
}
295293

296294
#[cfg(not(feature = "union"))]
297295
enum SmallVecData<A: Array> {
298296
Inline(MaybeUninit<A>),
299-
Heap((NonNull<A::Item>, usize)),
297+
Heap((*mut A::Item, usize)),
300298
}
301299

302300
#[cfg(not(feature = "union"))]
@@ -329,20 +327,20 @@ impl<A: Array> SmallVecData<A> {
329327
#[inline]
330328
unsafe fn heap(&self) -> (*mut A::Item, usize) {
331329
match self {
332-
SmallVecData::Heap(data) => (data.0.as_ptr(), data.1),
330+
SmallVecData::Heap(data) => *data,
333331
_ => debug_unreachable!(),
334332
}
335333
}
336334
#[inline]
337-
unsafe fn heap_mut(&mut self) -> (*mut A::Item, &mut usize) {
335+
unsafe fn heap_mut(&mut self) -> &mut (*mut A::Item, usize) {
338336
match self {
339-
SmallVecData::Heap(data) => (data.0.as_ptr(), &mut data.1),
337+
SmallVecData::Heap(data) => data,
340338
_ => debug_unreachable!(),
341339
}
342340
}
343341
#[inline]
344342
fn from_heap(ptr: *mut A::Item, len: usize) -> SmallVecData<A> {
345-
SmallVecData::Heap((NonNull::new(ptr).unwrap(), len))
343+
SmallVecData::Heap((ptr, len))
346344
}
347345
}
348346

@@ -569,7 +567,7 @@ impl<A: Array> SmallVec<A> {
569567
fn triple_mut(&mut self) -> (*mut A::Item, &mut usize, usize) {
570568
unsafe {
571569
if self.spilled() {
572-
let (ptr, len_ptr) = self.data.heap_mut();
570+
let &mut (ptr, ref mut len_ptr) = self.data.heap_mut();
573571
(ptr, len_ptr, self.capacity)
574572
} else {
575573
(self.data.inline_mut(), &mut self.capacity, A::size())
@@ -641,7 +639,7 @@ impl<A: Array> SmallVec<A> {
641639
}
642640
let (ptr, len_ptr, _) = self.triple_mut();
643641
*len_ptr = len + 1;
644-
ptr::write(ptr.offset(len as isize), value);
642+
ptr::write(ptr.add(len), value);
645643
}
646644
}
647645

@@ -655,7 +653,7 @@ impl<A: Array> SmallVec<A> {
655653
}
656654
let last_index = *len_ptr - 1;
657655
*len_ptr = last_index;
658-
Some(ptr::read(ptr.offset(last_index as isize)))
656+
Some(ptr::read(ptr.add(last_index)))
659657
}
660658
}
661659

@@ -761,7 +759,7 @@ impl<A: Array> SmallVec<A> {
761759
while len < *len_ptr {
762760
let last_index = *len_ptr - 1;
763761
*len_ptr = last_index;
764-
ptr::drop_in_place(ptr.offset(last_index as isize));
762+
ptr::drop_in_place(ptr.add(last_index));
765763
}
766764
}
767765
}
@@ -809,9 +807,9 @@ impl<A: Array> SmallVec<A> {
809807
let len = *len_ptr;
810808
assert!(index < len);
811809
*len_ptr = len - 1;
812-
ptr = ptr.offset(index as isize);
810+
ptr = ptr.add(index);
813811
let item = ptr::read(ptr);
814-
ptr::copy(ptr.offset(1), ptr, len - index - 1);
812+
ptr::copy(ptr.add(1), ptr, len - index - 1);
815813
item
816814
}
817815
}
@@ -827,8 +825,8 @@ impl<A: Array> SmallVec<A> {
827825
let len = *len_ptr;
828826
assert!(index <= len);
829827
*len_ptr = len + 1;
830-
ptr = ptr.offset(index as isize);
831-
ptr::copy(ptr, ptr.offset(1), len - index);
828+
ptr = ptr.add(index);
829+
ptr::copy(ptr, ptr.add(1), len - index);
832830
ptr::write(ptr, element);
833831
}
834832
}
@@ -849,32 +847,32 @@ impl<A: Array> SmallVec<A> {
849847
unsafe {
850848
let old_len = self.len();
851849
assert!(index <= old_len);
852-
let mut ptr = self.as_mut_ptr().offset(index as isize);
850+
let mut ptr = self.as_mut_ptr().add(index);
853851

854852
// Move the trailing elements.
855-
ptr::copy(ptr, ptr.offset(lower_size_bound as isize), old_len - index);
853+
ptr::copy(ptr, ptr.add(lower_size_bound), old_len - index);
856854

857855
// In case the iterator panics, don't double-drop the items we just copied above.
858856
self.set_len(index);
859857

860858
let mut num_added = 0;
861859
for element in iter {
862-
let mut cur = ptr.offset(num_added as isize);
860+
let mut cur = ptr.add(num_added);
863861
if num_added >= lower_size_bound {
864862
// Iterator provided more elements than the hint. Move trailing items again.
865863
self.reserve(1);
866-
ptr = self.as_mut_ptr().offset(index as isize);
867-
cur = ptr.offset(num_added as isize);
868-
ptr::copy(cur, cur.offset(1), old_len - index);
864+
ptr = self.as_mut_ptr().add(index);
865+
cur = ptr.add(num_added);
866+
ptr::copy(cur, cur.add(1), old_len - index);
869867
}
870868
ptr::write(cur, element);
871869
num_added += 1;
872870
}
873871
if num_added < lower_size_bound {
874872
// Iterator provided fewer elements than the hint
875873
ptr::copy(
876-
ptr.offset(lower_size_bound as isize),
877-
ptr.offset(num_added as isize),
874+
ptr.add(lower_size_bound),
875+
ptr.add(num_added),
878876
old_len - index,
879877
);
880878
}
@@ -957,11 +955,11 @@ impl<A: Array> SmallVec<A> {
957955

958956
unsafe {
959957
for r in 1..len {
960-
let p_r = ptr.offset(r as isize);
961-
let p_wm1 = ptr.offset((w - 1) as isize);
958+
let p_r = ptr.add(r);
959+
let p_wm1 = ptr.add(w - 1);
962960
if !same_bucket(&mut *p_r, &mut *p_wm1) {
963961
if r != w {
964-
let p_w = p_wm1.offset(1);
962+
let p_w = p_wm1.add(1);
965963
mem::swap(&mut *p_r, &mut *p_w);
966964
}
967965
w += 1;
@@ -1039,8 +1037,8 @@ impl<A: Array> SmallVec<A> {
10391037
/// // writing into the old `SmallVec`'s inline storage on the
10401038
/// // stack.
10411039
/// assert!(spilled);
1042-
/// for i in 0..len as isize {
1043-
/// ptr::write(p.offset(i), 4 + i);
1040+
/// for i in 0..len {
1041+
/// ptr::write(p.add(i), 4 + i);
10441042
/// }
10451043
///
10461044
/// // Put everything back together into a SmallVec with a different
@@ -1103,8 +1101,8 @@ where
11031101

11041102
unsafe {
11051103
let slice_ptr = slice.as_ptr();
1106-
let ptr = self.as_mut_ptr().offset(index as isize);
1107-
ptr::copy(ptr, ptr.offset(slice.len() as isize), len - index);
1104+
let ptr = self.as_mut_ptr().add(index);
1105+
ptr::copy(ptr, ptr.add(slice.len()), len - index);
11081106
ptr::copy_nonoverlapping(slice_ptr, ptr, slice.len());
11091107
self.set_len(len + slice.len());
11101108
}
@@ -1156,8 +1154,8 @@ where
11561154
let (ptr, len_ptr, _) = v.triple_mut();
11571155
let mut local_len = SetLenOnDrop::new(len_ptr);
11581156

1159-
for i in 0..n as isize {
1160-
::core::ptr::write(ptr.offset(i), elem.clone());
1157+
for i in 0..n {
1158+
::core::ptr::write(ptr.add(i), elem.clone());
11611159
local_len.increment_len(1);
11621160
}
11631161
}
@@ -1318,7 +1316,7 @@ where
13181316
#[cfg(not(feature = "specialization"))]
13191317
#[inline]
13201318
fn from(slice: &'a [A::Item]) -> SmallVec<A> {
1321-
slice.into_iter().cloned().collect()
1319+
slice.iter().cloned().collect()
13221320
}
13231321

13241322
#[cfg(feature = "specialization")]
@@ -1384,7 +1382,7 @@ impl<A: Array> Extend<A::Item> for SmallVec<A> {
13841382
let mut len = SetLenOnDrop::new(len_ptr);
13851383
while len.get() < cap {
13861384
if let Some(out) = iter.next() {
1387-
ptr::write(ptr.offset(len.get() as isize), out);
1385+
ptr::write(ptr.add(len.get()), out);
13881386
len.increment_len(1);
13891387
} else {
13901388
return;
@@ -1463,10 +1461,6 @@ where
14631461
fn eq(&self, other: &SmallVec<B>) -> bool {
14641462
self[..] == other[..]
14651463
}
1466-
#[inline]
1467-
fn ne(&self, other: &SmallVec<B>) -> bool {
1468-
self[..] != other[..]
1469-
}
14701464
}
14711465

14721466
impl<A: Array> Eq for SmallVec<A> where A::Item: Eq {}
@@ -1528,9 +1522,9 @@ impl<A: Array> Iterator for IntoIter<A> {
15281522
None
15291523
} else {
15301524
unsafe {
1531-
let current = self.current as isize;
1525+
let current = self.current;
15321526
self.current += 1;
1533-
Some(ptr::read(self.data.as_ptr().offset(current)))
1527+
Some(ptr::read(self.data.as_ptr().add(current)))
15341528
}
15351529
}
15361530
}
@@ -1550,7 +1544,7 @@ impl<A: Array> DoubleEndedIterator for IntoIter<A> {
15501544
} else {
15511545
unsafe {
15521546
self.end -= 1;
1553-
Some(ptr::read(self.data.as_ptr().offset(self.end as isize)))
1547+
Some(ptr::read(self.data.as_ptr().add(self.end)))
15541548
}
15551549
}
15561550
}
@@ -1613,7 +1607,7 @@ impl<'a> SetLenOnDrop<'a> {
16131607
fn new(len: &'a mut usize) -> Self {
16141608
SetLenOnDrop {
16151609
local_len: *len,
1616-
len: len,
1610+
len,
16171611
}
16181612
}
16191613

@@ -1649,7 +1643,7 @@ macro_rules! impl_array(
16491643
impl_array!(
16501644
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 24, 32, 36, 0x40, 0x60, 0x80,
16511645
0x100, 0x200, 0x400, 0x600, 0x800, 0x1000, 0x2000, 0x4000, 0x6000, 0x8000, 0x10000, 0x20000,
1652-
0x40000, 0x60000, 0x80000, 0x100000
1646+
0x40000, 0x60000, 0x80000, 0x10_0000
16531647
);
16541648

16551649
#[cfg(test)]

0 commit comments

Comments
 (0)