Skip to content

Commit 9b3dfd8

Browse files
committed
core: Make pointer offset methods "const fn"
Signed-off-by: Joe Richey <joerichey@google.com>
1 parent 08df311 commit 9b3dfd8

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

src/libcore/intrinsics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,7 @@ extern "rust-intrinsic" {
13141314
/// The stabilized version of this intrinsic is
13151315
/// [`std::pointer::offset`](../../std/primitive.pointer.html#method.offset).
13161316
#[must_use = "returns a new pointer rather than modifying its argument"]
1317+
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
13171318
pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
13181319

13191320
/// Calculates the offset from a pointer, potentially wrapping.
@@ -1331,6 +1332,7 @@ extern "rust-intrinsic" {
13311332
/// The stabilized version of this intrinsic is
13321333
/// [`std::pointer::wrapping_offset`](../../std/primitive.pointer.html#method.wrapping_offset).
13331334
#[must_use = "returns a new pointer rather than modifying its argument"]
1335+
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
13341336
pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
13351337

13361338
/// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with

src/libcore/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
#![feature(const_panic)]
8686
#![feature(const_fn_union)]
8787
#![feature(const_generics)]
88+
#![feature(const_ptr_offset)]
8889
#![feature(const_ptr_offset_from)]
8990
#![feature(const_result)]
9091
#![feature(const_slice_from_raw_parts)]

src/libcore/ptr/const_ptr.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,9 @@ impl<T: ?Sized> *const T {
151151
/// ```
152152
#[stable(feature = "rust1", since = "1.0.0")]
153153
#[must_use = "returns a new pointer rather than modifying its argument"]
154+
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
154155
#[inline]
155-
pub unsafe fn offset(self, count: isize) -> *const T
156+
pub const unsafe fn offset(self, count: isize) -> *const T
156157
where
157158
T: Sized,
158159
{
@@ -210,8 +211,9 @@ impl<T: ?Sized> *const T {
210211
/// ```
211212
#[stable(feature = "ptr_wrapping_offset", since = "1.16.0")]
212213
#[must_use = "returns a new pointer rather than modifying its argument"]
214+
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
213215
#[inline]
214-
pub fn wrapping_offset(self, count: isize) -> *const T
216+
pub const fn wrapping_offset(self, count: isize) -> *const T
215217
where
216218
T: Sized,
217219
{
@@ -393,8 +395,9 @@ impl<T: ?Sized> *const T {
393395
/// ```
394396
#[stable(feature = "pointer_methods", since = "1.26.0")]
395397
#[must_use = "returns a new pointer rather than modifying its argument"]
398+
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
396399
#[inline]
397-
pub unsafe fn add(self, count: usize) -> Self
400+
pub const unsafe fn add(self, count: usize) -> Self
398401
where
399402
T: Sized,
400403
{
@@ -455,8 +458,9 @@ impl<T: ?Sized> *const T {
455458
/// ```
456459
#[stable(feature = "pointer_methods", since = "1.26.0")]
457460
#[must_use = "returns a new pointer rather than modifying its argument"]
461+
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
458462
#[inline]
459-
pub unsafe fn sub(self, count: usize) -> Self
463+
pub const unsafe fn sub(self, count: usize) -> Self
460464
where
461465
T: Sized,
462466
{
@@ -511,8 +515,9 @@ impl<T: ?Sized> *const T {
511515
/// ```
512516
#[stable(feature = "pointer_methods", since = "1.26.0")]
513517
#[must_use = "returns a new pointer rather than modifying its argument"]
518+
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
514519
#[inline]
515-
pub fn wrapping_add(self, count: usize) -> Self
520+
pub const fn wrapping_add(self, count: usize) -> Self
516521
where
517522
T: Sized,
518523
{
@@ -567,8 +572,9 @@ impl<T: ?Sized> *const T {
567572
/// ```
568573
#[stable(feature = "pointer_methods", since = "1.26.0")]
569574
#[must_use = "returns a new pointer rather than modifying its argument"]
575+
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
570576
#[inline]
571-
pub fn wrapping_sub(self, count: usize) -> Self
577+
pub const fn wrapping_sub(self, count: usize) -> Self
572578
where
573579
T: Sized,
574580
{

src/libcore/ptr/mut_ptr.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ impl<T: ?Sized> *mut T {
145145
/// ```
146146
#[stable(feature = "rust1", since = "1.0.0")]
147147
#[must_use = "returns a new pointer rather than modifying its argument"]
148+
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
148149
#[inline]
149-
pub unsafe fn offset(self, count: isize) -> *mut T
150+
pub const unsafe fn offset(self, count: isize) -> *mut T
150151
where
151152
T: Sized,
152153
{
@@ -203,8 +204,9 @@ impl<T: ?Sized> *mut T {
203204
/// ```
204205
#[stable(feature = "ptr_wrapping_offset", since = "1.16.0")]
205206
#[must_use = "returns a new pointer rather than modifying its argument"]
207+
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
206208
#[inline]
207-
pub fn wrapping_offset(self, count: isize) -> *mut T
209+
pub const fn wrapping_offset(self, count: isize) -> *mut T
208210
where
209211
T: Sized,
210212
{
@@ -439,8 +441,9 @@ impl<T: ?Sized> *mut T {
439441
/// ```
440442
#[stable(feature = "pointer_methods", since = "1.26.0")]
441443
#[must_use = "returns a new pointer rather than modifying its argument"]
444+
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
442445
#[inline]
443-
pub unsafe fn add(self, count: usize) -> Self
446+
pub const unsafe fn add(self, count: usize) -> Self
444447
where
445448
T: Sized,
446449
{
@@ -501,8 +504,9 @@ impl<T: ?Sized> *mut T {
501504
/// ```
502505
#[stable(feature = "pointer_methods", since = "1.26.0")]
503506
#[must_use = "returns a new pointer rather than modifying its argument"]
507+
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
504508
#[inline]
505-
pub unsafe fn sub(self, count: usize) -> Self
509+
pub const unsafe fn sub(self, count: usize) -> Self
506510
where
507511
T: Sized,
508512
{
@@ -557,8 +561,9 @@ impl<T: ?Sized> *mut T {
557561
/// ```
558562
#[stable(feature = "pointer_methods", since = "1.26.0")]
559563
#[must_use = "returns a new pointer rather than modifying its argument"]
564+
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
560565
#[inline]
561-
pub fn wrapping_add(self, count: usize) -> Self
566+
pub const fn wrapping_add(self, count: usize) -> Self
562567
where
563568
T: Sized,
564569
{
@@ -613,8 +618,9 @@ impl<T: ?Sized> *mut T {
613618
/// ```
614619
#[stable(feature = "pointer_methods", since = "1.26.0")]
615620
#[must_use = "returns a new pointer rather than modifying its argument"]
621+
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
616622
#[inline]
617-
pub fn wrapping_sub(self, count: usize) -> Self
623+
pub const fn wrapping_sub(self, count: usize) -> Self
618624
where
619625
T: Sized,
620626
{

0 commit comments

Comments
 (0)