Skip to content

Add to_bits and from_bits to ptr::NonNull as well #93978

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

Closed
wants to merge 1 commit into from
Closed
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
61 changes: 61 additions & 0 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::fmt;
use crate::hash;
use crate::marker::Unsize;
use crate::mem::{self, MaybeUninit};
use crate::num::NonZeroUsize;
use crate::ops::{CoerceUnsized, DispatchFromDyn};
use crate::ptr::Unique;
use crate::slice::{self, SliceIndex};
Expand Down Expand Up @@ -401,6 +402,66 @@ impl<T: ?Sized> NonNull<T> {
// SAFETY: `self` is a `NonNull` pointer which is necessarily non-null
unsafe { NonNull::new_unchecked(self.as_ptr() as *mut U) }
}

/// Casts a pointer to its raw (non-zero) bits.
///
/// This is basically the same as `as` casting the underlying pointer, but
/// stays in non-null and non-zero land to avoid losing information.
///
/// The inverse method is [`from_bits`](#method.from_bits).
///
/// # Examples
///
/// ```
/// #![feature(ptr_to_from_bits)]
/// use std::num::NonZeroUsize;
/// use std::ptr::NonNull;
///
/// let dangling: NonNull<u8> = NonNull::dangling();
/// assert_eq!(dangling.to_bits(), NonZeroUsize::new(1).unwrap());
///
/// let misaligned: NonNull<u64> = NonNull::new(12345 as _).unwrap();
/// assert_eq!(misaligned.to_bits(), NonZeroUsize::new(12345).unwrap());
/// ```
#[unstable(feature = "ptr_to_from_bits", issue = "91126")]
pub fn to_bits(self) -> NonZeroUsize
where
T: Sized,
{
// SAFETY: `self` is a `NonNull` pointer which is necessarily non-null
unsafe { NonZeroUsize::new_unchecked(self.as_ptr().to_bits()) }
}

/// Creates a pointer from its raw (non-zero) bits.
///
/// This is basically the same as `as` casting to get the pointer, but
/// stays in non-null and non-zero land to avoid losing information.
///
/// The inverse method is [`to_bits`](#method.to_bits).
///
/// # Examples
///
/// ```
/// #![feature(ptr_to_from_bits)]
/// use std::num::NonZeroUsize;
/// use std::ptr::NonNull;
///
/// let dangling: NonNull<u8> = NonNull::dangling();
/// assert_eq!(dangling, NonNull::from_bits(NonZeroUsize::new(1).unwrap()));
///
/// assert_eq!(
/// NonNull::<i64>::from_bits(NonZeroUsize::new(12345).unwrap()),
/// NonNull::<i64>::new(12345 as _).unwrap(),
/// );
/// ```
#[unstable(feature = "ptr_to_from_bits", issue = "91126")]
pub fn from_bits(bits: NonZeroUsize) -> Self
where
T: Sized,
{
// SAFETY: `bits` is `NonZeroUsize`, so this always makes a non-null pointer
unsafe { Self::new_unchecked(<*mut T>::from_bits(bits.get())) }
}
}

impl<T> NonNull<[T]> {
Expand Down