Skip to content

Commit d920f1f

Browse files
authored
Rollup merge of rust-lang#47631 - SimonSapin:nonnull, r=alexcrichton
Add some APIs to ptr::NonNull and fix `since` attributes This is a follow-up to its stabilization in rust-lang#46952. Tracking issue: rust-lang#27730. * These trait impls are insta-stable: `Hash`, `PartialEq`, `Eq`, `PartialOrd` and `Ord`. * The new `cast<U>() -> NonNull<U>` method is `#[unstable]`. It was proposed in rust-lang#46952 (comment).
2 parents aee2255 + b8ffc8a commit d920f1f

File tree

2 files changed

+64
-25
lines changed

2 files changed

+64
-25
lines changed

src/libcore/ptr.rs

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,7 +2461,7 @@ impl<'a, T: ?Sized> From<NonNull<T>> for Unique<T> {
24612461
}
24622462

24632463
/// Previous name of `NonNull`.
2464-
#[rustc_deprecated(since = "1.24", reason = "renamed to `NonNull`")]
2464+
#[rustc_deprecated(since = "1.25.0", reason = "renamed to `NonNull`")]
24652465
#[unstable(feature = "shared", issue = "27730")]
24662466
pub type Shared<T> = NonNull<T>;
24672467

@@ -2482,34 +2482,27 @@ pub type Shared<T> = NonNull<T>;
24822482
/// Usually this won't be necessary; covariance is correct for most safe abstractions,
24832483
/// such as Box, Rc, Arc, Vec, and LinkedList. This is the case because they
24842484
/// provide a public API that follows the normal shared XOR mutable rules of Rust.
2485-
#[stable(feature = "nonnull", since = "1.24.0")]
2485+
#[stable(feature = "nonnull", since = "1.25.0")]
24862486
pub struct NonNull<T: ?Sized> {
24872487
pointer: NonZero<*const T>,
24882488
}
24892489

2490-
#[stable(feature = "nonnull", since = "1.24.0")]
2491-
impl<T: ?Sized> fmt::Debug for NonNull<T> {
2492-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2493-
fmt::Pointer::fmt(&self.as_ptr(), f)
2494-
}
2495-
}
2496-
24972490
/// `NonNull` pointers are not `Send` because the data they reference may be aliased.
24982491
// NB: This impl is unnecessary, but should provide better error messages.
2499-
#[stable(feature = "nonnull", since = "1.24.0")]
2492+
#[stable(feature = "nonnull", since = "1.25.0")]
25002493
impl<T: ?Sized> !Send for NonNull<T> { }
25012494

25022495
/// `NonNull` pointers are not `Sync` because the data they reference may be aliased.
25032496
// NB: This impl is unnecessary, but should provide better error messages.
2504-
#[stable(feature = "nonnull", since = "1.24.0")]
2497+
#[stable(feature = "nonnull", since = "1.25.0")]
25052498
impl<T: ?Sized> !Sync for NonNull<T> { }
25062499

