Skip to content

Commit 4d840a6

Browse files
authored
Rollup merge of #91823 - woppopo:const_ptr_as_ref, r=lcnr
Make `PTR::as_ref` and similar methods `const`. Tracking issue: #91822 Feature gate: `#![feature(const_ptr_as_ref)]` ```rust // core::ptr impl<T: ?Sized> *const T { pub const unsafe fn as_ref<'a>(self) -> Option<&'a T>; pub const unsafe fn as_uninit_ref<'a>(self) -> Option<&'a MaybeUninit<T>> where T: Sized; pub const unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]>; } impl<T: ?Sized> *mut T { pub const unsafe fn as_ref<'a>(self) -> Option<&'a T>; pub const unsafe fn as_uninit_ref<'a>(self) -> Option<&'a MaybeUninit<T>> where T: Sized; pub const unsafe fn as_mut<'a>(self) -> Option<&'a mut T>; pub const unsafe fn as_uninit_mut<'a>(self) -> Option<&'a mut MaybeUninit<T>> where T: Sized; pub const unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]>; pub const unsafe fn as_uninit_slice_mut<'a>(self) -> Option<&'a mut [MaybeUninit<T>]>; } impl<T: Sized> NonNull<T> { pub const unsafe fn as_uninit_ref<'a>(&self) -> &'a MaybeUninit<T>; pub const unsafe fn as_uninit_mut<'a>(&mut self) -> &'a mut MaybeUninit<T>; } impl<T: ?Sized> NonNull<T> { pub const unsafe fn as_ref<'a>(&self) -> &'a T; pub const unsafe fn as_mut<'a>(&mut self) -> &'a mut T; pub const unsafe fn as_uninit_slice<'a>(&self) -> &'a [MaybeUninit<T>]; pub const unsafe fn as_uninit_slice_mut<'a>(&self) -> &'a mut [MaybeUninit<T>]; } ```
2 parents 790950a + a4b3fe0 commit 4d840a6

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

