91
91
use mem;
92
92
use clone:: Clone ;
93
93
use intrinsics;
94
+ use ops:: Deref ;
94
95
use option:: Option :: { self , Some , None } ;
95
- use marker:: { self , Send , Sized , Sync } ;
96
+ use marker:: { PhantomData , Send , Sized , Sync } ;
97
+ use nonzero:: NonZero ;
96
98
97
99
use cmp:: { PartialEq , Eq , Ord , PartialOrd } ;
98
100
use cmp:: Ordering :: { self , Less , Equal , Greater } ;
@@ -517,15 +519,16 @@ impl<T> PartialOrd for *mut T {
517
519
518
520
/// A wrapper around a raw `*mut T` that indicates that the possessor
519
521
/// of this wrapper owns the referent. This in turn implies that the
520
- /// `Unique<T>` is `Send`/`Sync` if `T` is `Send`/`Sync`, unlike a
521
- /// raw `*mut T` (which conveys no particular ownership semantics).
522
- /// Useful for building abstractions like `Vec<T>` or `Box<T>`, which
522
+ /// `Unique<T>` is `Send`/`Sync` if `T` is `Send`/`Sync`, unlike a raw
523
+ /// `*mut T` (which conveys no particular ownership semantics). It
524
+ /// also implies that the referent of the pointer should not be
525
+ /// modified without a unique path to the `Unique` reference. Useful
526
+ /// for building abstractions like `Vec<T>` or `Box<T>`, which
523
527
/// internally use raw pointers to manage the memory that they own.
524
528
#[ unstable( feature = "core" , reason = "recently added to this module" ) ]
525
- pub struct Unique < T : ?Sized > {
526
- /// The wrapped `*mut T`.
527
- pub ptr : * mut T ,
528
- _own : marker:: PhantomData < T > ,
529
+ pub struct Unique < T : ?Sized > {
530
+ pointer : NonZero < * const T > ,
531
+ _marker : PhantomData < T > ,
529
532
}
530
533
531
534
/// `Unique` pointers are `Send` if `T` is `Send` because the data they
@@ -542,25 +545,34 @@ unsafe impl<T: Send + ?Sized> Send for Unique<T> { }
542
545
#[ unstable( feature = "core" , reason = "recently added to this module" ) ]
543
546
unsafe impl < T : Sync + ?Sized > Sync for Unique < T > { }
544
547
545
- impl < T > Unique < T > {
546
- /// Returns a null Unique.
548
+ impl < T : ? Sized > Unique < T > {
549
+ /// Create a new ` Unique` .
547
550
#[ unstable( feature = "core" ,
548
551
reason = "recently added to this module" ) ]
549
- pub fn null ( ) -> Unique < T > {
550
- Unique ( null_mut ( ) )
552
+ pub unsafe fn new ( ptr : * mut T ) -> Unique < T > {
553
+ Unique { pointer : NonZero :: new ( ptr as * const T ) , _marker : PhantomData }
551
554
}
552
555
553
- /// Return an (unsafe) pointer into the memory owned by `self` .
556
+ /// Dereference the content .
554
557
#[ unstable( feature = "core" ,
555
558
reason = "recently added to this module" ) ]
556
- pub unsafe fn offset ( self , offset : isize ) -> * mut T {
557
- self . ptr . offset ( offset)
559
+ pub unsafe fn get ( & self ) -> & T {
560
+ & * * self . pointer
561
+ }
562
+
563
+ /// Mutably dereference the content.
564
+ #[ unstable( feature = "core" ,
565
+ reason = "recently added to this module" ) ]
566
+ pub unsafe fn get_mut ( & mut self ) -> & mut T {
567
+ & mut * * * self
558
568
}
559
569
}
560
570
561
- /// Creates a `Unique` wrapped around `ptr`, taking ownership of the
562
- /// data referenced by `ptr`.
563
- #[ allow( non_snake_case) ]
564
- pub fn Unique < T : ?Sized > ( ptr : * mut T ) -> Unique < T > {
565
- Unique { ptr : ptr, _own : marker:: PhantomData }
571
+ impl < T : ?Sized > Deref for Unique < T > {
572
+ type Target = * mut T ;
573
+
574
+ #[ inline]
575
+ fn deref < ' a > ( & ' a self ) -> & ' a * mut T {
576
+ unsafe { mem:: transmute ( & * self . pointer ) }
577
+ }
566
578
}
0 commit comments