Skip to content

Commit 2f8e2ff

Browse files
authored
Rollup merge of #91127 - scottmcm:ptr_to_from_bits, r=dtolnay
Add `<*{const|mut} T>::{to|from}_bits` Named based on the floating-point methods of the same name, as those are also about returning the *representation* of the value. Tracking issue: #91126 Based on the conversation in https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60/near/238391074 r? `@joshtriplett`
2 parents c185610 + 348a250 commit 2f8e2ff

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

library/core/src/ptr/const_ptr.rs

+48
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,54 @@ impl<T: ?Sized> *const T {
4848
self as _
4949
}
5050

51+
/// Casts a pointer to its raw bits.
52+
///
53+
/// This is equivalent to `as usize`, but is more specific to enhance readability.
54+
/// The inverse method is [`from_bits`](#method.from_bits).
55+
///
56+
/// In particular, `*p as usize` and `p as usize` will both compile for
57+
/// pointers to numeric types but do very different things, so using this
58+
/// helps emphasize that reading the bits was intentional.
59+
///
60+
/// # Examples
61+
///
62+
/// ```
63+
/// #![feature(ptr_to_from_bits)]
64+
/// let array = [13, 42];
65+
/// let p0: *const i32 = &array[0];
66+
/// assert_eq!(<*const _>::from_bits(p0.to_bits()), p0);
67+
/// let p1: *const i32 = &array[1];
68+
/// assert_eq!(p1.to_bits() - p0.to_bits(), 4);
69+
/// ```
70+
#[unstable(feature = "ptr_to_from_bits", issue = "91126")]
71+
pub fn to_bits(self) -> usize
72+
where
73+
T: Sized,
74+
{
75+
self as usize
76+
}
77+
78+
/// Creates a pointer from its raw bits.
79+
///
80+
/// This is equivalent to `as *const T`, but is more specific to enhance readability.
81+
/// The inverse method is [`to_bits`](#method.to_bits).
82+
///
83+
/// # Examples
84+
///
85+
/// ```
86+
/// #![feature(ptr_to_from_bits)]
87+
/// use std::ptr::NonNull;
88+
/// let dangling: *const u8 = NonNull::dangling().as_ptr();
89+
/// assert_eq!(<*const u8>::from_bits(1), dangling);
90+
/// ```
91+
#[unstable(feature = "ptr_to_from_bits", issue = "91126")]
92+
pub fn from_bits(bits: usize) -> Self
93+
where
94+
T: Sized,
95+
{
96+
bits as Self
97+
}
98+
5199
/// Decompose a (possibly wide) pointer into its address and metadata components.
52100
///
53101
/// The pointer can be later reconstructed with [`from_raw_parts`].

library/core/src/ptr/mut_ptr.rs

+49
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,55 @@ impl<T: ?Sized> *mut T {
4747
self as _
4848
}
4949

50+
/// Casts a pointer to its raw bits.
51+
///
52+
/// This is equivalent to `as usize`, but is more specific to enhance readability.
53+
/// The inverse method is [`from_bits`](#method.from_bits-1).
54+
///
55+
/// In particular, `*p as usize` and `p as usize` will both compile for
56+
/// pointers to numeric types but do very different things, so using this
57+
/// helps emphasize that reading the bits was intentional.
58+
///
59+
/// # Examples
60+
///
61+
/// ```
62+
/// #![feature(ptr_to_from_bits)]
63+
/// let mut array = [13, 42];
64+
/// let mut it = array.iter_mut();
65+
/// let p0: *mut i32 = it.next().unwrap();
66+
/// assert_eq!(<*mut _>::from_bits(p0.to_bits()), p0);
67+
/// let p1: *mut i32 = it.next().unwrap();
68+
/// assert_eq!(p1.to_bits() - p0.to_bits(), 4);
69+
/// ```
70+
#[unstable(feature = "ptr_to_from_bits", issue = "91126")]
71+
pub fn to_bits(self) -> usize
72+
where
73+
T: Sized,
74+
{
75+
self as usize
76+
}
77+
78+
/// Creates a pointer from its raw bits.
79+
///
80+
/// This is equivalent to `as *mut T`, but is more specific to enhance readability.
81+
/// The inverse method is [`to_bits`](#method.to_bits-1).
82+
///
83+
/// # Examples
84+
///
85+
/// ```
86+
/// #![feature(ptr_to_from_bits)]
87+
/// use std::ptr::NonNull;
88+
/// let dangling: *mut u8 = NonNull::dangling().as_ptr();
89+
/// assert_eq!(<*mut u8>::from_bits(1), dangling);
90+
/// ```
91+
#[unstable(feature = "ptr_to_from_bits", issue = "91126")]
92+
pub fn from_bits(bits: usize) -> Self
93+
where
94+
T: Sized,
95+
{
96+
bits as Self
97+
}
98+
5099
/// Decompose a (possibly wide) pointer into its address and metadata components.
51100
///
52101
/// The pointer can be later reconstructed with [`from_raw_parts_mut`].

0 commit comments

Comments
 (0)