Skip to content

Commit 29aa44b

Browse files
committed
use MaybeDangling in ManuallyDrop
1 parent 68a92e9 commit 29aa44b

File tree

6 files changed

+69
-17
lines changed

6 files changed

+69
-17
lines changed

library/core/src/mem/manually_drop.rs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::marker::Destruct;
2+
use crate::mem::MaybeDangling;
23
use crate::ops::{Deref, DerefMut, DerefPure};
34
use crate::ptr;
45

@@ -152,11 +153,42 @@ use crate::ptr;
152153
/// [`MaybeUninit`]: crate::mem::MaybeUninit
153154
#[stable(feature = "manually_drop", since = "1.20.0")]
154155
#[lang = "manually_drop"]
155-
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
156+
#[derive(Copy, Clone, Debug, Default)]
156157
#[repr(transparent)]
157158
#[rustc_pub_transparent]
158159
pub struct ManuallyDrop<T: ?Sized> {
159-
value: T,
160+
value: MaybeDangling<T>,
161+
}
162+
163+
#[stable(feature = "manually_drop", since = "1.20.0")]
164+
impl<T: ?Sized + Eq> Eq for ManuallyDrop<T> {}
165+
166+
#[stable(feature = "manually_drop", since = "1.20.0")]
167+
impl<T: ?Sized + crate::hash::Hash> crate::hash::Hash for ManuallyDrop<T> {
168+
fn hash<H: crate::hash::Hasher>(&self, state: &mut H) {
169+
self.value.as_ref().hash(state);
170+
}
171+
}
172+
173+
#[stable(feature = "manually_drop", since = "1.20.0")]
174+
impl<T: ?Sized + Ord> Ord for ManuallyDrop<T> {
175+
fn cmp(&self, other: &Self) -> crate::cmp::Ordering {
176+
self.value.as_ref().cmp(other.value.as_ref())
177+
}
178+
}
179+
180+
#[stable(feature = "manually_drop", since = "1.20.0")]
181+
impl<T: ?Sized + PartialOrd> PartialOrd for ManuallyDrop<T> {
182+
fn partial_cmp(&self, other: &Self) -> Option<crate::cmp::Ordering> {
183+
self.value.as_ref().partial_cmp(other.value.as_ref())
184+
}
185+
}
186+
187+
#[stable(feature = "manually_drop", since = "1.20.0")]
188+
impl<T: ?Sized + PartialEq> PartialEq for ManuallyDrop<T> {
189+
fn eq(&self, other: &Self) -> bool {
190+
self.value.as_ref().eq(other.value.as_ref())
191+
}
160192
}
161193

162194
impl<T> ManuallyDrop<T> {
@@ -179,7 +211,7 @@ impl<T> ManuallyDrop<T> {
179211
#[rustc_const_stable(feature = "const_manually_drop", since = "1.32.0")]
180212
#[inline(always)]
181213
pub const fn new(value: T) -> ManuallyDrop<T> {
182-
ManuallyDrop { value }
214+
ManuallyDrop { value: MaybeDangling::new(value) }
183215
}
184216

185217
/// Extracts the value from the `ManuallyDrop` container.
@@ -197,7 +229,7 @@ impl<T> ManuallyDrop<T> {
197229
#[rustc_const_stable(feature = "const_manually_drop", since = "1.32.0")]
198230
#[inline(always)]
199231
pub const fn into_inner(slot: ManuallyDrop<T>) -> T {
200-
slot.value
232+
slot.value.into_inner()
201233
}
202234

203235
/// Takes the value from the `ManuallyDrop<T>` container out.
@@ -222,7 +254,7 @@ impl<T> ManuallyDrop<T> {
222254
pub const unsafe fn take(slot: &mut ManuallyDrop<T>) -> T {
223255
// SAFETY: we are reading from a reference, which is guaranteed
224256
// to be valid for reads.
225-
unsafe { ptr::read(&slot.value) }
257+
unsafe { ptr::read(slot.value.as_ref()) }
226258
}
227259
}
228260

@@ -259,7 +291,7 @@ impl<T: ?Sized> ManuallyDrop<T> {
259291
// SAFETY: we are dropping the value pointed to by a mutable reference
260292
// which is guaranteed to be valid for writes.
261293
// It is up to the caller to make sure that `slot` isn't dropped again.
262-
unsafe { ptr::drop_in_place(&mut slot.value) }
294+
unsafe { ptr::drop_in_place(slot.value.as_mut()) }
263295
}
264296
}
265297

@@ -269,7 +301,7 @@ impl<T: ?Sized> const Deref for ManuallyDrop<T> {
269301
type Target = T;
270302
#[inline(always)]
271303
fn deref(&self) -> &T {
272-
&self.value
304+
self.value.as_ref()
273305
}
274306
}
275307

@@ -278,7 +310,7 @@ impl<T: ?Sized> const Deref for ManuallyDrop<T> {
278310
impl<T: ?Sized> const DerefMut for ManuallyDrop<T> {
279311
#[inline(always)]
280312
fn deref_mut(&mut self) -> &mut T {
281-
&mut self.value
313+
self.value.as_mut()
282314
}
283315
}
284316

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)