diff options
| author | bors <bors@rust-lang.org> | 2022-05-12 02:49:00 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-05-12 02:49:00 +0000 |
| commit | 1d2ea98cff1fde1d8b9e83a0eb639b8ec2cb82d8 (patch) | |
| tree | 5a6932062880a7b698c794abb827ac1f39192948 /library/alloc | |
| parent | 0cd939e36c0696aad44a213566c9b152f0437020 (diff) | |
| parent | 003b954a43a7f1f9058f25e8f9b6ddfd4a3dced9 (diff) | |
| download | rust-1d2ea98cff1fde1d8b9e83a0eb639b8ec2cb82d8.tar.gz rust-1d2ea98cff1fde1d8b9e83a0eb639b8ec2cb82d8.zip | |
Auto merge of #95837 - scottmcm:ptr-offset-from-unsigned, r=oli-obk
Add `sub_ptr` on pointers (the `usize` version of `offset_from`) We have `add`/`sub` which are the `usize` versions of `offset`, this adds the `usize` equivalent of `offset_from`. Like how `.add(d)` replaced a whole bunch of `.offset(d as isize)`, you can see from the changes here that it's fairly common that code actually knows the order between the pointers and *wants* a `usize`, not an `isize`. As a bonus, this can do `sub nuw`+`udiv exact`, rather than `sub`+`sdiv exact`, which can be optimized slightly better because it doesn't have to worry about negatives. That's why the slice iterators weren't using `offset_from`, though I haven't updated that code in this PR because slices are so perf-critical that I'll do it as its own change. This is an intrinsic, like `offset_from`, so that it can eventually be allowed in CTFE. It also allows checking the extra safety condition -- see the test confirming that CTFE catches it if you pass the pointers in the wrong order.
Diffstat (limited to 'library/alloc')
| -rw-r--r-- | library/alloc/src/lib.rs | 1 | ||||
| -rw-r--r-- | library/alloc/src/slice.rs | 2 | ||||
| -rw-r--r-- | library/alloc/src/vec/drain.rs | 2 | ||||
| -rw-r--r-- | library/alloc/src/vec/in_place_collect.rs | 2 | ||||
| -rw-r--r-- | library/alloc/src/vec/in_place_drop.rs | 2 | ||||
| -rw-r--r-- | library/alloc/src/vec/into_iter.rs | 2 |
6 files changed, 6 insertions, 5 deletions
diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index ecebc7ed9ac..fd21b367118 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -127,6 +127,7 @@ #![feature(pattern)] #![feature(ptr_internals)] #![feature(ptr_metadata)] +#![feature(ptr_sub_ptr)] #![feature(receiver_trait)] #![feature(set_ptr_value)] #![feature(slice_group_by)] diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs index 5bdf36a63a2..199b3c9d029 100644 --- a/library/alloc/src/slice.rs +++ b/library/alloc/src/slice.rs @@ -1056,7 +1056,7 @@ where fn drop(&mut self) { // `T` is not a zero-sized type, and these are pointers into a slice's elements. unsafe { - let len = self.end.offset_from(self.start) as usize; + let len = self.end.sub_ptr(self.start); ptr::copy_nonoverlapping(self.start, self.dest, len); } } diff --git a/library/alloc/src/vec/drain.rs b/library/alloc/src/vec/drain.rs index 1bff19d05c1..5cdee0bd4da 100644 --- a/library/alloc/src/vec/drain.rs +++ b/library/alloc/src/vec/drain.rs @@ -163,7 +163,7 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> { // it from the original vec but also avoid creating a &mut to the front since that could // invalidate raw pointers to it which some unsafe code might rely on. let vec_ptr = vec.as_mut().as_mut_ptr(); - let drop_offset = drop_ptr.offset_from(vec_ptr) as usize; + let drop_offset = drop_ptr.sub_ptr(vec_ptr); let to_drop = ptr::slice_from_raw_parts_mut(vec_ptr.add(drop_offset), drop_len); ptr::drop_in_place(to_drop); } diff --git a/library/alloc/src/vec/in_place_collect.rs b/library/alloc/src/vec/in_place_collect.rs index 282af8cc33f..55dcb84ad16 100644 --- a/library/alloc/src/vec/in_place_collect.rs +++ b/library/alloc/src/vec/in_place_collect.rs @@ -250,7 +250,7 @@ where let sink = self.try_fold::<_, _, Result<_, !>>(sink, write_in_place_with_drop(end)).unwrap(); // iteration succeeded, don't drop head - unsafe { ManuallyDrop::new(sink).dst.offset_from(dst_buf) as usize } + unsafe { ManuallyDrop::new(sink).dst.sub_ptr(dst_buf) } } } diff --git a/library/alloc/src/vec/in_place_drop.rs b/library/alloc/src/vec/in_place_drop.rs index 354d25c2389..1b1ef9130fa 100644 --- a/library/alloc/src/vec/in_place_drop.rs +++ b/library/alloc/src/vec/in_place_drop.rs @@ -10,7 +10,7 @@ pub(super) struct InPlaceDrop<T> { impl<T> InPlaceDrop<T> { fn len(&self) -> usize { - unsafe { self.dst.offset_from(self.inner) as usize } + unsafe { self.dst.sub_ptr(self.inner) } } } diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index a7df6f59b59..9b84a1d9b4b 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -169,7 +169,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> { let exact = if mem::size_of::<T>() == 0 { self.end.addr().wrapping_sub(self.ptr.addr()) } else { - unsafe { self.end.offset_from(self.ptr) as usize } + unsafe { self.end.sub_ptr(self.ptr) } }; (exact, Some(exact)) } |
