@@ -159,13 +159,14 @@ library as a replacement.
159
159
Add the aforementioned ` MaybeUninit ` type to the standard library:
160
160
161
161
``` rust
162
- union MaybeUninit <T > {
162
+ pub union MaybeUninit <T > {
163
163
uninit : (),
164
164
value : ManuallyDrop <T >,
165
165
}
166
166
```
167
167
168
168
The type should have at least the following interface
169
+ ([ Playground link] ( https://play.rust-lang.org/?gist=81f5ab9a7e7107c9583de21382ef4333&version=nightly&mode=debug&edition=2015 ) ):
169
170
170
171
``` rust
171
172
impl <T > MaybeUninit <T > {
@@ -188,8 +189,8 @@ impl<T> MaybeUninit<T> {
188
189
/// Note that dropping a `MaybeUninit` will never call `T`'s drop code.
189
190
/// It is your responsibility to make sure `T` gets dropped if it got initialized.
190
191
pub fn zeroed () -> MaybeUninit <T > {
191
- let mut u = uninitialized ();
192
- u . as_mut_ptr (). write_bytes (0u8 , 1 );
192
+ let mut u = MaybeUninit :: < T > :: uninitialized ();
193
+ unsafe { u . as_mut_ptr (). write_bytes (0u8 , 1 ); }
193
194
u
194
195
}
195
196
@@ -233,13 +234,13 @@ impl<T> MaybeUninit<T> {
233
234
/// Get a pointer to the contained value. Reading from this pointer will be undefined
234
235
/// behavior unless the `MaybeUninit` is initialized.
235
236
pub fn as_ptr (& self ) -> * const T {
236
- & * self . value * const T
237
+ unsafe { & * self . value as * const T }
237
238
}
238
239
239
240
/// Get a mutable pointer to the contained value. Reading from this pointer will be undefined
240
241
/// behavior unless the `MaybeUninit` is initialized.
241
242
pub fn as_mut_ptr (& mut self ) -> * mut T {
242
- & mut * self . value * mut T
243
+ unsafe { & mut * self . value as * mut T }
243
244
}
244
245
}
245
246
```
0 commit comments