File tree 2 files changed +33
-0
lines changed
2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 10
10
#![ feature( const_assume) ]
11
11
#![ feature( const_cell_into_inner) ]
12
12
#![ feature( const_convert) ]
13
+ #![ feature( const_maybe_uninit_as_mut_ptr) ]
13
14
#![ feature( const_maybe_uninit_assume_init) ]
14
15
#![ feature( const_ptr_read) ]
15
16
#![ feature( const_ptr_write) ]
Original file line number Diff line number Diff line change @@ -269,3 +269,35 @@ fn uninit_const_assume_init_read() {
269
269
const FOO : u32 = unsafe { MaybeUninit :: new ( 42 ) . assume_init_read ( ) } ;
270
270
assert_eq ! ( FOO , 42 ) ;
271
271
}
272
+
273
+ #[ test]
274
+ fn const_maybe_uninit ( ) {
275
+ use std:: ptr;
276
+
277
+ #[ derive( Debug , PartialEq ) ]
278
+ struct Foo {
279
+ x : u8 ,
280
+ y : u8 ,
281
+ }
282
+
283
+ const FIELD_BY_FIELD : Foo = unsafe {
284
+ let mut val = MaybeUninit :: uninit ( ) ;
285
+ init_y ( & mut val) ; // order shouldn't matter
286
+ init_x ( & mut val) ;
287
+ val. assume_init ( )
288
+ } ;
289
+
290
+ const fn init_x ( foo : & mut MaybeUninit < Foo > ) {
291
+ unsafe {
292
+ * ptr:: addr_of_mut!( ( * foo. as_mut_ptr( ) ) . x) = 1 ;
293
+ }
294
+ }
295
+
296
+ const fn init_y ( foo : & mut MaybeUninit < Foo > ) {
297
+ unsafe {
298
+ * ptr:: addr_of_mut!( ( * foo. as_mut_ptr( ) ) . y) = 2 ;
299
+ }
300
+ }
301
+
302
+ assert_eq ! ( FIELD_BY_FIELD , Foo { x: 1 , y: 2 } ) ;
303
+ }
You can’t perform that action at this time.
0 commit comments