1
1
/// Temporary fork of some stuff in `core` that's doesn't have a `const fn` API
2
2
3
3
pub mod mem {
4
- pub use core:: mem:: { replace, zeroed} ;
4
+ #[ cfg( not( feature = "const-fn" ) ) ]
5
+ pub use core:: mem:: uninitialized;
6
+ pub use core:: mem:: { replace, zeroed, ManuallyDrop } ;
5
7
6
- use core:: ops:: { Deref , DerefMut } ;
7
-
8
- #[ allow( unions_with_drop_fields) ]
9
- pub union ManuallyDrop < T > {
10
- value : T ,
11
- }
12
-
13
- impl < T > ManuallyDrop < T > {
14
- #[ inline]
15
- const_fn ! (
16
- pub const fn new( value: T ) -> ManuallyDrop <T > {
17
- ManuallyDrop { value: value }
18
- }
19
- ) ;
20
- }
21
-
22
- impl < T > Deref for ManuallyDrop < T > {
23
- type Target = T ;
24
-
25
- #[ inline]
26
- fn deref ( & self ) -> & Self :: Target {
27
- unsafe { & self . value }
8
+ #[ cfg( feature = "const-fn" ) ]
9
+ pub const unsafe fn uninitialized < T > ( ) -> T {
10
+ #[ allow( unions_with_drop_fields) ]
11
+ union U < T > {
12
+ none : ( ) ,
13
+ some : T ,
28
14
}
29
- }
30
15
31
- impl < T > DerefMut for ManuallyDrop < T > {
32
- #[ inline]
33
- fn deref_mut ( & mut self ) -> & mut Self :: Target {
34
- unsafe { & mut self . value }
35
- }
16
+ U { none : ( ) } . some
36
17
}
37
-
38
- const_fn ! (
39
- pub const unsafe fn uninitialized<T >( ) -> T {
40
- #[ allow( unions_with_drop_fields) ]
41
- union U <T > {
42
- none: ( ) ,
43
- some: T ,
44
- }
45
-
46
- U { none: ( ) } . some
47
- }
48
- ) ;
49
18
}
50
19
51
20
#[ cfg( feature = "const-fn" ) ] // Remove this if there are more tests
@@ -61,14 +30,16 @@ mod test {
61
30
static mut I : i32 = unsafe { __core:: mem:: uninitialized ( ) } ;
62
31
// Initialize before drop
63
32
unsafe { core:: ptr:: write ( & mut I as * mut i32 , 42 ) } ;
64
- unsafe { assert_eq ! ( I , 42 ) } ;
33
+ unsafe { assert_eq ! ( I , 42 ) } ;
65
34
}
66
35
67
36
#[ cfg( feature = "const-fn" ) ]
68
37
#[ test]
69
38
fn static_new_manually_drop ( ) {
70
39
static mut M : ManuallyDrop < i32 > = ManuallyDrop :: new ( 42 ) ;
71
- unsafe { assert_eq ! ( * M , 42 ) ; }
40
+ unsafe {
41
+ assert_eq ! ( * M , 42 ) ;
42
+ }
72
43
// Drop before deinitialization
73
44
unsafe { core:: ptr:: drop_in_place ( & mut M as & mut i32 as * mut i32 ) } ;
74
45
}
0 commit comments