@@ -2461,7 +2461,7 @@ impl<'a, T: ?Sized> From<NonNull<T>> for Unique<T> {
2461
2461
}
2462
2462
2463
2463
/// 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`" ) ]
2465
2465
#[ unstable( feature = "shared" , issue = "27730" ) ]
2466
2466
pub type Shared < T > = NonNull < T > ;
2467
2467
@@ -2482,34 +2482,27 @@ pub type Shared<T> = NonNull<T>;
2482
2482
/// Usually this won't be necessary; covariance is correct for most safe abstractions,
2483
2483
/// such as Box, Rc, Arc, Vec, and LinkedList. This is the case because they
2484
2484
/// 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" ) ]
2486
2486
pub struct NonNull < T : ?Sized > {
2487
2487
pointer : NonZero < * const T > ,
2488
2488
}
2489
2489
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
-
2497
2490
/// `NonNull` pointers are not `Send` because the data they reference may be aliased.
2498
2491
// 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" ) ]
2500
2493
impl < T : ?Sized > !Send for NonNull < T > { }
2501
2494
2502
2495
/// `NonNull` pointers are not `Sync` because the data they reference may be aliased.
2503
2496
// 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" ) ]
2505
2498
impl < T : ?Sized > !Sync for NonNull < T > { }
2506
2499
2507
2500
impl < T : Sized > NonNull < T > {
2508
2501
/// Creates a new `NonNull` that is dangling, but well-aligned.
2509
2502
///
2510
2503
/// This is useful for initializing types which lazily allocate, like
2511
2504
/// `Vec::new` does.
2512
- #[ stable( feature = "nonnull" , since = "1.24 .0" ) ]
2505
+ #[ stable( feature = "nonnull" , since = "1.25 .0" ) ]
2513
2506
pub fn dangling ( ) -> Self {
2514
2507
unsafe {
2515
2508
let ptr = mem:: align_of :: < T > ( ) as * mut T ;
@@ -2524,19 +2517,19 @@ impl<T: ?Sized> NonNull<T> {
2524
2517
/// # Safety
2525
2518
///
2526
2519
/// `ptr` must be non-null.
2527
- #[ stable( feature = "nonnull" , since = "1.24 .0" ) ]
2520
+ #[ stable( feature = "nonnull" , since = "1.25 .0" ) ]
2528
2521
pub const unsafe fn new_unchecked ( ptr : * mut T ) -> Self {
2529
2522
NonNull { pointer : NonZero :: new_unchecked ( ptr) }
2530
2523
}
2531
2524
2532
2525
/// 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" ) ]
2534
2527
pub fn new ( ptr : * mut T ) -> Option < Self > {
2535
2528
NonZero :: new ( ptr as * const T ) . map ( |nz| NonNull { pointer : nz } )
2536
2529
}
2537
2530
2538
2531
/// Acquires the underlying `*mut` pointer.
2539
- #[ stable( feature = "nonnull" , since = "1.24 .0" ) ]
2532
+ #[ stable( feature = "nonnull" , since = "1.25 .0" ) ]
2540
2533
pub fn as_ptr ( self ) -> * mut T {
2541
2534
self . pointer . get ( ) as * mut T
2542
2535
}
@@ -2546,7 +2539,7 @@ impl<T: ?Sized> NonNull<T> {
2546
2539
/// The resulting lifetime is bound to self so this behaves "as if"
2547
2540
/// it were actually an instance of T that is getting borrowed. If a longer
2548
2541
/// (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" ) ]
2550
2543
pub unsafe fn as_ref ( & self ) -> & T {
2551
2544
& * self . as_ptr ( )
2552
2545
}
@@ -2556,47 +2549,93 @@ impl<T: ?Sized> NonNull<T> {
2556
2549
/// The resulting lifetime is bound to self so this behaves "as if"
2557
2550
/// it were actually an instance of T that is getting borrowed. If a longer
2558
2551
/// (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" ) ]
2560
2553
pub unsafe fn as_mut ( & mut self ) -> & mut T {
2561
2554
& mut * self . as_ptr ( )
2562
2555
}
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
+ }
2563
2564
}
2564
2565
2565
- #[ stable( feature = "nonnull" , since = "1.24 .0" ) ]
2566
+ #[ stable( feature = "nonnull" , since = "1.25 .0" ) ]
2566
2567
impl < T : ?Sized > Clone for NonNull < T > {
2567
2568
fn clone ( & self ) -> Self {
2568
2569
* self
2569
2570
}
2570
2571
}
2571
2572
2572
- #[ stable( feature = "nonnull" , since = "1.24 .0" ) ]
2573
+ #[ stable( feature = "nonnull" , since = "1.25 .0" ) ]
2573
2574
impl < T : ?Sized > Copy for NonNull < T > { }
2574
2575
2575
- #[ stable( feature = "nonnull" , since = "1.24 .0" ) ]
2576
+ #[ stable( feature = "nonnull" , since = "1.25 .0" ) ]
2576
2577
impl < T : ?Sized , U : ?Sized > CoerceUnsized < NonNull < U > > for NonNull < T > where T : Unsize < U > { }
2577
2578
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" ) ]
2579
2587
impl < T : ?Sized > fmt:: Pointer for NonNull < T > {
2580
2588
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
2581
2589
fmt:: Pointer :: fmt ( & self . as_ptr ( ) , f)
2582
2590
}
2583
2591
}
2584
2592
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" ) ]
2586
2625
impl < T : ?Sized > From < Unique < T > > for NonNull < T > {
2587
2626
fn from ( unique : Unique < T > ) -> Self {
2588
2627
NonNull { pointer : unique. pointer }
2589
2628
}
2590
2629
}
2591
2630
2592
- #[ stable( feature = "nonnull" , since = "1.24 .0" ) ]
2631
+ #[ stable( feature = "nonnull" , since = "1.25 .0" ) ]
2593
2632
impl < ' a , T : ?Sized > From < & ' a mut T > for NonNull < T > {
2594
2633
fn from ( reference : & ' a mut T ) -> Self {
2595
2634
NonNull { pointer : NonZero :: from ( reference) }
2596
2635
}
2597
2636
}
2598
2637
2599
- #[ stable( feature = "nonnull" , since = "1.24 .0" ) ]
2638
+ #[ stable( feature = "nonnull" , since = "1.25 .0" ) ]
2600
2639
impl < ' a , T : ?Sized > From < & ' a T > for NonNull < T > {
2601
2640
fn from ( reference : & ' a T ) -> Self {
2602
2641
NonNull { pointer : NonZero :: from ( reference) }
0 commit comments