Skip to content

Commit 309085e

Browse files
RalfJunggitbot
authored and
gitbot
committed
add missing provenance APIs on NonNull
1 parent d74caf1 commit 309085e

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

core/src/ptr/non_null.rs

+45-3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ impl<T: ?Sized> !Send for NonNull<T> {}
8585
impl<T: ?Sized> !Sync for NonNull<T> {}
8686

8787
impl<T: Sized> NonNull<T> {
88+
/// Creates a pointer with the given address and no [provenance][crate::ptr#provenance].
89+
///
90+
/// For more details, see the equivalent method on a raw pointer, [`ptr::without_provenance_mut`].
91+
///
92+
/// This is a [Strict Provenance][crate::ptr#strict-provenance] API.
93+
#[unstable(feature = "nonnull_provenance", issue = "135243")]
94+
pub const fn without_provenance(addr: NonZero<usize>) -> Self {
95+
// SAFETY: we know `addr` is non-zero.
96+
unsafe {
97+
let ptr = crate::ptr::without_provenance_mut(addr.get());
98+
NonNull::new_unchecked(ptr)
99+
}
100+
}
101+
88102
/// Creates a new `NonNull` that is dangling, but well-aligned.
89103
///
90104
/// This is useful for initializing types which lazily allocate, like
@@ -116,6 +130,21 @@ impl<T: Sized> NonNull<T> {
116130
}
117131
}
118132

133+
/// Converts an address back to a mutable pointer, picking up some previously 'exposed'
134+
/// [provenance][crate::ptr#provenance].
135+
///
136+
/// For more details, see the equivalent method on a raw pointer, [`ptr::with_exposed_provenance_mut`].
137+
///
138+
/// This is an [Exposed Provenance][crate::ptr#exposed-provenance] API.
139+
#[unstable(feature = "nonnull_provenance", issue = "135243")]
140+
pub fn with_exposed_provenance(addr: NonZero<usize>) -> Self {
141+
// SAFETY: we know `addr` is non-zero.
142+
unsafe {
143+
let ptr = crate::ptr::with_exposed_provenance_mut(addr.get());
144+
NonNull::new_unchecked(ptr)
145+
}
146+
}
147+
119148
/// Returns a shared references to the value. In contrast to [`as_ref`], this does not require
120149
/// that the value has to be initialized.
121150
///
@@ -282,7 +311,7 @@ impl<T: ?Sized> NonNull<T> {
282311

283312
/// Gets the "address" portion of the pointer.
284313
///
285-
/// For more details see the equivalent method on a raw pointer, [`pointer::addr`].
314+
/// For more details, see the equivalent method on a raw pointer, [`pointer::addr`].
286315
///
287316
/// This is a [Strict Provenance][crate::ptr#strict-provenance] API.
288317
#[must_use]
@@ -294,10 +323,23 @@ impl<T: ?Sized> NonNull<T> {
294323
unsafe { NonZero::new_unchecked(self.as_ptr().addr()) }
295324
}
296325

326+
/// Exposes the ["provenance"][crate::ptr#provenance] part of the pointer for future use in
327+
/// [`with_exposed_provenance`][NonNull::with_exposed_provenance] and returns the "address" portion.
328+
///
329+
/// For more details, see the equivalent method on a raw pointer, [`pointer::expose_provenance`].
330+
///
331+
/// This is an [Exposed Provenance][crate::ptr#exposed-provenance] API.
332+
#[unstable(feature = "nonnull_provenance", issue = "135243")]
333+
pub fn expose_provenance(self) -> NonZero<usize> {
334+
// SAFETY: The pointer is guaranteed by the type to be non-null,
335+
// meaning that the address will be non-zero.
336+
unsafe { NonZero::new_unchecked(self.as_ptr().expose_provenance()) }
337+
}
338+
297339
/// Creates a new pointer with the given address and the [provenance][crate::ptr#provenance] of
298340
/// `self`.
299341
///
300-
/// For more details see the equivalent method on a raw pointer, [`pointer::with_addr`].
342+
/// For more details, see the equivalent method on a raw pointer, [`pointer::with_addr`].
301343
///
302344
/// This is a [Strict Provenance][crate::ptr#strict-provenance] API.
303345
#[must_use]
@@ -311,7 +353,7 @@ impl<T: ?Sized> NonNull<T> {
311353
/// Creates a new pointer by mapping `self`'s address to a new one, preserving the
312354
/// [provenance][crate::ptr#provenance] of `self`.
313355
///
314-
/// For more details see the equivalent method on a raw pointer, [`pointer::map_addr`].
356+
/// For more details, see the equivalent method on a raw pointer, [`pointer::map_addr`].
315357
///
316358
/// This is a [Strict Provenance][crate::ptr#strict-provenance] API.
317359
#[must_use]

0 commit comments

Comments
 (0)