This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 1010#![ feature( const_assume) ]
1111#![ feature( const_cell_into_inner) ]
1212#![ feature( const_convert) ]
13+ #![ feature( const_maybe_uninit_as_mut_ptr) ]
1314#![ feature( const_maybe_uninit_assume_init) ]
1415#![ feature( const_ptr_read) ]
1516#![ feature( const_ptr_write) ]
Original file line number Diff line number Diff line change @@ -269,3 +269,35 @@ fn uninit_const_assume_init_read() {
269269 const FOO : u32 = unsafe { MaybeUninit :: new ( 42 ) . assume_init_read ( ) } ;
270270 assert_eq ! ( FOO , 42 ) ;
271271}
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