Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make PTR::as_ref and similar methods const. #91823

Merged
merged 1 commit into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions library/core/src/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@ impl<T: ?Sized> *const T {
/// }
/// ```
#[stable(feature = "ptr_as_ref", since = "1.9.0")]
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
#[inline]
pub unsafe fn as_ref<'a>(self) -> Option<&'a T> {
pub const unsafe fn as_ref<'a>(self) -> Option<&'a T> {
// SAFETY: the caller must guarantee that `self` is valid
// for a reference if it isn't null.
if self.is_null() { None } else { unsafe { Some(&*self) } }
Expand Down Expand Up @@ -211,7 +212,8 @@ impl<T: ?Sized> *const T {
/// ```
#[inline]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
pub unsafe fn as_uninit_ref<'a>(self) -> Option<&'a MaybeUninit<T>>
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
pub const unsafe fn as_uninit_ref<'a>(self) -> Option<&'a MaybeUninit<T>>
where
T: Sized,
{
Expand Down Expand Up @@ -1068,7 +1070,8 @@ impl<T> *const [T] {
/// [allocated object]: crate::ptr#allocated-object
#[inline]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
pub unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]> {
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
pub const unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]> {
if self.is_null() {
None
} else {
Expand Down
18 changes: 12 additions & 6 deletions library/core/src/ptr/mut_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ impl<T: ?Sized> *mut T {
/// }
/// ```
#[stable(feature = "ptr_as_ref", since = "1.9.0")]
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
#[inline]
pub unsafe fn as_ref<'a>(self) -> Option<&'a T> {
pub const unsafe fn as_ref<'a>(self) -> Option<&'a T> {
// SAFETY: the caller must guarantee that `self` is valid for a
// reference if it isn't null.
if self.is_null() { None } else { unsafe { Some(&*self) } }
Expand Down Expand Up @@ -217,7 +218,8 @@ impl<T: ?Sized> *mut T {
/// ```
#[inline]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
pub unsafe fn as_uninit_ref<'a>(self) -> Option<&'a MaybeUninit<T>>
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
pub const unsafe fn as_uninit_ref<'a>(self) -> Option<&'a MaybeUninit<T>>
where
T: Sized,
{
Expand Down Expand Up @@ -411,8 +413,9 @@ impl<T: ?Sized> *mut T {
/// println!("{:?}", s); // It'll print: "[4, 2, 3]".
/// ```
#[stable(feature = "ptr_as_ref", since = "1.9.0")]
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
#[inline]
pub unsafe fn as_mut<'a>(self) -> Option<&'a mut T> {
pub const unsafe fn as_mut<'a>(self) -> Option<&'a mut T> {
// SAFETY: the caller must guarantee that `self` is be valid for
// a mutable reference if it isn't null.
if self.is_null() { None } else { unsafe { Some(&mut *self) } }
Expand Down Expand Up @@ -446,7 +449,8 @@ impl<T: ?Sized> *mut T {
/// [the module documentation]: crate::ptr#safety
#[inline]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
pub unsafe fn as_uninit_mut<'a>(self) -> Option<&'a mut MaybeUninit<T>>
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
pub const unsafe fn as_uninit_mut<'a>(self) -> Option<&'a mut MaybeUninit<T>>
where
T: Sized,
{
Expand Down Expand Up @@ -1334,7 +1338,8 @@ impl<T> *mut [T] {
/// [allocated object]: crate::ptr#allocated-object
#[inline]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
pub unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]> {
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
pub const unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]> {
if self.is_null() {
None
} else {
Expand Down Expand Up @@ -1385,7 +1390,8 @@ impl<T> *mut [T] {
/// [allocated object]: crate::ptr#allocated-object
#[inline]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
pub unsafe fn as_uninit_slice_mut<'a>(self) -> Option<&'a mut [MaybeUninit<T>]> {
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
pub const unsafe fn as_uninit_slice_mut<'a>(self) -> Option<&'a mut [MaybeUninit<T>]> {
if self.is_null() {
None
} else {
Expand Down
18 changes: 12 additions & 6 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ impl<T: Sized> NonNull<T> {
#[inline]
#[must_use]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
pub unsafe fn as_uninit_ref<'a>(&self) -> &'a MaybeUninit<T> {
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
pub const unsafe fn as_uninit_ref<'a>(&self) -> &'a MaybeUninit<T> {
// SAFETY: the caller must guarantee that `self` meets all the
// requirements for a reference.
unsafe { &*self.cast().as_ptr() }
Expand Down Expand Up @@ -155,7 +156,8 @@ impl<T: Sized> NonNull<T> {
#[inline]
#[must_use]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
pub unsafe fn as_uninit_mut<'a>(&mut self) -> &'a mut MaybeUninit<T> {
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
pub const unsafe fn as_uninit_mut<'a>(&mut self) -> &'a mut MaybeUninit<T> {
// SAFETY: the caller must guarantee that `self` meets all the
// requirements for a reference.
unsafe { &mut *self.cast().as_ptr() }
Expand Down Expand Up @@ -316,9 +318,10 @@ impl<T: ?Sized> NonNull<T> {
///
/// [the module documentation]: crate::ptr#safety
#[stable(feature = "nonnull", since = "1.25.0")]
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
#[must_use]
#[inline]
pub unsafe fn as_ref<'a>(&self) -> &'a T {
pub const unsafe fn as_ref<'a>(&self) -> &'a T {
// SAFETY: the caller must guarantee that `self` meets all the
// requirements for a reference.
unsafe { &*self.as_ptr() }
Expand Down Expand Up @@ -366,9 +369,10 @@ impl<T: ?Sized> NonNull<T> {
///
/// [the module documentation]: crate::ptr#safety
#[stable(feature = "nonnull", since = "1.25.0")]
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
#[must_use]
#[inline]
pub unsafe fn as_mut<'a>(&mut self) -> &'a mut T {
pub const unsafe fn as_mut<'a>(&mut self) -> &'a mut T {
// SAFETY: the caller must guarantee that `self` meets all the
// requirements for a mutable reference.
unsafe { &mut *self.as_ptr() }
Expand Down Expand Up @@ -534,7 +538,8 @@ impl<T> NonNull<[T]> {
#[inline]
#[must_use]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
pub unsafe fn as_uninit_slice<'a>(&self) -> &'a [MaybeUninit<T>] {
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
pub const unsafe fn as_uninit_slice<'a>(&self) -> &'a [MaybeUninit<T>] {
// SAFETY: the caller must uphold the safety contract for `as_uninit_slice`.
unsafe { slice::from_raw_parts(self.cast().as_ptr(), self.len()) }
}
Expand Down Expand Up @@ -596,7 +601,8 @@ impl<T> NonNull<[T]> {
#[inline]
#[must_use]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
pub unsafe fn as_uninit_slice_mut<'a>(&self) -> &'a mut [MaybeUninit<T>] {
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
pub const unsafe fn as_uninit_slice_mut<'a>(&self) -> &'a mut [MaybeUninit<T>] {
// SAFETY: the caller must uphold the safety contract for `as_uninit_slice_mut`.
unsafe { slice::from_raw_parts_mut(self.cast().as_ptr(), self.len()) }
}
Expand Down