25072500
impl<T: Sized> NonNull<T> {
25082501
/// Creates a new `NonNull` that is dangling, but well-aligned.
25092502
///
25102503
/// This is useful for initializing types which lazily allocate, like
25112504
/// `Vec::new` does.
2512-
#[stable(feature = "nonnull", since = "1.24.0")]
2505+
#[stable(feature = "nonnull", since = "1.25.0")]
25132506
pub fn dangling() -> Self {
25142507
unsafe {
25152508
let ptr = mem::align_of::<T>() as *mut T;
@@ -2524,19 +2517,19 @@ impl<T: ?Sized> NonNull<T> {
25242517
/// # Safety
25252518
///
25262519
/// `ptr` must be non-null.
2527-
#[stable(feature = "nonnull", since = "1.24.0")]
2520+
#[stable(feature = "nonnull", since = "1.25.0")]
25282521
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
25292522
NonNull { pointer: NonZero::new_unchecked(ptr) }
25302523
}
25312524

25322525
/// Creates a new `NonNull` if `ptr` is non-null.
2533-
#[stable(feature = "nonnull", since = "1.24.0")]
2526+
#[stable(feature = "nonnull", since = "1.25.0")]
25342527
pub fn new(ptr: *mut T) -> Option<Self> {
25352528
NonZero::new(ptr as *const T).map(|nz| NonNull { pointer: nz })
25362529
}
25372530

25382531
/// Acquires the underlying `*mut` pointer.
2539-
#[stable(feature = "nonnull", since = "1.24.0")]
2532+
#[stable(feature = "nonnull", since = "1.25.0")]
25402533
pub fn as_ptr(self) -> *mut T {
25412534
self.pointer.get() as *mut T
25422535
}
@@ -2546,7 +2539,7 @@ impl<T: ?Sized> NonNull<T> {
25462539
/// The resulting lifetime is bound to self so this behaves "as if"
25472540
/// it were actually an instance of T that is getting borrowed. If a longer
25482541
/// (unbound) lifetime is needed, use `&*my_ptr.as_ptr()`.
2549-
#[stable(feature = "nonnull", since = "1.24.0")]
2542+
#[stable(feature = "nonnull", since = "1.25.0")]
25502543
pub unsafe fn as_ref(&self) -> &T {
25512544
&*self.as_ptr()
25522545
}
@@ -2556,47 +2549,93 @@ impl<T: ?Sized> NonNull<T> {
25562549
/// The resulting lifetime is bound to self so this behaves "as if"
25572550
/// it were actually an instance of T that is getting borrowed. If a longer
25582551
/// (unbound) lifetime is needed, use `&mut *my_ptr.as_ptr()`.
2559-
#[stable(feature = "nonnull", since = "1.24.0")]
2552+
#[stable(feature = "nonnull", since = "1.25.0")]
25602553
pub unsafe fn as_mut(&mut self) -> &mut T {
25612554
&mut *self.as_ptr()
25622555
}
2556+
2557+
/// Cast to a pointer of another type
2558+
#[unstable(feature = "nonnull_cast", issue = "47653")]
2559+
pub fn cast<U>(self) -> NonNull<U> {
2560+
unsafe {
2561+
NonNull::new_unchecked(self.as_ptr() as *mut U)
2562+
}
2563+
}
25632564
}
25642565

2565-
#[stable(feature = "nonnull", since = "1.24.0")]
2566+
#[stable(feature = "nonnull", since = "1.25.0")]
25662567
impl<T: ?Sized> Clone for NonNull<T> {
25672568
fn clone(&self) -> Self {
25682569
*self
25692570
}
25702571
}
25712572

2572-
#[stable(feature = "nonnull", since = "1.24.0")]
2573+
#[stable(feature = "nonnull", since = "1.25.0")]
25732574
impl<T: ?Sized> Copy for NonNull<T> { }
25742575

2575-
#[stable(feature = "nonnull", since = "1.24.0")]
2576+
#[stable(feature = "nonnull", since = "1.25.0")]
25762577
impl<T: ?Sized, U: ?Sized> CoerceUnsized<NonNull<U>> for NonNull<T> where T: Unsize<U> { }
25772578

2578-
#[stable(feature = "nonnull", since = "1.24.0")]
2579+
#[stable(feature = "nonnull", since = "1.25.0")]
2580+
impl<T: ?Sized> fmt::Debug for NonNull<T> {
2581+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2582+
fmt::Pointer::fmt(&self.as_ptr(), f)
2583+
}
2584+
}
2585+
2586+
#[stable(feature = "nonnull", since = "1.25.0")]
25792587
impl<T: ?Sized> fmt::Pointer for NonNull<T> {
25802588
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25812589
fmt::Pointer::fmt(&self.as_ptr(), f)
25822590
}
25832591
}
25842592

2585-
#[stable(feature = "nonnull", since = "1.24.0")]
2593+
#[stable(feature = "nonnull", since = "1.25.0")]
2594+
impl<T: ?Sized> Eq for NonNull<T> {}
2595+
2596+
#[stable(feature = "nonnull", since = "1.25.0")]
2597+
impl<T: ?Sized> PartialEq for NonNull<T> {
2598+
fn eq(&self, other: &Self) -> bool {
2599+
self.as_ptr() == other.as_ptr()
2600+
}
2601+
}
2602+
2603+
#[stable(feature = "nonnull", since = "1.25.0")]
2604+
impl<T: ?Sized> Ord for NonNull<T> {
2605+
fn cmp(&self, other: &Self) -> Ordering {
2606+
self.as_ptr().cmp(&other.as_ptr())
2607+
}
2608+
}
2609+
2610+
#[stable(feature = "nonnull", since = "1.25.0")]
2611+
impl<T: ?Sized> PartialOrd for NonNull<T> {
2612+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
2613+
self.as_ptr().partial_cmp(&other.as_ptr())
2614+
}
2615+
}
2616+
2617+
#[stable(feature = "nonnull", since = "1.25.0")]
2618+
impl<T: ?Sized> hash::Hash for NonNull<T> {
2619+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
2620+
self.as_ptr().hash(state)
2621+
}
2622+
}
2623+
2624+
#[stable(feature = "nonnull", since = "1.25.0")]
25862625
impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
25872626
fn from(unique: Unique<T>) -> Self {
25882627
NonNull { pointer: unique.pointer }
25892628
}
25902629
}
25912630

2592-
#[stable(feature = "nonnull", since = "1.24.0")]
2631+
#[stable(feature = "nonnull", since = "1.25.0")]
25932632
impl<'a, T: ?Sized> From<&'a mut T> for NonNull<T> {
25942633
fn from(reference: &'a mut T) -> Self {
25952634
NonNull { pointer: NonZero::from(reference) }
25962635
}
25972636
}
25982637

2599-
#[stable(feature = "nonnull", since = "1.24.0")]
2638+
#[stable(feature = "nonnull", since = "1.25.0")]
26002639
impl<'a, T: ?Sized> From<&'a T> for NonNull<T> {
26012640
fn from(reference: &'a T) -> Self {
26022641
NonNull { pointer: NonZero::from(reference) }

src/libstd/panic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl<T: RefUnwindSafe + ?Sized> UnwindSafe for *const T {}
198198
impl<T: RefUnwindSafe + ?Sized> UnwindSafe for *mut T {}
199199
#[unstable(feature = "ptr_internals", issue = "0")]
200200
impl<T: UnwindSafe + ?Sized> UnwindSafe for Unique<T> {}
201-
#[stable(feature = "nonnull", since = "1.24.0")]
201+
#[stable(feature = "nonnull", since = "1.25.0")]
202202
impl<T: RefUnwindSafe + ?Sized> UnwindSafe for NonNull<T> {}
203203
#[stable(feature = "catch_unwind", since = "1.9.0")]
204204
impl<T: ?Sized> UnwindSafe for Mutex<T> {}

0 commit comments

Comments
 (0)