Skip to content

Commit d3b5192

Browse files
committed
Simplify implementation of various pointer methods
1 parent 6c54745 commit d3b5192

File tree

3 files changed

+15
-25
lines changed

3 files changed

+15
-25
lines changed

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
#![feature(maybe_uninit_uninit_array)]
155155
#![feature(ptr_alignment_type)]
156156
#![feature(ptr_metadata)]
157+
#![feature(set_ptr_value)]
157158
#![feature(slice_ptr_get)]
158159
#![feature(slice_split_at_unchecked)]
159160
#![feature(str_internals)]

library/core/src/ptr/const_ptr.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,7 @@ impl<T: ?Sized> *const T {
473473
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
474474
pub const unsafe fn byte_offset(self, count: isize) -> Self {
475475
// SAFETY: the caller must uphold the safety contract for `offset`.
476-
let this = unsafe { self.cast::<u8>().offset(count).cast::<()>() };
477-
from_raw_parts::<T>(this, metadata(self))
476+
unsafe { self.cast::<u8>().offset(count).with_metadata_of(self) }
478477
}
479478

480479
/// Calculates the offset from a pointer using wrapping arithmetic.
@@ -554,7 +553,7 @@ impl<T: ?Sized> *const T {
554553
#[unstable(feature = "pointer_byte_offsets", issue = "96283")]
555554
#[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")]
556555
pub const fn wrapping_byte_offset(self, count: isize) -> Self {
557-
from_raw_parts::<T>(self.cast::<u8>().wrapping_offset(count).cast::<()>(), metadata(self))
556+
self.cast::<u8>().wrapping_offset(count).with_metadata_of(self)
558557
}
559558

560559
/// Masks out bits of the pointer according to a mask.
@@ -567,8 +566,7 @@ impl<T: ?Sized> *const T {
567566
#[must_use = "returns a new pointer rather than modifying its argument"]
568567
#[inline(always)]
569568
pub fn mask(self, mask: usize) -> *const T {
570-
let this = intrinsics::ptr_mask(self.cast::<()>(), mask);
571-
from_raw_parts::<T>(this, metadata(self))
569+
intrinsics::ptr_mask(self.cast::<()>(), mask).with_metadata_of(self)
572570
}
573571

574572
/// Calculates the distance between two pointers. The returned value is in
@@ -906,8 +904,7 @@ impl<T: ?Sized> *const T {
906904
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
907905
pub const unsafe fn byte_add(self, count: usize) -> Self {
908906
// SAFETY: the caller must uphold the safety contract for `add`.
909-
let this = unsafe { self.cast::<u8>().add(count).cast::<()>() };
910-
from_raw_parts::<T>(this, metadata(self))
907+
unsafe { self.cast::<u8>().add(count).with_metadata_of(self) }
911908
}
912909

913910
/// Calculates the offset from a pointer (convenience for
@@ -993,8 +990,7 @@ impl<T: ?Sized> *const T {
993990
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
994991
pub const unsafe fn byte_sub(self, count: usize) -> Self {
995992
// SAFETY: the caller must uphold the safety contract for `sub`.
996-
let this = unsafe { self.cast::<u8>().sub(count).cast::<()>() };
997-
from_raw_parts::<T>(this, metadata(self))
993+
unsafe { self.cast::<u8>().sub(count).with_metadata_of(self) }
998994
}
999995

1000996
/// Calculates the offset from a pointer using wrapping arithmetic.
@@ -1074,7 +1070,7 @@ impl<T: ?Sized> *const T {
10741070
#[unstable(feature = "pointer_byte_offsets", issue = "96283")]
10751071
#[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")]
10761072
pub const fn wrapping_byte_add(self, count: usize) -> Self {
1077-
from_raw_parts::<T>(self.cast::<u8>().wrapping_add(count).cast::<()>(), metadata(self))
1073+
self.cast::<u8>().wrapping_add(count).with_metadata_of(self)
10781074
}
10791075

10801076
/// Calculates the offset from a pointer using wrapping arithmetic.
@@ -1154,7 +1150,7 @@ impl<T: ?Sized> *const T {
11541150
#[unstable(feature = "pointer_byte_offsets", issue = "96283")]
11551151
#[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")]
11561152
pub const fn wrapping_byte_sub(self, count: usize) -> Self {
1157-
from_raw_parts::<T>(self.cast::<u8>().wrapping_sub(count).cast::<()>(), metadata(self))
1153+
self.cast::<u8>().wrapping_sub(count).with_metadata_of(self)
11581154
}
11591155

11601156
/// Reads the value from `self` without moving it. This leaves the

library/core/src/ptr/mut_ptr.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,7 @@ impl<T: ?Sized> *mut T {
487487
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
488488
pub const unsafe fn byte_offset(self, count: isize) -> Self {
489489
// SAFETY: the caller must uphold the safety contract for `offset`.
490-
let this = unsafe { self.cast::<u8>().offset(count).cast::<()>() };
491-
from_raw_parts_mut::<T>(this, metadata(self))
490+
unsafe { self.cast::<u8>().offset(count).with_metadata_of(self) }
492491
}
493492

494493
/// Calculates the offset from a pointer using wrapping arithmetic.
@@ -567,10 +566,7 @@ impl<T: ?Sized> *mut T {
567566
#[unstable(feature = "pointer_byte_offsets", issue = "96283")]
568567
#[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")]
569568
pub const fn wrapping_byte_offset(self, count: isize) -> Self {
570-
from_raw_parts_mut::<T>(
571-
self.cast::<u8>().wrapping_offset(count).cast::<()>(),
572-
metadata(self),
573-
)
569+
self.cast::<u8>().wrapping_offset(count).with_metadata_of(self)
574570
}
575571

576572
/// Masks out bits of the pointer according to a mask.
@@ -583,8 +579,7 @@ impl<T: ?Sized> *mut T {
583579
#[must_use = "returns a new pointer rather than modifying its argument"]
584580
#[inline(always)]
585581
pub fn mask(self, mask: usize) -> *mut T {
586-
let this = intrinsics::ptr_mask(self.cast::<()>(), mask) as *mut ();
587-
from_raw_parts_mut::<T>(this, metadata(self))
582+
intrinsics::ptr_mask(self.cast::<()>(), mask).cast_mut().with_metadata_of(self)
588583
}
589584

590585
/// Returns `None` if the pointer is null, or else returns a unique reference to
@@ -1011,8 +1006,7 @@ impl<T: ?Sized> *mut T {
10111006
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
10121007
pub const unsafe fn byte_add(self, count: usize) -> Self {
10131008
// SAFETY: the caller must uphold the safety contract for `add`.
1014-
let this = unsafe { self.cast::<u8>().add(count).cast::<()>() };
1015-
from_raw_parts_mut::<T>(this, metadata(self))
1009+
unsafe { self.cast::<u8>().add(count).with_metadata_of(self) }
10161010
}
10171011

10181012
/// Calculates the offset from a pointer (convenience for
@@ -1098,8 +1092,7 @@ impl<T: ?Sized> *mut T {
10981092
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
10991093
pub const unsafe fn byte_sub(self, count: usize) -> Self {
11001094
// SAFETY: the caller must uphold the safety contract for `sub`.
1101-
let this = unsafe { self.cast::<u8>().sub(count).cast::<()>() };
1102-
from_raw_parts_mut::<T>(this, metadata(self))
1095+
unsafe { self.cast::<u8>().sub(count).with_metadata_of(self) }
11031096
}
11041097

11051098
/// Calculates the offset from a pointer using wrapping arithmetic.
@@ -1179,7 +1172,7 @@ impl<T: ?Sized> *mut T {
11791172
#[unstable(feature = "pointer_byte_offsets", issue = "96283")]
11801173
#[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")]
11811174
pub const fn wrapping_byte_add(self, count: usize) -> Self {
1182-
from_raw_parts_mut::<T>(self.cast::<u8>().wrapping_add(count).cast::<()>(), metadata(self))
1175+
self.cast::<u8>().wrapping_add(count).with_metadata_of(self)
11831176
}
11841177

11851178
/// Calculates the offset from a pointer using wrapping arithmetic.
@@ -1259,7 +1252,7 @@ impl<T: ?Sized> *mut T {
12591252
#[unstable(feature = "pointer_byte_offsets", issue = "96283")]
12601253
#[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")]
12611254
pub const fn wrapping_byte_sub(self, count: usize) -> Self {
1262-
from_raw_parts_mut::<T>(self.cast::<u8>().wrapping_sub(count).cast::<()>(), metadata(self))
1255+
self.cast::<u8>().wrapping_sub(count).with_metadata_of(self)
12631256
}
12641257

12651258
/// Reads the value from `self` without moving it. This leaves the

0 commit comments

Comments
 (0)