Skip to content

Commit d5d081c

Browse files
authored
Rollup merge of #102198 - lukas-code:nonnull_as_ref, r=Amanieu
`const`-stablilize `NonNull::as_ref` A bunch of pointer to reference methods have been made unstably const some time ago in #91823 under the feature gate `const_ptr_as_ref`. Out of these, `NonNull::as_ref` can be implemented as a `const fn` in stable rust today, so i hereby propose to const stabilize this function only. Tracking issue: #91822 ``@rustbot`` label +T-libs-api -T-libs
2 parents 9579451 + 48d0364 commit d5d081c

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

core/src/ptr/non_null.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,14 @@ impl<T: ?Sized> NonNull<T> {
367367
///
368368
/// [the module documentation]: crate::ptr#safety
369369
#[stable(feature = "nonnull", since = "1.25.0")]
370-
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
370+
#[rustc_const_stable(feature = "const_nonnull_as_ref", since = "CURRENT_RUSTC_VERSION")]
371371
#[must_use]
372372
#[inline(always)]
373373
pub const unsafe fn as_ref<'a>(&self) -> &'a T {
374374
// SAFETY: the caller must guarantee that `self` meets all the
375375
// requirements for a reference.
376-
unsafe { &*self.as_ptr() }
376+
// `cast_const` avoids a mutable raw pointer deref.
377+
unsafe { &*self.as_ptr().cast_const() }
377378
}
378379

379380
/// Returns a unique reference to the value. If the value may be uninitialized, [`as_uninit_mut`]

0 commit comments

Comments
 (0)