@@ -1165,6 +1165,32 @@ impl<T> MaybeUninit<T> {
1165
1165
1166
1166
/// Gets a pointer to the contained value. Reading from this pointer or turning it
1167
1167
/// into a reference is undefined behavior unless the `MaybeUninit` is initialized.
1168
+ ///
1169
+ /// # Examples
1170
+ ///
1171
+ /// Correct usage of this method:
1172
+ ///
1173
+ /// ```rust
1174
+ /// #![feature(maybe_uninit)]
1175
+ /// use std::mem::MaybeUninit;
1176
+ ///
1177
+ /// let mut x = MaybeUninit::<Vec<u32>>::uninitialized();
1178
+ /// x.set(vec![0,1,2]);
1179
+ /// // Create a reference into the `MaybeUninit`. This is okay because we initialized it.
1180
+ /// let x_vec = unsafe { &*x.as_ptr() };
1181
+ /// assert_eq!(x_vec.len(), 3);
1182
+ /// ```
1183
+ ///
1184
+ /// *Incorrect* usage of this method:
1185
+ ///
1186
+ /// ```rust,no_run
1187
+ /// #![feature(maybe_uninit)]
1188
+ /// use std::mem::MaybeUninit;
1189
+ ///
1190
+ /// let x = MaybeUninit::<Vec<u32>>::uninitialized();
1191
+ /// let x_vec = unsafe { &*x.as_ptr() };
1192
+ /// // We have created a reference to an uninitialized vector! This is undefined behavior.
1193
+ /// ```
1168
1194
#[ unstable( feature = "maybe_uninit" , issue = "53491" ) ]
1169
1195
#[ inline( always) ]
1170
1196
pub fn as_ptr ( & self ) -> * const T {
@@ -1173,6 +1199,33 @@ impl<T> MaybeUninit<T> {
1173
1199
1174
1200
/// Gets a mutable pointer to the contained value. Reading from this pointer or turning it
1175
1201
/// into a reference is undefined behavior unless the `MaybeUninit` is initialized.
1202
+ ///
1203
+ /// # Examples
1204
+ ///
1205
+ /// Correct usage of this method:
1206
+ ///
1207
+ /// ```rust
1208
+ /// #![feature(maybe_uninit)]
1209
+ /// use std::mem::MaybeUninit;
1210
+ ///
1211
+ /// let mut x = MaybeUninit::<Vec<u32>>::uninitialized();
1212
+ /// x.set(vec![0,1,2]);
1213
+ /// // Create a reference into the `MaybeUninit`. This is okay because we initialized it.
1214
+ /// let x_vec = unsafe { &mut *x.as_mut_ptr() };
1215
+ /// x_vec.push(3);
1216
+ /// assert_eq!(x_vec.len(), 4);
1217
+ /// ```
1218
+ ///
1219
+ /// *Incorrect* usage of this method:
1220
+ ///
1221
+ /// ```rust,no_run
1222
+ /// #![feature(maybe_uninit)]
1223
+ /// use std::mem::MaybeUninit;
1224
+ ///
1225
+ /// let mut x = MaybeUninit::<Vec<u32>>::uninitialized();
1226
+ /// let x_vec = unsafe { &mut *x.as_mut_ptr() };
1227
+ /// // We have created a reference to an uninitialized vector! This is undefined behavior.
1228
+ /// ```
1176
1229
#[ unstable( feature = "maybe_uninit" , issue = "53491" ) ]
1177
1230
#[ inline( always) ]
1178
1231
pub fn as_mut_ptr ( & mut self ) -> * mut T {
0 commit comments