@@ -85,6 +85,20 @@ impl<T: ?Sized> !Send for NonNull<T> {}
85
85
impl < T : ?Sized > !Sync for NonNull < T > { }
86
86
87
87
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
+
88
102
/// Creates a new `NonNull` that is dangling, but well-aligned.
89
103
///
90
104
/// This is useful for initializing types which lazily allocate, like
@@ -116,6 +130,21 @@ impl<T: Sized> NonNull<T> {
116
130
}
117
131
}
118
132
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
+
119
148
/// Returns a shared references to the value. In contrast to [`as_ref`], this does not require
120
149
/// that the value has to be initialized.
121
150
///
@@ -282,7 +311,7 @@ impl<T: ?Sized> NonNull<T> {
282
311
283
312
/// Gets the "address" portion of the pointer.
284
313
///
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`].
286
315
///
287
316
/// This is a [Strict Provenance][crate::ptr#strict-provenance] API.
288
317
#[ must_use]
@@ -294,10 +323,23 @@ impl<T: ?Sized> NonNull<T> {
294
323
unsafe { NonZero :: new_unchecked ( self . as_ptr ( ) . addr ( ) ) }
295
324
}
296
325
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
+
297
339
/// Creates a new pointer with the given address and the [provenance][crate::ptr#provenance] of
298
340
/// `self`.
299
341
///
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`].
301
343
///
302
344
/// This is a [Strict Provenance][crate::ptr#strict-provenance] API.
303
345
#[ must_use]
@@ -311,7 +353,7 @@ impl<T: ?Sized> NonNull<T> {
311
353
/// Creates a new pointer by mapping `self`'s address to a new one, preserving the
312
354
/// [provenance][crate::ptr#provenance] of `self`.
313
355
///
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`].
315
357
///
316
358
/// This is a [Strict Provenance][crate::ptr#strict-provenance] API.
317
359
#[ must_use]
0 commit comments