implement zeroed and uninitialized with MaybeUninit

This commit is contained in:
Ralf Jung 2020-03-11 14:23:13 +01:00
parent 303d8aff60
commit d49306da13
9 changed files with 14 additions and 110 deletions

View file

@ -10,17 +10,13 @@
#![feature(intrinsics)]
use std::thread;
extern "rust-intrinsic" {
pub fn init<T>() -> T;
}
use std::{mem, thread};
const SIZE: usize = 1024 * 1024;
fn main() {
// do the test in a new thread to avoid (spurious?) stack overflows
thread::spawn(|| {
let _memory: [u8; SIZE] = unsafe { init() };
let _memory: [u8; SIZE] = unsafe { mem::zeroed() };
}).join();
}

View file

@ -1,9 +0,0 @@
#![allow(deprecated)]
#![feature(core_intrinsics)]
use std::intrinsics::{init};
// Test that the `init` intrinsic is really unsafe
pub fn main() {
let stuff = init::<isize>(); //~ ERROR call to unsafe function is unsafe
}

View file

@ -1,11 +0,0 @@
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
--> $DIR/init-unsafe.rs:8:17
|
LL | let stuff = init::<isize>();
| ^^^^^^^^^^^^^^^ call to unsafe function
|
= note: consult the function's documentation for information on how to avoid undefined behavior
error: aborting due to previous error
For more information about this error, try `rustc --explain E0133`.

View file

@ -5,7 +5,6 @@
mod rusti {
extern "rust-intrinsic" {
pub fn init<T>() -> T;
pub fn move_val_init<T>(dst: *mut T, src: T);
}
}
@ -15,17 +14,17 @@ pub fn main() {
// sanity check
check_drops_state(0, None);
let mut x: Box<D> = box D(1);
assert_eq!(x.0, 1);
let mut x: Option<Box<D>> = Some(box D(1));
assert_eq!(x.as_ref().unwrap().0, 1);
// A normal overwrite, to demonstrate `check_drops_state`.
x = box D(2);
x = Some(box D(2));
// At this point, one destructor has run, because the
// overwrite of `x` drops its initial value.
check_drops_state(1, Some(1));
let mut y: Box<D> = rusti::init();
let mut y: Option<Box<D>> = std::mem::zeroed();
// An initial binding does not overwrite anything.
check_drops_state(1, Some(1));
@ -51,9 +50,9 @@ pub fn main() {
// during such a destructor call. We do so after the end of
// this scope.
assert_eq!(y.0, 2);
y.0 = 3;
assert_eq!(y.0, 3);
assert_eq!(y.as_ref().unwrap().0, 2);
y.as_mut().unwrap().0 = 3;
assert_eq!(y.as_ref().unwrap().0, 3);
check_drops_state(1, Some(1));
}

View file

@ -1,13 +0,0 @@
// run-pass
// pretty-expanded FIXME #23616
#![feature(intrinsics)]
mod rusti {
extern "rust-intrinsic" {
pub fn uninit<T>() -> T;
}
}
pub fn main() {
let _a : isize = unsafe {rusti::uninit()};
}