Skip to content

Commit 39f3756

Browse files
committed
Auto merge of #53329 - frewsxcv:frewsxcv-ptr-add-sub, r=RalfJung
Replace usages of ptr::offset with ptr::{add,sub}. Rust provides these helper methods – so let's use them!
2 parents 1558ae7 + 993fb93 commit 39f3756

File tree

29 files changed

+148
-148
lines changed

29 files changed

+148
-148
lines changed

src/liballoc/alloc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ mod tests {
245245
.unwrap_or_else(|_| handle_alloc_error(layout));
246246

247247
let mut i = ptr.cast::<u8>().as_ptr();
248-
let end = i.offset(layout.size() as isize);
248+
let end = i.add(layout.size());
249249
while i < end {
250250
assert_eq!(*i, 0);
251251
i = i.offset(1);

src/liballoc/boxed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ impl<T: Clone> Clone for Box<[T]> {
704704
impl<T> Drop for BoxBuilder<T> {
705705
fn drop(&mut self) {
706706
let mut data = self.data.ptr();
707-
let max = unsafe { data.offset(self.len as isize) };
707+
let max = unsafe { data.add(self.len) };
708708

709709
while data != max {
710710
unsafe {

src/liballoc/collections/btree/node.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -1151,12 +1151,12 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::KV>
11511151
let new_len = self.node.len() - self.idx - 1;
11521152

11531153
ptr::copy_nonoverlapping(
1154-
self.node.keys().as_ptr().offset(self.idx as isize + 1),
1154+
self.node.keys().as_ptr().add(self.idx + 1),
11551155
new_node.keys.as_mut_ptr(),
11561156
new_len
11571157
);
11581158
ptr::copy_nonoverlapping(
1159-
self.node.vals().as_ptr().offset(self.idx as isize + 1),
1159+
self.node.vals().as_ptr().add(self.idx + 1),
11601160
new_node.vals.as_mut_ptr(),
11611161
new_len
11621162
);
@@ -1209,17 +1209,17 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
12091209
let new_len = self.node.len() - self.idx - 1;
12101210

12111211
ptr::copy_nonoverlapping(
1212-
self.node.keys().as_ptr().offset(self.idx as isize + 1),
1212+
self.node.keys().as_ptr().add(self.idx + 1),
12131213
new_node.data.keys.as_mut_ptr(),
12141214
new_len
12151215
);
12161216
ptr::copy_nonoverlapping(
1217-
self.node.vals().as_ptr().offset(self.idx as isize + 1),
1217+
self.node.vals().as_ptr().add(self.idx + 1),
12181218
new_node.data.vals.as_mut_ptr(),
12191219
new_len
12201220
);
12211221
ptr::copy_nonoverlapping(
1222-
self.node.as_internal().edges.as_ptr().offset(self.idx as isize + 1),
1222+
self.node.as_internal().edges.as_ptr().add(self.idx + 1),
12231223
new_node.edges.as_mut_ptr(),
12241224
new_len + 1
12251225
);
@@ -1283,14 +1283,14 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
12831283
slice_remove(self.node.keys_mut(), self.idx));
12841284
ptr::copy_nonoverlapping(
12851285
right_node.keys().as_ptr(),
1286-
left_node.keys_mut().as_mut_ptr().offset(left_len as isize + 1),
1286+
left_node.keys_mut().as_mut_ptr().add(left_len + 1),
12871287
right_len
12881288
);
12891289
ptr::write(left_node.vals_mut().get_unchecked_mut(left_len),
12901290
slice_remove(self.node.vals_mut(), self.idx));
12911291
ptr::copy_nonoverlapping(
12921292
right_node.vals().as_ptr(),
1293-
left_node.vals_mut().as_mut_ptr().offset(left_len as isize + 1),
1293+
left_node.vals_mut().as_mut_ptr().add(left_len + 1),
12941294
right_len
12951295
);
12961296

@@ -1309,7 +1309,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
13091309
.as_internal_mut()
13101310
.edges
13111311
.as_mut_ptr()
1312-
.offset(left_len as isize + 1),
1312+
.add(left_len + 1),
13131313
right_len + 1
13141314
);
13151315

@@ -1394,10 +1394,10 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
13941394

13951395
// Make room for stolen elements in the right child.
13961396
ptr::copy(right_kv.0,
1397-
right_kv.0.offset(count as isize),
1397+
right_kv.0.add(count),
13981398
right_len);
13991399
ptr::copy(right_kv.1,
1400-
right_kv.1.offset(count as isize),
1400+
right_kv.1.add(count),
14011401
right_len);
14021402

14031403
// Move elements from the left child to the right one.
@@ -1418,7 +1418,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
14181418
// Make room for stolen edges.
14191419
let right_edges = right.reborrow_mut().as_internal_mut().edges.as_mut_ptr();
14201420
ptr::copy(right_edges,
1421-
right_edges.offset(count as isize),
1421+
right_edges.add(count),
14221422
right_len + 1);
14231423
right.correct_childrens_parent_links(count, count + right_len + 1);
14241424

@@ -1463,10 +1463,10 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
14631463
move_kv(right_kv, count - 1, parent_kv, 0, 1);
14641464

14651465
// Fix right indexing
1466-
ptr::copy(right_kv.0.offset(count as isize),
1466+
ptr::copy(right_kv.0.add(count),
14671467
right_kv.0,
14681468
new_right_len);
1469-
ptr::copy(right_kv.1.offset(count as isize),
1469+
ptr::copy(right_kv.1.add(count),
14701470
right_kv.1,
14711471
new_right_len);
14721472
}
@@ -1480,7 +1480,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
14801480

14811481
// Fix right indexing.
14821482
let right_edges = right.reborrow_mut().as_internal_mut().edges.as_mut_ptr();
1483-
ptr::copy(right_edges.offset(count as isize),
1483+
ptr::copy(right_edges.add(count),
14841484
right_edges,
14851485
new_right_len + 1);
14861486
right.correct_childrens_parent_links(0, new_right_len + 1);
@@ -1497,11 +1497,11 @@ unsafe fn move_kv<K, V>(
14971497
dest: (*mut K, *mut V), dest_offset: usize,
14981498
count: usize)
14991499
{
1500-
ptr::copy_nonoverlapping(source.0.offset(source_offset as isize),
1501-
dest.0.offset(dest_offset as isize),
1500+
ptr::copy_nonoverlapping(source.0.add(source_offset),
1501+
dest.0.add(dest_offset),
15021502
count);
1503-
ptr::copy_nonoverlapping(source.1.offset(source_offset as isize),
1504-
dest.1.offset(dest_offset as isize),
1503+
ptr::copy_nonoverlapping(source.1.add(source_offset),
1504+
dest.1.add(dest_offset),
15051505
count);
15061506
}
15071507

@@ -1513,8 +1513,8 @@ unsafe fn move_edges<K, V>(
15131513
{
15141514
let source_ptr = source.as_internal_mut().edges.as_mut_ptr();
15151515
let dest_ptr = dest.as_internal_mut().edges.as_mut_ptr();
1516-
ptr::copy_nonoverlapping(source_ptr.offset(source_offset as isize),
1517-
dest_ptr.offset(dest_offset as isize),
1516+
ptr::copy_nonoverlapping(source_ptr.add(source_offset),
1517+
dest_ptr.add(dest_offset),
15181518
count);
15191519
dest.correct_childrens_parent_links(dest_offset, dest_offset + count);
15201520
}
@@ -1604,8 +1604,8 @@ pub mod marker {
16041604

16051605
unsafe fn slice_insert<T>(slice: &mut [T], idx: usize, val: T) {
16061606
ptr::copy(
1607-
slice.as_ptr().offset(idx as isize),
1608-
slice.as_mut_ptr().offset(idx as isize + 1),
1607+
slice.as_ptr().add(idx),
1608+
slice.as_mut_ptr().add(idx + 1),
16091609
slice.len() - idx
16101610
);
16111611
ptr::write(slice.get_unchecked_mut(idx), val);
@@ -1614,8 +1614,8 @@ unsafe fn slice_insert<T>(slice: &mut [T], idx: usize, val: T) {
16141614
unsafe fn slice_remove<T>(slice: &mut [T], idx: usize) -> T {
16151615
let ret = ptr::read(slice.get_unchecked(idx));
16161616
ptr::copy(
1617-
slice.as_ptr().offset(idx as isize + 1),
1618-
slice.as_mut_ptr().offset(idx as isize),
1617+
slice.as_ptr().add(idx + 1),
1618+
slice.as_mut_ptr().add(idx),
16191619
slice.len() - idx - 1
16201620
);
16211621
ret

src/liballoc/collections/vec_deque.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ impl<T> VecDeque<T> {
126126
/// Moves an element out of the buffer
127127
#[inline]
128128
unsafe fn buffer_read(&mut self, off: usize) -> T {
129-
ptr::read(self.ptr().offset(off as isize))
129+
ptr::read(self.ptr().add(off))
130130
}
131131

132132
/// Writes an element into the buffer, moving it.
133133
#[inline]
134134
unsafe fn buffer_write(&mut self, off: usize, value: T) {
135-
ptr::write(self.ptr().offset(off as isize), value);
135+
ptr::write(self.ptr().add(off), value);
136136
}
137137

138138
/// Returns `true` if and only if the buffer is at full capacity.
@@ -177,8 +177,8 @@ impl<T> VecDeque<T> {
177177
src,
178178
len,
179179
self.cap());
180-
ptr::copy(self.ptr().offset(src as isize),
181-
self.ptr().offset(dst as isize),
180+
ptr::copy(self.ptr().add(src),
181+
self.ptr().add(dst),
182182
len);
183183
}
184184

@@ -197,8 +197,8 @@ impl<T> VecDeque<T> {
197197
src,
198198
len,
199199
self.cap());
200-
ptr::copy_nonoverlapping(self.ptr().offset(src as isize),
201-
self.ptr().offset(dst as isize),
200+
ptr::copy_nonoverlapping(self.ptr().add(src),
201+
self.ptr().add(dst),
202202
len);
203203
}
204204

@@ -436,7 +436,7 @@ impl<T> VecDeque<T> {
436436
pub fn get(&self, index: usize) -> Option<&T> {
437437
if index < self.len() {
438438
let idx = self.wrap_add(self.tail, index);
439-
unsafe { Some(&*self.ptr().offset(idx as isize)) }
439+
unsafe { Some(&*self.ptr().add(idx)) }
440440
} else {
441441
None
442442
}
@@ -465,7 +465,7 @@ impl<T> VecDeque<T> {
465465
pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
466466
if index < self.len() {
467467
let idx = self.wrap_add(self.tail, index);
468-
unsafe { Some(&mut *self.ptr().offset(idx as isize)) }
468+
unsafe { Some(&mut *self.ptr().add(idx)) }
469469
} else {
470470
None
471471
}
@@ -501,8 +501,8 @@ impl<T> VecDeque<T> {
501501
let ri = self.wrap_add(self.tail, i);
502502
let rj = self.wrap_add(self.tail, j);
503503
unsafe {
504-
ptr::swap(self.ptr().offset(ri as isize),
505-
self.ptr().offset(rj as isize))
504+
ptr::swap(self.ptr().add(ri),
505+
self.ptr().add(rj))
506506
}
507507
}
508508

@@ -1805,20 +1805,20 @@ impl<T> VecDeque<T> {
18051805
// `at` lies in the first half.
18061806
let amount_in_first = first_len - at;
18071807

1808-
ptr::copy_nonoverlapping(first_half.as_ptr().offset(at as isize),
1808+
ptr::copy_nonoverlapping(first_half.as_ptr().add(at),
18091809
other.ptr(),
18101810
amount_in_first);
18111811

18121812
// just take all of the second half.
18131813
ptr::copy_nonoverlapping(second_half.as_ptr(),
1814-
other.ptr().offset(amount_in_first as isize),
1814+
other.ptr().add(amount_in_first),
18151815
second_len);
18161816
} else {
18171817
// `at` lies in the second half, need to factor in the elements we skipped
18181818
// in the first half.
18191819
let offset = at - first_len;
18201820
let amount_in_second = second_len - offset;
1821-
ptr::copy_nonoverlapping(second_half.as_ptr().offset(offset as isize),
1821+
ptr::copy_nonoverlapping(second_half.as_ptr().add(offset),
18221822
other.ptr(),
18231823
amount_in_second);
18241824
}
@@ -2709,24 +2709,24 @@ impl<T> From<VecDeque<T>> for Vec<T> {
27092709

27102710
// Need to move the ring to the front of the buffer, as vec will expect this.
27112711
if other.is_contiguous() {
2712-
ptr::copy(buf.offset(tail as isize), buf, len);
2712+
ptr::copy(buf.add(tail), buf, len);
27132713
} else {
27142714
if (tail - head) >= cmp::min(cap - tail, head) {
27152715
// There is enough free space in the centre for the shortest block so we can
27162716
// do this in at most three copy moves.
27172717
if (cap - tail) > head {
27182718
// right hand block is the long one; move that enough for the left
2719-
ptr::copy(buf.offset(tail as isize),
2720-
buf.offset((tail - head) as isize),
2719+
ptr::copy(buf.add(tail),
2720+
buf.add(tail - head),
27212721
cap - tail);
27222722
// copy left in the end
2723-
ptr::copy(buf, buf.offset((cap - head) as isize), head);
2723+
ptr::copy(buf, buf.add(cap - head), head);
27242724
// shift the new thing to the start
2725-
ptr::copy(buf.offset((tail - head) as isize), buf, len);
2725+
ptr::copy(buf.add(tail - head), buf, len);
27262726
} else {
27272727
// left hand block is the long one, we can do it in two!
2728-
ptr::copy(buf, buf.offset((cap - tail) as isize), head);
2729-
ptr::copy(buf.offset(tail as isize), buf, cap - tail);
2728+
ptr::copy(buf, buf.add(cap - tail), head);
2729+
ptr::copy(buf.add(tail), buf, cap - tail);
27302730
}
27312731
} else {
27322732
// Need to use N swaps to move the ring
@@ -2751,7 +2751,7 @@ impl<T> From<VecDeque<T>> for Vec<T> {
27512751
for i in left_edge..right_edge {
27522752
right_offset = (i - left_edge) % (cap - right_edge);
27532753
let src: isize = (right_edge + right_offset) as isize;
2754-
ptr::swap(buf.offset(i as isize), buf.offset(src));
2754+
ptr::swap(buf.add(i), buf.offset(src));
27552755
}
27562756
let n_ops = right_edge - left_edge;
27572757
left_edge += n_ops;

src/liballoc/raw_vec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ impl<T, A: Alloc> RawVec<T, A> {
282282
/// // double would have aborted or panicked if the len exceeded
283283
/// // `isize::MAX` so this is safe to do unchecked now.
284284
/// unsafe {
285-
/// ptr::write(self.buf.ptr().offset(self.len as isize), elem);
285+
/// ptr::write(self.buf.ptr().add(self.len), elem);
286286
/// }
287287
/// self.len += 1;
288288
/// }
@@ -487,7 +487,7 @@ impl<T, A: Alloc> RawVec<T, A> {
487487
/// // `isize::MAX` so this is safe to do unchecked now.
488488
/// for x in elems {
489489
/// unsafe {
490-
/// ptr::write(self.buf.ptr().offset(self.len as isize), x.clone());
490+
/// ptr::write(self.buf.ptr().add(self.len), x.clone());
491491
/// }
492492
/// self.len += 1;
493493
/// }

src/liballoc/rc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ impl<T: Clone> RcFromSlice<T> for Rc<[T]> {
771771
};
772772

773773
for (i, item) in v.iter().enumerate() {
774-
ptr::write(elems.offset(i as isize), item.clone());
774+
ptr::write(elems.add(i), item.clone());
775775
guard.n_elems += 1;
776776
}
777777

src/liballoc/slice.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -715,8 +715,8 @@ unsafe fn merge<T, F>(v: &mut [T], mid: usize, buf: *mut T, is_less: &mut F)
715715
{
716716
let len = v.len();
717717
let v = v.as_mut_ptr();
718-
let v_mid = v.offset(mid as isize);
719-
let v_end = v.offset(len as isize);
718+
let v_mid = v.add(mid);
719+
let v_end = v.add(len);
720720

721721
// The merge process first copies the shorter run into `buf`. Then it traces the newly copied
722722
// run and the longer run forwards (or backwards), comparing their next unconsumed elements and
@@ -742,7 +742,7 @@ unsafe fn merge<T, F>(v: &mut [T], mid: usize, buf: *mut T, is_less: &mut F)
742742
ptr::copy_nonoverlapping(v, buf, mid);
743743
hole = MergeHole {
744744
start: buf,
745-
end: buf.offset(mid as isize),
745+
end: buf.add(mid),
746746
dest: v,
747747
};
748748

@@ -766,7 +766,7 @@ unsafe fn merge<T, F>(v: &mut [T], mid: usize, buf: *mut T, is_less: &mut F)
766766
ptr::copy_nonoverlapping(v_mid, buf, len - mid);
767767
hole = MergeHole {
768768
start: buf,
769-
end: buf.offset((len - mid) as isize),
769+
end: buf.add(len - mid),
770770
dest: v_mid,
771771
};
772772

src/liballoc/string.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1190,8 +1190,8 @@ impl String {
11901190
let next = idx + ch.len_utf8();
11911191
let len = self.len();
11921192
unsafe {
1193-
ptr::copy(self.vec.as_ptr().offset(next as isize),
1194-
self.vec.as_mut_ptr().offset(idx as isize),
1193+
ptr::copy(self.vec.as_ptr().add(next),
1194+
self.vec.as_mut_ptr().add(idx),
11951195
len - next);
11961196
self.vec.set_len(len - (next - idx));
11971197
}
@@ -1232,8 +1232,8 @@ impl String {
12321232
del_bytes += ch_len;
12331233
} else if del_bytes > 0 {
12341234
unsafe {
1235-
ptr::copy(self.vec.as_ptr().offset(idx as isize),
1236-
self.vec.as_mut_ptr().offset((idx - del_bytes) as isize),
1235+
ptr::copy(self.vec.as_ptr().add(idx),
1236+
self.vec.as_mut_ptr().add(idx - del_bytes),
12371237
ch_len);
12381238
}
12391239
}
@@ -1289,11 +1289,11 @@ impl String {
12891289
let amt = bytes.len();
12901290
self.vec.reserve(amt);
12911291

1292-
ptr::copy(self.vec.as_ptr().offset(idx as isize),
1293-
self.vec.as_mut_ptr().offset((idx + amt) as isize),
1292+
ptr::copy(self.vec.as_ptr().add(idx),
1293+
self.vec.as_mut_ptr().add(idx + amt),
12941294
len - idx);
12951295
ptr::copy(bytes.as_ptr(),
1296-
self.vec.as_mut_ptr().offset(idx as isize),
1296+
self.vec.as_mut_ptr().add(idx),
12971297
amt);
12981298
self.vec.set_len(len + amt);
12991299
}

0 commit comments

Comments
 (0)