Skip to content

Commit 722e310

Browse files
committed
use MaybeDangling in ManuallyDrop
1 parent 4ea5cac commit 722e310

File tree

6 files changed

+75
-18
lines changed

6 files changed

+75
-18
lines changed

library/core/src/mem/manually_drop.rs

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
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;
25
use crate::ops::{Deref, DerefMut, DerefPure};
36
use 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]
158161
pub struct ManuallyDrop<T: ?Sized> {
159-
value: T,
162+
value: MaybeDangling<T>,
160163
}
161164

162165
impl<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> {
278281
impl<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")]
286289
unsafe 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+
}

tests/ui/async-await/future-sizes/async-awaiting-fut.stdout

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ print-type-size variant `Returned`: 0 bytes
77
print-type-size variant `Panicked`: 0 bytes
88
print-type-size type: `std::mem::ManuallyDrop<{async fn body of calls_fut<{async fn body of big_fut()}>()}>`: 3077 bytes, alignment: 1 bytes
99
print-type-size field `.value`: 3077 bytes
10+
print-type-size type: `std::mem::MaybeDangling<{async fn body of calls_fut<{async fn body of big_fut()}>()}>`: 3077 bytes, alignment: 1 bytes
11+
print-type-size field `.0`: 3077 bytes
1012
print-type-size type: `std::mem::MaybeUninit<{async fn body of calls_fut<{async fn body of big_fut()}>()}>`: 3077 bytes, alignment: 1 bytes
1113
print-type-size variant `MaybeUninit`: 3077 bytes
1214
print-type-size field `.uninit`: 0 bytes
@@ -36,6 +38,8 @@ print-type-size variant `Panicked`: 1025 bytes
3638
print-type-size upvar `.fut`: 1025 bytes
3739
print-type-size type: `std::mem::ManuallyDrop<{async fn body of big_fut()}>`: 1025 bytes, alignment: 1 bytes
3840
print-type-size field `.value`: 1025 bytes
41+
print-type-size type: `std::mem::MaybeDangling<{async fn body of big_fut()}>`: 1025 bytes, alignment: 1 bytes
42+
print-type-size field `.0`: 1025 bytes
3943
print-type-size type: `std::mem::MaybeUninit<{async fn body of big_fut()}>`: 1025 bytes, alignment: 1 bytes
4044
print-type-size variant `MaybeUninit`: 1025 bytes
4145
print-type-size field `.uninit`: 0 bytes
@@ -85,6 +89,10 @@ print-type-size type: `std::mem::ManuallyDrop<bool>`: 1 bytes, alignment: 1 byte
8589
print-type-size field `.value`: 1 bytes
8690
print-type-size type: `std::mem::ManuallyDrop<{async fn body of wait()}>`: 1 bytes, alignment: 1 bytes
8791
print-type-size field `.value`: 1 bytes
92+
print-type-size type: `std::mem::MaybeDangling<bool>`: 1 bytes, alignment: 1 bytes
93+
print-type-size field `.0`: 1 bytes
94+
print-type-size type: `std::mem::MaybeDangling<{async fn body of wait()}>`: 1 bytes, alignment: 1 bytes
95+
print-type-size field `.0`: 1 bytes
8896
print-type-size type: `std::mem::MaybeUninit<bool>`: 1 bytes, alignment: 1 bytes
8997
print-type-size variant `MaybeUninit`: 1 bytes
9098
print-type-size field `.uninit`: 0 bytes

