Skip to content

Commit 44b5b83

Browse files
committed
Add test for const MaybeUninit
1 parent ad8e6bf commit 44b5b83

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

library/core/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
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)]

library/core/tests/mem.rs

+32
Original file line numberDiff line numberDiff 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+
}

0 commit comments

Comments
 (0)