Skip to content

Commit 9d14a62

Browse files
committed
Auto merge of #913 - RalfJung:uninit, r=RalfJung
test some new uninit APIs
2 parents 285a585 + be4108e commit 9d14a62

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

tests/run-pass/rc.rs

+31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#![feature(weak_into_raw)]
2+
#![feature(new_uninit)]
3+
#![feature(get_mut_unchecked)]
24

35
use std::cell::{Cell, RefCell};
46
use std::rc::{Rc, Weak};
@@ -102,6 +104,33 @@ fn weak_from_raw() {
102104
assert!(Weak::upgrade(&unsafe { Weak::from_raw(raw_2) }).is_none());
103105
}
104106

107+
fn rc_uninit() {
108+
let mut five = Rc::<Box<u32>>::new_uninit();
109+
let five = unsafe {
110+
// Deferred initialization:
111+
Rc::get_mut_unchecked(&mut five).as_mut_ptr().write(Box::new(5));
112+
five.assume_init()
113+
};
114+
assert_eq!(**five, 5)
115+
}
116+
117+
fn rc_uninit_slice() {
118+
let mut values = Rc::<[Box<usize>]>::new_uninit_slice(3);
119+
120+
let values = unsafe {
121+
// Deferred initialization:
122+
Rc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(Box::new(0));
123+
Rc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(Box::new(1));
124+
Rc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(Box::new(2));
125+
126+
values.assume_init()
127+
};
128+
129+
for (idx, i) in values.iter().enumerate() {
130+
assert_eq!(idx, **i);
131+
}
132+
}
133+
105134
fn main() {
106135
rc_fat_ptr_eq();
107136
rc_refcell();
@@ -111,6 +140,8 @@ fn main() {
111140
rc_from();
112141
weak_into_raw();
113142
weak_from_raw();
143+
rc_uninit();
144+
rc_uninit_slice();
114145

115146
arc();
116147
}

tests/run-pass/slices.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![feature(new_uninit)]
2+
13
use std::slice;
24

35
fn slice_of_zst() {
@@ -169,7 +171,23 @@ fn test_iter_ref_consistency() {
169171
test_mut([0u32; 0]); // ZST with alignment > 0
170172
}
171173

174+
fn uninit_slice() {
175+
let mut values = Box::<[Box<u32>]>::new_uninit_slice(3);
176+
177+
let values = unsafe {
178+
// Deferred initialization:
179+
values[0].as_mut_ptr().write(Box::new(1));
180+
values[1].as_mut_ptr().write(Box::new(2));
181+
values[2].as_mut_ptr().write(Box::new(3));
182+
183+
values.assume_init()
184+
};
185+
186+
assert_eq!(values.iter().map(|x| **x).collect::<Vec<_>>(), vec![1, 2, 3])
187+
}
188+
172189
fn main() {
173190
slice_of_zst();
174191
test_iter_ref_consistency();
192+
uninit_slice();
175193
}

0 commit comments

Comments
 (0)