tests/ui/async-await/future-sizes/large-arg.stdout

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ print-type-size variant `Returned`: 0 bytes
77
print-type-size variant `Panicked`: 0 bytes
88
print-type-size type: `std::mem::ManuallyDrop<{async fn body of a<[u8; 1024]>()}>`: 3075 bytes, alignment: 1 bytes
99
print-type-size field `.value`: 3075 bytes
10+
print-type-size type: `std::mem::MaybeDangling<{async fn body of a<[u8; 1024]>()}>`: 3075 bytes, alignment: 1 bytes
11+
print-type-size field `.0`: 3075 bytes
1012
print-type-size type: `std::mem::MaybeUninit<{async fn body of a<[u8; 1024]>()}>`: 3075 bytes, alignment: 1 bytes
1113
print-type-size variant `MaybeUninit`: 3075 bytes
1214
print-type-size field `.uninit`: 0 bytes
@@ -24,6 +26,8 @@ print-type-size variant `Panicked`: 1024 bytes
2426
print-type-size upvar `.t`: 1024 bytes
2527
print-type-size type: `std::mem::ManuallyDrop<{async fn body of b<[u8; 1024]>()}>`: 2050 bytes, alignment: 1 bytes
2628
print-type-size field `.value`: 2050 bytes
29+
print-type-size type: `std::mem::MaybeDangling<{async fn body of b<[u8; 1024]>()}>`: 2050 bytes, alignment: 1 bytes
30+
print-type-size field `.0`: 2050 bytes
2731
print-type-size type: `std::mem::MaybeUninit<{async fn body of b<[u8; 1024]>()}>`: 2050 bytes, alignment: 1 bytes
2832
print-type-size variant `MaybeUninit`: 2050 bytes
2933
print-type-size field `.uninit`: 0 bytes
@@ -41,6 +45,8 @@ print-type-size variant `Panicked`: 1024 bytes
4145
print-type-size upvar `.t`: 1024 bytes
4246
print-type-size type: `std::mem::ManuallyDrop<{async fn body of c<[u8; 1024]>()}>`: 1025 bytes, alignment: 1 bytes
4347
print-type-size field `.value`: 1025 bytes
48+
print-type-size type: `std::mem::MaybeDangling<{async fn body of c<[u8; 1024]>()}>`: 1025 bytes, alignment: 1 bytes
49+
print-type-size field `.0`: 1025 bytes
4450
print-type-size type: `std::mem::MaybeUninit<{async fn body of c<[u8; 1024]>()}>`: 1025 bytes, alignment: 1 bytes
4551
print-type-size variant `MaybeUninit`: 1025 bytes
4652
print-type-size field `.uninit`: 0 bytes

tests/ui/print_type_sizes/async.stdout

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ print-type-size variant `Panicked`: 8192 bytes
1212
print-type-size upvar `.arg`: 8192 bytes
1313
print-type-size type: `std::mem::ManuallyDrop<[u8; 8192]>`: 8192 bytes, alignment: 1 bytes
1414
print-type-size field `.value`: 8192 bytes
15+
print-type-size type: `std::mem::MaybeDangling<[u8; 8192]>`: 8192 bytes, alignment: 1 bytes
16+
print-type-size field `.0`: 8192 bytes
1517
print-type-size type: `std::mem::MaybeUninit<[u8; 8192]>`: 8192 bytes, alignment: 1 bytes
1618
print-type-size variant `MaybeUninit`: 8192 bytes
1719
print-type-size field `.uninit`: 0 bytes
@@ -47,6 +49,8 @@ print-type-size type: `std::ptr::NonNull<std::ptr::metadata::VTable>`: 8 bytes,
4749
print-type-size field `.pointer`: 8 bytes
4850
print-type-size type: `std::mem::ManuallyDrop<{async fn body of wait()}>`: 1 bytes, alignment: 1 bytes
4951
print-type-size field `.value`: 1 bytes
52+
print-type-size type: `std::mem::MaybeDangling<{async fn body of wait()}>`: 1 bytes, alignment: 1 bytes
53+
print-type-size field `.0`: 1 bytes
5054
print-type-size type: `std::mem::MaybeUninit<{async fn body of wait()}>`: 1 bytes, alignment: 1 bytes
5155
print-type-size variant `MaybeUninit`: 1 bytes
5256
print-type-size field `.uninit`: 0 bytes

