Skip to content

Commit 4bf500f

Browse files
committed
elliminate mem::uninitialize from docs in libcore
1 parent 11fba52 commit 4bf500f

File tree

2 files changed

+4
-60
lines changed

2 files changed

+4
-60
lines changed

Diff for: src/libcore/mem.rs

-56
Original file line numberDiff line numberDiff line change
@@ -63,62 +63,6 @@ pub use crate::intrinsics::transmute;
6363
/// The practical use cases for `forget` are rather specialized and mainly come
6464
/// up in unsafe or FFI code.
6565
///
66-
/// ## Use case 1
67-
///
68-
/// You have created an uninitialized value using [`mem::uninitialized`][uninit].
69-
/// You must either initialize or `forget` it on every computation path before
70-
/// Rust drops it automatically, like at the end of a scope or after a panic.
71-
/// Running the destructor on an uninitialized value would be [undefined behavior][ub].
72-
///
73-
/// ```
74-
/// use std::mem;
75-
/// use std::ptr;
76-
///
77-
/// # let some_condition = false;
78-
/// unsafe {
79-
/// let mut uninit_vec: Vec<u32> = mem::uninitialized();
80-
///
81-
/// if some_condition {
82-
/// // Initialize the variable.
83-
/// ptr::write(&mut uninit_vec, Vec::new());
84-
/// } else {
85-
/// // Forget the uninitialized value so its destructor doesn't run.
86-
/// mem::forget(uninit_vec);
87-
/// }
88-
/// }
89-
/// ```
90-
///
91-
/// ## Use case 2
92-
///
93-
/// You have duplicated the bytes making up a value, without doing a proper
94-
/// [`Clone`][clone]. You need the value's destructor to run only once,
95-
/// because a double `free` is undefined behavior.
96-
///
97-
/// An example is a possible implementation of [`mem::swap`][swap]:
98-
///
99-
/// ```
100-
/// use std::mem;
101-
/// use std::ptr;
102-
///
103-
/// # #[allow(dead_code)]
104-
/// fn swap<T>(x: &mut T, y: &mut T) {
105-
/// unsafe {
106-
/// // Give ourselves some scratch space to work with
107-
/// let mut t: T = mem::uninitialized();
108-
///
109-
/// // Perform the swap, `&mut` pointers never alias
110-
/// ptr::copy_nonoverlapping(&*x, &mut t, 1);
111-
/// ptr::copy_nonoverlapping(&*y, x, 1);
112-
/// ptr::copy_nonoverlapping(&t, y, 1);
113-
///
114-
/// // y and t now point to the same thing, but we need to completely
115-
/// // forget `t` because we do not want to run the destructor for `T`
116-
/// // on its value, which is still owned somewhere outside this function.
117-
/// mem::forget(t);
118-
/// }
119-
/// }
120-
/// ```
121-
///
12266
/// [drop]: fn.drop.html
12367
/// [uninit]: fn.uninitialized.html
12468
/// [clone]: ../clone/trait.Clone.html

Diff for: src/libcore/ptr.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,12 @@ pub use crate::intrinsics::write_bytes;
155155
/// location first:
156156
/// ```
157157
/// use std::ptr;
158-
/// use std::mem;
158+
/// use std::mem::{self, MaybeUninit};
159159
///
160160
/// unsafe fn drop_after_copy<T>(to_drop: *mut T) {
161-
/// let mut copy: T = mem::uninitialized();
162-
/// ptr::copy(to_drop, &mut copy, 1);
163-
/// drop(copy);
161+
/// let mut copy: MaybeUninit<T> = MaybeUninit::uninit();
162+
/// ptr::copy(to_drop, copy.as_mut_ptr(), 1);
163+
/// drop(copy.assume_init());
164164
/// }
165165
///
166166
/// #[repr(packed, C)]

0 commit comments

Comments
 (0)