library/core/src/ptr/const_ptr.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,9 @@ impl<T: ?Sized> *const T {
163163
/// }
164164
/// ```
165165
#[stable(feature = "ptr_as_ref", since = "1.9.0")]
166+
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
166167
#[inline]
167-
pub unsafe fn as_ref<'a>(self) -> Option<&'a T> {
168+
pub const unsafe fn as_ref<'a>(self) -> Option<&'a T> {
168169
// SAFETY: the caller must guarantee that `self` is valid
169170
// for a reference if it isn't null.
170171
if self.is_null() { None } else { unsafe { Some(&*self) } }
@@ -211,7 +212,8 @@ impl<T: ?Sized> *const T {
211212
/// ```
212213
#[inline]
213214
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
214-
pub unsafe fn as_uninit_ref<'a>(self) -> Option<&'a MaybeUninit<T>>
215+
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
216+
pub const unsafe fn as_uninit_ref<'a>(self) -> Option<&'a MaybeUninit<T>>
215217
where
216218
T: Sized,
217219
{
@@ -1068,7 +1070,8 @@ impl<T> *const [T] {
10681070
/// [allocated object]: crate::ptr#allocated-object
10691071
#[inline]
10701072
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
1071-
pub unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]> {
1073+
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
1074+
pub const unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]> {
10721075
if self.is_null() {
10731076
None
10741077
} else {

library/core/src/ptr/mut_ptr.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,9 @@ impl<T: ?Sized> *mut T {
166166
/// }
167167
/// ```
168168
#[stable(feature = "ptr_as_ref", since = "1.9.0")]
169+
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
169170
#[inline]
170-
pub unsafe fn as_ref<'a>(self) -> Option<&'a T> {
171+
pub const unsafe fn as_ref<'a>(self) -> Option<&'a T> {
171172
// SAFETY: the caller must guarantee that `self` is valid for a
172173
// reference if it isn't null.
173174
if self.is_null() { None } else { unsafe { Some(&*self) } }
@@ -217,7 +218,8 @@ impl<T: ?Sized> *mut T {
217218
/// ```
218219
#[inline]
219220
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
220-
pub unsafe fn as_uninit_ref<'a>(self) -> Option<&'a MaybeUninit<T>>
221+
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
222+
pub const unsafe fn as_uninit_ref<'a>(self) -> Option<&'a MaybeUninit<T>>
221223
where
222224
T: Sized,
223225
{
@@ -411,8 +413,9 @@ impl<T: ?Sized> *mut T {
411413
/// println!("{:?}", s); // It'll print: "[4, 2, 3]".
412414
/// ```
413415
#[stable(feature = "ptr_as_ref", since = "1.9.0")]
416+
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
414417
#[inline]
415-
pub unsafe fn as_mut<'a>(self) -> Option<&'a mut T> {
418+
pub const unsafe fn as_mut<'a>(self) -> Option<&'a mut T> {
416419
// SAFETY: the caller must guarantee that `self` is be valid for
417420
// a mutable reference if it isn't null.
418421
if self.is_null() { None } else { unsafe { Some(&mut *self) } }
@@ -446,7 +449,8 @@ impl<T: ?Sized> *mut T {
446449
/// [the module documentation]: crate::ptr#safety
447450
#[inline]
448451
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
449-
pub unsafe fn as_uninit_mut<'a>(self) -> Option<&'a mut MaybeUninit<T>>
452+
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
453+
pub const unsafe fn as_uninit_mut<'a>(self) -> Option<&'a mut MaybeUninit<T>>
450454
where
451455
T: Sized,
452456
{
@@ -1335,7 +1339,8 @@ impl<T> *mut [T] {
13351339
/// [allocated object]: crate::ptr#allocated-object
13361340
#[inline]
13371341
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
1338-
pub unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]> {
1342+
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
1343+
pub const unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]> {
13391344
if self.is_null() {
13401345
None
13411346
} else {
@@ -1386,7 +1391,8 @@ impl<T> *mut [T] {
13861391
/// [allocated object]: crate::ptr#allocated-object
13871392
#[inline]
13881393
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
1389-
pub unsafe fn as_uninit_slice_mut<'a>(self) -> Option<&'a mut [MaybeUninit<T>]> {
1394+
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
1395+
pub const unsafe fn as_uninit_slice_mut<'a>(self) -> Option<&'a mut [MaybeUninit<T>]> {
13901396
if self.is_null() {
13911397
None
13921398
} else {

library/core/src/ptr/non_null.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ impl<T: Sized> NonNull<T> {
122122
#[inline]
123123
#[must_use]
124124
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
125-
pub unsafe fn as_uninit_ref<'a>(&self) -> &'a MaybeUninit<T> {
125+
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
126+
pub const unsafe fn as_uninit_ref<'a>(&self) -> &'a MaybeUninit<T> {
126127
// SAFETY: the caller must guarantee that `self` meets all the
127128
// requirements for a reference.
128129
unsafe { &*self.cast().as_ptr() }
@@ -155,7 +156,8 @@ impl<T: Sized> NonNull<T> {
155156
#[inline]
156157
#[must_use]
157158
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
158-
pub unsafe fn as_uninit_mut<'a>(&mut self) -> &'a mut MaybeUninit<T> {
159+
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
160+
pub const unsafe fn as_uninit_mut<'a>(&mut self) -> &'a mut MaybeUninit<T> {
159161
// SAFETY: the caller must guarantee that `self` meets all the
160162
// requirements for a reference.
161163
unsafe { &mut *self.cast().as_ptr() }
@@ -316,9 +318,10 @@ impl<T: ?Sized> NonNull<T> {
316318
///
317319
/// [the module documentation]: crate::ptr#safety
318320
#[stable(feature = "nonnull", since = "1.25.0")]
321+
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
319322
#[must_use]
320323
#[inline]
321-
pub unsafe fn as_ref<'a>(&self) -> &'a T {
324+
pub const unsafe fn as_ref<'a>(&self) -> &'a T {
322325
// SAFETY: the caller must guarantee that `self` meets all the
323326
// requirements for a reference.
324327
unsafe { &*self.as_ptr() }
@@ -366,9 +369,10 @@ impl<T: ?Sized> NonNull<T> {
366369
///
367370
/// [the module documentation]: crate::ptr#safety
368371
#[stable(feature = "nonnull", since = "1.25.0")]
372+
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
369373
#[must_use]
370374
#[inline]
371-
pub unsafe fn as_mut<'a>(&mut self) -> &'a mut T {
375+
pub const unsafe fn as_mut<'a>(&mut self) -> &'a mut T {
372376
// SAFETY: the caller must guarantee that `self` meets all the
373377
// requirements for a mutable reference.
374378
unsafe { &mut *self.as_ptr() }
@@ -534,7 +538,8 @@ impl<T> NonNull<[T]> {
534538
#[inline]
535539
#[must_use]
536540
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
537-
pub unsafe fn as_uninit_slice<'a>(&self) -> &'a [MaybeUninit<T>] {
541+
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
542+
pub const unsafe fn as_uninit_slice<'a>(&self) -> &'a [MaybeUninit<T>] {
538543
// SAFETY: the caller must uphold the safety contract for `as_uninit_slice`.
539544
unsafe { slice::from_raw_parts(self.cast().as_ptr(), self.len()) }
540545
}
@@ -596,7 +601,8 @@ impl<T> NonNull<[T]> {
596601
#[inline]
597602
#[must_use]
598603
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
599-
pub unsafe fn as_uninit_slice_mut<'a>(&self) -> &'a mut [MaybeUninit<T>] {
604+
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
605+
pub const unsafe fn as_uninit_slice_mut<'a>(&self) -> &'a mut [MaybeUninit<T>] {
600606
// SAFETY: the caller must uphold the safety contract for `as_uninit_slice_mut`.
601607
unsafe { slice::from_raw_parts_mut(self.cast().as_ptr(), self.len()) }
602608
}

0 commit comments

Comments
 (0)