File tree 2 files changed +49
-0
lines changed
2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change 1
1
#![ feature( weak_into_raw) ]
2
+ #![ feature( new_uninit) ]
3
+ #![ feature( get_mut_unchecked) ]
2
4
3
5
use std:: cell:: { Cell , RefCell } ;
4
6
use std:: rc:: { Rc , Weak } ;
@@ -102,6 +104,33 @@ fn weak_from_raw() {
102
104
assert ! ( Weak :: upgrade( & unsafe { Weak :: from_raw( raw_2) } ) . is_none( ) ) ;
103
105
}
104
106
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
+
105
134
fn main ( ) {
106
135
rc_fat_ptr_eq ( ) ;
107
136
rc_refcell ( ) ;
@@ -111,6 +140,8 @@ fn main() {
111
140
rc_from ( ) ;
112
141
weak_into_raw ( ) ;
113
142
weak_from_raw ( ) ;
143
+ rc_uninit ( ) ;
144
+ rc_uninit_slice ( ) ;
114
145
115
146
arc ( ) ;
116
147
}
Original file line number Diff line number Diff line change
1
+ #![ feature( new_uninit) ]
2
+
1
3
use std:: slice;
2
4
3
5
fn slice_of_zst ( ) {
@@ -169,7 +171,23 @@ fn test_iter_ref_consistency() {
169
171
test_mut ( [ 0u32 ; 0 ] ) ; // ZST with alignment > 0
170
172
}
171
173
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
+
172
189
fn main ( ) {
173
190
slice_of_zst ( ) ;
174
191
test_iter_ref_consistency ( ) ;
192
+ uninit_slice ( ) ;
175
193
}
You can’t perform that action at this time.
0 commit comments