tests/ui/print_type_sizes/coroutine_discr_placement.stdout

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ print-type-size variant `Returned`: 0 bytes
1111
print-type-size variant `Panicked`: 0 bytes
1212
print-type-size type: `std::mem::ManuallyDrop<i32>`: 4 bytes, alignment: 4 bytes
1313
print-type-size field `.value`: 4 bytes
14+
print-type-size type: `std::mem::MaybeDangling<i32>`: 4 bytes, alignment: 4 bytes
15+
print-type-size field `.0`: 4 bytes
1416
print-type-size type: `std::mem::MaybeUninit<i32>`: 4 bytes, alignment: 4 bytes
1517
print-type-size variant `MaybeUninit`: 4 bytes
1618
print-type-size field `.uninit`: 0 bytes

tests/ui/thir-print/offset_of.stdout

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ body:
6868
)
6969
else_block: None
7070
lint_level: Explicit(HirId(DefId(offset_of::concrete).10))
71-
span: $DIR/offset_of.rs:37:5: 1433:57 (#0)
71+
span: $DIR/offset_of.rs:37:5: 1437:57 (#0)
7272
}
7373
}
7474
Stmt {
@@ -117,7 +117,7 @@ body:
117117
)
118118
else_block: None
119119
lint_level: Explicit(HirId(DefId(offset_of::concrete).20))
120-
span: $DIR/offset_of.rs:38:5: 1433:57 (#0)
120+
span: $DIR/offset_of.rs:38:5: 1437:57 (#0)
121121
}
122122
}
123123
Stmt {
@@ -166,7 +166,7 @@ body:
166166
)
167167
else_block: None
168168
lint_level: Explicit(HirId(DefId(offset_of::concrete).30))
169-
span: $DIR/offset_of.rs:39:5: 1433:57 (#0)
169+
span: $DIR/offset_of.rs:39:5: 1437:57 (#0)
170170
}
171171
}
172172
Stmt {
@@ -215,7 +215,7 @@ body:
215215
)
216216
else_block: None
217217
lint_level: Explicit(HirId(DefId(offset_of::concrete).40))
218-
span: $DIR/offset_of.rs:40:5: 1433:57 (#0)
218+
span: $DIR/offset_of.rs:40:5: 1437:57 (#0)
219219
}
220220
}
221221
Stmt {
@@ -264,7 +264,7 @@ body:
264264
)
265265
else_block: None
266266
lint_level: Explicit(HirId(DefId(offset_of::concrete).50))
267-
span: $DIR/offset_of.rs:41:5: 1433:57 (#0)
267+
span: $DIR/offset_of.rs:41:5: 1437:57 (#0)
268268
}
269269
}
270270
]
@@ -864,7 +864,7 @@ body:
864864
)
865865
else_block: None
866866
lint_level: Explicit(HirId(DefId(offset_of::generic).12))
867-
span: $DIR/offset_of.rs:45:5: 1433:57 (#0)
867+
span: $DIR/offset_of.rs:45:5: 1437:57 (#0)
868868
}
869869
}
870870
Stmt {
@@ -913,7 +913,7 @@ body:
913913
)
914914
else_block: None
915915
lint_level: Explicit(HirId(DefId(offset_of::generic).24))
916-
span: $DIR/offset_of.rs:46:5: 1433:57 (#0)
916+
span: $DIR/offset_of.rs:46:5: 1437:57 (#0)
917917
}
918918
}
919919
Stmt {
@@ -962,7 +962,7 @@ body:
962962
)
963963
else_block: None
964964
lint_level: Explicit(HirId(DefId(offset_of::generic).36))
965-
span: $DIR/offset_of.rs:47:5: 1433:57 (#0)
965+
span: $DIR/offset_of.rs:47:5: 1437:57 (#0)
966966
}
967967
}
968968
Stmt {
@@ -1011,7 +1011,7 @@ body:
10111011
)
10121012
else_block: None
10131013
lint_level: Explicit(HirId(DefId(offset_of::generic).48))
1014-
span: $DIR/offset_of.rs:48:5: 1433:57 (#0)
1014+
span: $DIR/offset_of.rs:48:5: 1437:57 (#0)
10151015
}
10161016
}
10171017
]

0 commit comments

Comments
 (0)