1
1
use crate :: convert:: From ;
2
2
use crate :: fmt;
3
3
use crate :: marker:: { PhantomData , Unsize } ;
4
- use crate :: mem;
5
4
use crate :: ops:: { CoerceUnsized , DispatchFromDyn } ;
5
+ use crate :: ptr:: NonNull ;
6
6
7
7
/// A wrapper around a raw non-null `*mut T` that indicates that the possessor
8
8
/// of this wrapper owns the referent. Useful for building abstractions like
@@ -32,9 +32,8 @@ use crate::ops::{CoerceUnsized, DispatchFromDyn};
32
32
) ]
33
33
#[ doc( hidden) ]
34
34
#[ repr( transparent) ]
35
- #[ rustc_layout_scalar_valid_range_start( 1 ) ]
36
35
pub struct Unique < T : ?Sized > {
37
- pointer : * const T ,
36
+ pointer : NonNull < T > ,
38
37
// NOTE: this marker has no consequences for variance, but is necessary
39
38
// for dropck to understand that we logically own a `T`.
40
39
//
@@ -71,9 +70,7 @@ impl<T: Sized> Unique<T> {
71
70
#[ must_use]
72
71
#[ inline]
73
72
pub const fn dangling ( ) -> Self {
74
- // SAFETY: mem::align_of() returns a valid, non-null pointer. The
75
- // conditions to call new_unchecked() are thus respected.
76
- unsafe { Unique :: new_unchecked ( crate :: ptr:: invalid_mut :: < T > ( mem:: align_of :: < T > ( ) ) ) }
73
+ Self :: from ( NonNull :: dangling ( ) )
77
74
}
78
75
}
79
76
@@ -87,15 +84,14 @@ impl<T: ?Sized> Unique<T> {
87
84
#[ inline]
88
85
pub const unsafe fn new_unchecked ( ptr : * mut T ) -> Self {
89
86
// SAFETY: the caller must guarantee that `ptr` is non-null.
90
- unsafe { Unique { pointer : ptr as _ , _marker : PhantomData } }
87
+ unsafe { Unique { pointer : NonNull :: new_unchecked ( ptr) , _marker : PhantomData } }
91
88
}
92
89
93
90
/// Creates a new `Unique` if `ptr` is non-null.
94
91
#[ inline]
95
92
pub const fn new ( ptr : * mut T ) -> Option < Self > {
96
- if !ptr. is_null ( ) {
97
- // SAFETY: The pointer has already been checked and is not null.
98
- Some ( unsafe { Unique { pointer : ptr as _ , _marker : PhantomData } } )
93
+ if let Some ( pointer) = NonNull :: new ( ptr) {
94
+ Some ( Unique { pointer, _marker : PhantomData } )
99
95
} else {
100
96
None
101
97
}
@@ -105,7 +101,7 @@ impl<T: ?Sized> Unique<T> {
105
101
#[ must_use = "`self` will be dropped if the result is not used" ]
106
102
#[ inline]
107
103
pub const fn as_ptr ( self ) -> * mut T {
108
- self . pointer as * mut T
104
+ self . pointer . as_ptr ( )
109
105
}
110
106
111
107
/// Dereferences the content.
@@ -118,7 +114,7 @@ impl<T: ?Sized> Unique<T> {
118
114
pub const unsafe fn as_ref ( & self ) -> & T {
119
115
// SAFETY: the caller must guarantee that `self` meets all the
120
116
// requirements for a reference.
121
- unsafe { & * self . as_ptr ( ) }
117
+ unsafe { self . pointer . as_ref ( ) }
122
118
}
123
119
124
120
/// Mutably dereferences the content.
@@ -131,17 +127,14 @@ impl<T: ?Sized> Unique<T> {
131
127
pub const unsafe fn as_mut ( & mut self ) -> & mut T {
132
128
// SAFETY: the caller must guarantee that `self` meets all the
133
129
// requirements for a mutable reference.
134
- unsafe { & mut * self . as_ptr ( ) }
130
+ unsafe { self . pointer . as_mut ( ) }
135
131
}
136
132
137
133
/// Casts to a pointer of another type.
138
134
#[ must_use = "`self` will be dropped if the result is not used" ]
139
135
#[ inline]
140
136
pub const fn cast < U > ( self ) -> Unique < U > {
141
- // SAFETY: Unique::new_unchecked() creates a new unique and needs
142
- // the given pointer to not be null.
143
- // Since we are passing self as a pointer, it cannot be null.
144
- unsafe { Unique :: new_unchecked ( self . as_ptr ( ) as * mut U ) }
137
+ Unique :: from ( self . pointer . cast ( ) )
145
138
}
146
139
}
147
140
@@ -184,7 +177,17 @@ impl<T: ?Sized> const From<&mut T> for Unique<T> {
184
177
/// This conversion is infallible since references cannot be null.
185
178
#[ inline]
186
179
fn from ( reference : & mut T ) -> Self {
187
- // SAFETY: A mutable reference cannot be null
188
- unsafe { Unique { pointer : reference as * mut T , _marker : PhantomData } }
180
+ Self :: from ( NonNull :: from ( reference) )
181
+ }
182
+ }
183
+
184
+ #[ unstable( feature = "ptr_internals" , issue = "none" ) ]
185
+ impl < T : ?Sized > const From < NonNull < T > > for Unique < T > {
186
+ /// Converts a `NonNull<T>` to a `Unique<T>`.
187
+ ///
188
+ /// This conversion is infallible since `NonNull` cannot be null.
189
+ #[ inline]
190
+ fn from ( pointer : NonNull < T > ) -> Self {
191
+ Unique { pointer, _marker : PhantomData }
189
192
}
190
193
}
0 commit comments