Skip to content

Commit d6ea99d

Browse files
authored
Rollup merge of #103702 - WaffleLapkin:lift-sized-bounds-from-pointer-methods-where-applicable, r=m-ou-se
Lift `T: Sized` bounds from some `strict_provenance` pointer methods This PR removes requirement for `T` (pointee type) to be `Sized` to call `pointer::{addr, expose_addr, with_addr, map_addr}`. These functions don't use `T`'s size, so there is no reason for them to require this. Updated public API: cc ``@Gankra,`` #95228 r? libs-api
2 parents 74c1ad5 + 662f1f2 commit d6ea99d

File tree

3 files changed

+15
-48
lines changed

3 files changed

+15
-48
lines changed

library/core/src/ptr/const_ptr.rs

+6-18
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,11 @@ impl<T: ?Sized> *const T {
202202
#[must_use]
203203
#[inline(always)]
204204
#[unstable(feature = "strict_provenance", issue = "95228")]
205-
pub fn addr(self) -> usize
206-
where
207-
T: Sized,
208-
{
205+
pub fn addr(self) -> usize {
209206
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
210207
// SAFETY: Pointer-to-integer transmutes are valid (if you are okay with losing the
211208
// provenance).
212-
unsafe { mem::transmute(self) }
209+
unsafe { mem::transmute(self.cast::<()>()) }
213210
}
214211

215212
/// Gets the "address" portion of the pointer, and 'exposes' the "provenance" part for future
@@ -239,12 +236,9 @@ impl<T: ?Sized> *const T {
239236
#[must_use]
240237
#[inline(always)]
241238
#[unstable(feature = "strict_provenance", issue = "95228")]
242-
pub fn expose_addr(self) -> usize
243-
where
244-
T: Sized,
245-
{
239+
pub fn expose_addr(self) -> usize {
246240
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
247-
self as usize
241+
self.cast::<()>() as usize
248242
}
249243

250244
/// Creates a new pointer with the given address.
@@ -262,10 +256,7 @@ impl<T: ?Sized> *const T {
262256
#[must_use]
263257
#[inline]
264258
#[unstable(feature = "strict_provenance", issue = "95228")]
265-
pub fn with_addr(self, addr: usize) -> Self
266-
where
267-
T: Sized,
268-
{
259+
pub fn with_addr(self, addr: usize) -> Self {
269260
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
270261
//
271262
// In the mean-time, this operation is defined to be "as if" it was
@@ -288,10 +279,7 @@ impl<T: ?Sized> *const T {
288279
#[must_use]
289280
#[inline]
290281
#[unstable(feature = "strict_provenance", issue = "95228")]
291-
pub fn map_addr(self, f: impl FnOnce(usize) -> usize) -> Self
292-
where
293-
T: Sized,
294-
{
282+
pub fn map_addr(self, f: impl FnOnce(usize) -> usize) -> Self {
295283
self.with_addr(f(self.addr()))
296284
}
297285

library/core/src/ptr/mut_ptr.rs

+6-18
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,11 @@ impl<T: ?Sized> *mut T {
208208
#[must_use]
209209
#[inline(always)]
210210
#[unstable(feature = "strict_provenance", issue = "95228")]
211-
pub fn addr(self) -> usize
212-
where
213-
T: Sized,
214-
{
211+
pub fn addr(self) -> usize {
215212
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
216213
// SAFETY: Pointer-to-integer transmutes are valid (if you are okay with losing the
217214
// provenance).
218-
unsafe { mem::transmute(self) }
215+
unsafe { mem::transmute(self.cast::<()>()) }
219216
}
220217

221218
/// Gets the "address" portion of the pointer, and 'exposes' the "provenance" part for future
@@ -245,12 +242,9 @@ impl<T: ?Sized> *mut T {
245242
#[must_use]
246243
#[inline(always)]
247244
#[unstable(feature = "strict_provenance", issue = "95228")]
248-
pub fn expose_addr(self) -> usize
249-
where
250-
T: Sized,
251-
{
245+
pub fn expose_addr(self) -> usize {
252246
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
253-
self as usize
247+
self.cast::<()>() as usize
254248
}
255249

256250
/// Creates a new pointer with the given address.
@@ -268,10 +262,7 @@ impl<T: ?Sized> *mut T {
268262
#[must_use]
269263
#[inline]
270264
#[unstable(feature = "strict_provenance", issue = "95228")]
271-
pub fn with_addr(self, addr: usize) -> Self
272-
where
273-
T: Sized,
274-
{
265+
pub fn with_addr(self, addr: usize) -> Self {
275266
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
276267
//
277268
// In the mean-time, this operation is defined to be "as if" it was
@@ -294,10 +285,7 @@ impl<T: ?Sized> *mut T {
294285
#[must_use]
295286
#[inline]
296287
#[unstable(feature = "strict_provenance", issue = "95228")]
297-
pub fn map_addr(self, f: impl FnOnce(usize) -> usize) -> Self
298-
where
299-
T: Sized,
300-
{
288+
pub fn map_addr(self, f: impl FnOnce(usize) -> usize) -> Self {
301289
self.with_addr(f(self.addr()))
302290
}
303291

library/core/src/ptr/non_null.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,7 @@ impl<T: ?Sized> NonNull<T> {
268268
#[must_use]
269269
#[inline]
270270
#[unstable(feature = "strict_provenance", issue = "95228")]
271-
pub fn addr(self) -> NonZeroUsize
272-
where
273-
T: Sized,
274-
{
271+
pub fn addr(self) -> NonZeroUsize {
275272
// SAFETY: The pointer is guaranteed by the type to be non-null,
276273
// meaning that the address will be non-zero.
277274
unsafe { NonZeroUsize::new_unchecked(self.pointer.addr()) }
@@ -286,10 +283,7 @@ impl<T: ?Sized> NonNull<T> {
286283
#[must_use]
287284
#[inline]
288285
#[unstable(feature = "strict_provenance", issue = "95228")]
289-
pub fn with_addr(self, addr: NonZeroUsize) -> Self
290-
where
291-
T: Sized,
292-
{
286+
pub fn with_addr(self, addr: NonZeroUsize) -> Self {
293287
// SAFETY: The result of `ptr::from::with_addr` is non-null because `addr` is guaranteed to be non-zero.
294288
unsafe { NonNull::new_unchecked(self.pointer.with_addr(addr.get()) as *mut _) }
295289
}
@@ -303,10 +297,7 @@ impl<T: ?Sized> NonNull<T> {
303297
#[must_use]
304298
#[inline]
305299
#[unstable(feature = "strict_provenance", issue = "95228")]
306-
pub fn map_addr(self, f: impl FnOnce(NonZeroUsize) -> NonZeroUsize) -> Self
307-
where
308-
T: Sized,
309-
{
300+
pub fn map_addr(self, f: impl FnOnce(NonZeroUsize) -> NonZeroUsize) -> Self {
310301
self.with_addr(f(self.addr()))
311302
}
312303

0 commit comments

Comments
 (0)