1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 #![feature(core_intrinsics)]
14 #![feature(untagged_unions)]
16 #![allow(unions_with_drop_fields)]
19 use std::intrinsics::needs_drop;
23 impl Drop for NeedDrop {
27 // Constant expressios allow `NoDrop` to go out of scope,
28 // unlike a value of the interior type implementing `Drop`.
29 static X: () = (NoDrop { inner: NeedDrop }, ()).1;
31 // A union that scrubs the drop glue from its inner type
32 union NoDrop<T> {inner: T}
34 // Copy currently can't be implemented on drop-containing unions,
35 // this may change later
36 // https://github.com/rust-lang/rust/pull/38934#issuecomment-271219289
38 // // We should be able to implement Copy for NoDrop
39 // impl<T> Copy for NoDrop<T> {}
40 // impl<T> Clone for NoDrop<T> {fn clone(&self) -> Self { *self }}
42 // // We should be able to implement Copy for things using NoDrop
43 // #[derive(Copy, Clone)]
53 union ActuallyDrop<T> {inner: T}
55 impl<T> Drop for ActuallyDrop<T> {
61 // NoDrop should not make needs_drop true
62 assert!(!needs_drop::<Foo>());
63 assert!(!needs_drop::<NoDrop<u8>>());
64 assert!(!needs_drop::<NoDrop<Box<u8>>>());
65 // presence of other drop types should still work
66 assert!(needs_drop::<Baz>());
67 // drop impl on union itself should work
68 assert!(needs_drop::<ActuallyDrop<u8>>());
69 assert!(needs_drop::<ActuallyDrop<Box<u8>>>());