Skip to content

Commit 395ce34

Browse files
authored
Rollup merge of #100819 - WaffleLapkin:use_ptr_byte_methods, r=scottmcm
Make use of `[wrapping_]byte_{add,sub}` These new methods trivially replace old `.cast().wrapping_offset().cast()` & similar code. Note that [`arith_offset`](https://doc.rust-lang.org/std/intrinsics/fn.arith_offset.html) and `wrapping_offset` are the same thing. r? ``@scottmcm`` _split off from #100746_
2 parents 9f7e20b + 53565b2 commit 395ce34

File tree

10 files changed

+17
-18
lines changed

10 files changed

+17
-18
lines changed

Diff for: compiler/rustc_arena/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#![feature(maybe_uninit_slice)]
1717
#![feature(min_specialization)]
1818
#![feature(decl_macro)]
19+
#![feature(pointer_byte_offsets)]
1920
#![feature(rustc_attrs)]
2021
#![cfg_attr(test, feature(test))]
2122
#![feature(strict_provenance)]
@@ -211,7 +212,7 @@ impl<T> TypedArena<T> {
211212

212213
unsafe {
213214
if mem::size_of::<T>() == 0 {
214-
self.ptr.set((self.ptr.get() as *mut u8).wrapping_offset(1) as *mut T);
215+
self.ptr.set(self.ptr.get().wrapping_byte_add(1));
215216
let ptr = ptr::NonNull::<T>::dangling().as_ptr();
216217
// Don't drop the object. This `write` is equivalent to `forget`.
217218
ptr::write(ptr, object);

Diff for: library/alloc/src/vec/into_iter.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::alloc::{Allocator, Global};
44
use crate::raw_vec::RawVec;
55
use core::array;
66
use core::fmt;
7-
use core::intrinsics::arith_offset;
87
use core::iter::{
98
FusedIterator, InPlaceIterable, SourceIter, TrustedLen, TrustedRandomAccessNoCoerce,
109
};
@@ -154,7 +153,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
154153
// purposefully don't use 'ptr.offset' because for
155154
// vectors with 0-size elements this would return the
156155
// same pointer.
157-
self.ptr = unsafe { arith_offset(self.ptr as *const i8, 1) as *mut T };
156+
self.ptr = self.ptr.wrapping_byte_add(1);
158157

159158
// Make up a value of this ZST.
160159
Some(unsafe { mem::zeroed() })
@@ -184,7 +183,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
184183
// SAFETY: due to unchecked casts of unsigned amounts to signed offsets the wraparound
185184
// effectively results in unsigned pointers representing positions 0..usize::MAX,
186185
// which is valid for ZSTs.
187-
self.ptr = unsafe { arith_offset(self.ptr as *const i8, step_size as isize) as *mut T }
186+
self.ptr = self.ptr.wrapping_byte_add(step_size);
188187
} else {
189188
// SAFETY: the min() above ensures that step_size is in bounds
190189
self.ptr = unsafe { self.ptr.add(step_size) };
@@ -217,7 +216,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
217216
return Err(unsafe { array::IntoIter::new_unchecked(raw_ary, 0..len) });
218217
}
219218

220-
self.ptr = unsafe { arith_offset(self.ptr as *const i8, N as isize) as *mut T };
219+
self.ptr = self.ptr.wrapping_byte_add(N);
221220
// Safety: ditto
222221
return Ok(unsafe { MaybeUninit::array_assume_init(raw_ary) });
223222
}
@@ -267,7 +266,7 @@ impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> {
267266
None
268267
} else if mem::size_of::<T>() == 0 {
269268
// See above for why 'ptr.offset' isn't used
270-
self.end = unsafe { arith_offset(self.end as *const i8, -1) as *mut T };
269+
self.end = self.ptr.wrapping_byte_sub(1);
271270

272271
// Make up a value of this ZST.
273272
Some(unsafe { mem::zeroed() })
@@ -283,9 +282,7 @@ impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> {
283282
let step_size = self.len().min(n);
284283
if mem::size_of::<T>() == 0 {
285284
// SAFETY: same as for advance_by()
286-
self.end = unsafe {
287-
arith_offset(self.end as *const i8, step_size.wrapping_neg() as isize) as *mut T
288-
}
285+
self.end = self.end.wrapping_byte_sub(step_size);
289286
} else {
290287
// SAFETY: same as for advance_by()
291288
self.end = unsafe { self.end.sub(step_size) };

Diff for: library/alloc/src/vec/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use core::cmp::Ordering;
5959
use core::convert::TryFrom;
6060
use core::fmt;
6161
use core::hash::{Hash, Hasher};
62-
use core::intrinsics::{arith_offset, assume};
62+
use core::intrinsics::assume;
6363
use core::iter;
6464
#[cfg(not(no_global_oom_handling))]
6565
use core::iter::FromIterator;
@@ -2678,7 +2678,7 @@ impl<T, A: Allocator> IntoIterator for Vec<T, A> {
26782678
let alloc = ManuallyDrop::new(ptr::read(me.allocator()));
26792679
let begin = me.as_mut_ptr();
26802680
let end = if mem::size_of::<T>() == 0 {
2681-
arith_offset(begin as *const i8, me.len() as isize) as *const T
2681+
begin.wrapping_byte_add(me.len())
26822682
} else {
26832683
begin.add(me.len()) as *const T
26842684
};

Diff for: library/core/src/ptr/const_ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ impl<T: ?Sized> *const T {
249249
let offset = dest_addr.wrapping_sub(self_addr);
250250

251251
// This is the canonical desugarring of this operation
252-
self.cast::<u8>().wrapping_offset(offset).cast::<T>()
252+
self.wrapping_byte_offset(offset)
253253
}
254254

255255
/// Creates a new pointer by mapping `self`'s address to a new one.

Diff for: library/core/src/ptr/mut_ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl<T: ?Sized> *mut T {
255255
let offset = dest_addr.wrapping_sub(self_addr);
256256

257257
// This is the canonical desugarring of this operation
258-
self.cast::<u8>().wrapping_offset(offset).cast::<T>()
258+
self.wrapping_byte_offset(offset)
259259
}
260260

261261
/// Creates a new pointer by mapping `self`'s address to a new one.

Diff for: library/core/src/slice/iter/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ macro_rules! iterator {
6464
// backwards by `n`. `n` must not exceed `self.len()`.
6565
macro_rules! zst_shrink {
6666
($self: ident, $n: ident) => {
67-
$self.end = ($self.end as * $raw_mut u8).wrapping_offset(-$n) as * $raw_mut T;
67+
$self.end = $self.end.wrapping_byte_offset(-$n);
6868
}
6969
}
7070

Diff for: library/core/tests/atomic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ fn ptr_add_data() {
155155

156156
assert_eq!(atom.fetch_ptr_sub(1, SeqCst), n.wrapping_add(1));
157157
assert_eq!(atom.load(SeqCst), n);
158-
let bytes_from_n = |b| n.cast::<u8>().wrapping_add(b).cast::<i64>();
158+
let bytes_from_n = |b| n.wrapping_byte_add(b);
159159

160160
assert_eq!(atom.fetch_byte_add(1, SeqCst), n);
161161
assert_eq!(atom.load(SeqCst), bytes_from_n(1));

Diff for: library/core/tests/ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ fn thin_box() {
650650
.unwrap_or_else(|| handle_alloc_error(layout))
651651
.cast::<DynMetadata<T>>();
652652
ptr.as_ptr().write(meta);
653-
ptr.cast::<u8>().as_ptr().add(offset).cast::<Value>().write(value);
653+
ptr.as_ptr().byte_add(offset).cast::<Value>().write(value);
654654
Self { ptr, phantom: PhantomData }
655655
}
656656
}

Diff for: library/std/src/io/error/repr_bitpacked.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,10 @@ where
269269
}
270270
TAG_SIMPLE_MESSAGE => ErrorData::SimpleMessage(&*ptr.cast::<SimpleMessage>().as_ptr()),
271271
TAG_CUSTOM => {
272-
// It would be correct for us to use `ptr::sub` here (see the
272+
// It would be correct for us to use `ptr::byte_sub` here (see the
273273
// comment above the `wrapping_add` call in `new_custom` for why),
274274
// but it isn't clear that it makes a difference, so we don't.
275-
let custom = ptr.as_ptr().cast::<u8>().wrapping_sub(TAG_CUSTOM).cast::<Custom>();
275+
let custom = ptr.as_ptr().wrapping_byte_sub(TAG_CUSTOM).cast::<Custom>();
276276
ErrorData::Custom(make_custom(custom))
277277
}
278278
_ => {

Diff for: library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@
300300
#![feature(panic_can_unwind)]
301301
#![feature(panic_info_message)]
302302
#![feature(panic_internals)]
303+
#![feature(pointer_byte_offsets)]
303304
#![feature(pointer_is_aligned)]
304305
#![feature(portable_simd)]
305306
#![feature(prelude_2024)]

0 commit comments

Comments
 (0)