1- use crate :: marker:: Destruct ;
1+ use crate :: cmp:: Ordering ;
2+ use crate :: hash:: { Hash , Hasher } ;
3+ use crate :: marker:: { Destruct , StructuralPartialEq } ;
4+ use crate :: mem:: MaybeDangling ;
25use crate :: ops:: { Deref , DerefMut , DerefPure } ;
36use crate :: ptr;
47
@@ -152,11 +155,11 @@ use crate::ptr;
152155/// [`MaybeUninit`]: crate::mem::MaybeUninit
153156#[ stable( feature = "manually_drop" , since = "1.20.0" ) ]
154157#[ lang = "manually_drop" ]
155- #[ derive( Copy , Clone , Debug , Default , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
158+ #[ derive( Copy , Clone , Debug , Default ) ]
156159#[ repr( transparent) ]
157160#[ rustc_pub_transparent]
158161pub struct ManuallyDrop < T : ?Sized > {
159- value : T ,
162+ value : MaybeDangling < T > ,
160163}
161164
162165impl < T > ManuallyDrop < T > {
@@ -179,7 +182,7 @@ impl<T> ManuallyDrop<T> {
179182 #[ rustc_const_stable( feature = "const_manually_drop" , since = "1.32.0" ) ]
180183 #[ inline( always) ]
181184 pub const fn new ( value : T ) -> ManuallyDrop < T > {
182- ManuallyDrop { value }
185+ ManuallyDrop { value : MaybeDangling :: new ( value ) }
183186 }
184187
185188 /// Extracts the value from the `ManuallyDrop` container.
@@ -197,7 +200,7 @@ impl<T> ManuallyDrop<T> {
197200 #[ rustc_const_stable( feature = "const_manually_drop" , since = "1.32.0" ) ]
198201 #[ inline( always) ]
199202 pub const fn into_inner ( slot : ManuallyDrop < T > ) -> T {
200- slot. value
203+ slot. value . into_inner ( )
201204 }
202205
203206 /// Takes the value from the `ManuallyDrop<T>` container out.
@@ -222,7 +225,7 @@ impl<T> ManuallyDrop<T> {
222225 pub const unsafe fn take ( slot : & mut ManuallyDrop < T > ) -> T {
223226 // SAFETY: we are reading from a reference, which is guaranteed
224227 // to be valid for reads.
225- unsafe { ptr:: read ( & slot. value ) }
228+ unsafe { ptr:: read ( slot. value . as_ref ( ) ) }
226229 }
227230}
228231
@@ -259,7 +262,7 @@ impl<T: ?Sized> ManuallyDrop<T> {
259262 // SAFETY: we are dropping the value pointed to by a mutable reference
260263 // which is guaranteed to be valid for writes.
261264 // It is up to the caller to make sure that `slot` isn't dropped again.
262- unsafe { ptr:: drop_in_place ( & mut slot. value ) }
265+ unsafe { ptr:: drop_in_place ( slot. value . as_mut ( ) ) }
263266 }
264267}
265268
@@ -269,7 +272,7 @@ impl<T: ?Sized> const Deref for ManuallyDrop<T> {
269272 type Target = T ;
270273 #[ inline( always) ]
271274 fn deref ( & self ) -> & T {
272- & self . value
275+ self . value . as_ref ( )
273276 }
274277}
275278
@@ -278,9 +281,43 @@ impl<T: ?Sized> const Deref for ManuallyDrop<T> {
278281impl < T : ?Sized > const DerefMut for ManuallyDrop < T > {
279282 #[ inline( always) ]
280283 fn deref_mut ( & mut self ) -> & mut T {
281- & mut self . value
284+ self . value . as_mut ( )
282285 }
283286}
284287
285288#[ unstable( feature = "deref_pure_trait" , issue = "87121" ) ]
286289unsafe impl < T : ?Sized > DerefPure for ManuallyDrop < T > { }
290+
291+ #[ stable( feature = "manually_drop" , since = "1.20.0" ) ]
292+ impl < T : ?Sized + Eq > Eq for ManuallyDrop < T > { }
293+
294+ #[ stable( feature = "manually_drop" , since = "1.20.0" ) ]
295+ impl < T : ?Sized + PartialEq > PartialEq for ManuallyDrop < T > {
296+ fn eq ( & self , other : & Self ) -> bool {
297+ self . value . as_ref ( ) . eq ( other. value . as_ref ( ) )
298+ }
299+ }
300+
301+ #[ stable( feature = "manually_drop" , since = "1.20.0" ) ]
302+ impl < T : ?Sized > StructuralPartialEq for ManuallyDrop < T > { }
303+
304+ #[ stable( feature = "manually_drop" , since = "1.20.0" ) ]
305+ impl < T : ?Sized + Ord > Ord for ManuallyDrop < T > {
306+ fn cmp ( & self , other : & Self ) -> Ordering {
307+ self . value . as_ref ( ) . cmp ( other. value . as_ref ( ) )
308+ }
309+ }
310+
311+ #[ stable( feature = "manually_drop" , since = "1.20.0" ) ]
312+ impl < T : ?Sized + PartialOrd > PartialOrd for ManuallyDrop < T > {
313+ fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
314+ self . value . as_ref ( ) . partial_cmp ( other. value . as_ref ( ) )
315+ }
316+ }
317+
318+ #[ stable( feature = "manually_drop" , since = "1.20.0" ) ]
319+ impl < T : ?Sized + Hash > Hash for ManuallyDrop < T > {
320+ fn hash < H : Hasher > ( & self , state : & mut H ) {
321+ self . value . as_ref ( ) . hash ( state) ;
322+ }
323+ }
0 commit comments