5 use std::mem::needs_drop;
6 use std::mem::ManuallyDrop;
10 impl Drop for NeedDrop {
14 // Constant expressios allow `NoDrop` to go out of scope,
15 // unlike a value of the interior type implementing `Drop`.
16 static X: () = (NoDrop { inner: ManuallyDrop::new(NeedDrop) }, ()).1;
18 const Y: () = (NoDrop { inner: ManuallyDrop::new(NeedDrop) }, ()).1;
20 const fn _f() { (NoDrop { inner: ManuallyDrop::new(NeedDrop) }, ()).1 }
22 // A union that scrubs the drop glue from its inner type
23 union NoDrop<T> { inner: ManuallyDrop<T> }
25 // Copy currently can't be implemented on drop-containing unions,
26 // this may change later
27 // https://github.com/rust-lang/rust/pull/38934#issuecomment-271219289
29 // // We should be able to implement Copy for NoDrop
30 // impl<T> Copy for NoDrop<T> {}
31 // impl<T> Clone for NoDrop<T> {fn clone(&self) -> Self { *self }}
33 // // We should be able to implement Copy for things using NoDrop
34 // #[derive(Copy, Clone)]
44 union ActuallyDrop<T> { inner: ManuallyDrop<T> }
46 impl<T> Drop for ActuallyDrop<T> {
51 // NoDrop should not make needs_drop true
52 assert!(!needs_drop::<Foo>());
53 assert!(!needs_drop::<NoDrop<u8>>());
54 assert!(!needs_drop::<NoDrop<Box<u8>>>());
55 // presence of other drop types should still work
56 assert!(needs_drop::<Baz>());
57 // drop impl on union itself should work
58 assert!(needs_drop::<ActuallyDrop<u8>>());
59 assert!(needs_drop::<ActuallyDrop<Box<u8>>>());