4
4
//! heap allocation in Rust. Boxes provide ownership for this allocation, and
5
5
//! drop their contents when they go out of scope.
6
6
//!
7
- //! For non-zero-sized values, a [`Box`] will use the [`Global`] allocator for
8
- //! its allocation. It is valid to convert both ways between a [`Box`] and a
9
- //! raw pointer allocated with the [`Global`] allocator, given that the
10
- //! [`Layout`] used with the allocator is correct for the type. More precisely,
11
- //! a `value: *mut T` that has been allocated with the [`Global`] allocator
12
- //! with `Layout::for_value(&*value)` may be converted into a box using
13
- //! `Box::<T>::from_raw(value)`. Conversely, the memory backing a `value: *mut
14
- //! T` obtained from `Box::<T>::into_raw` may be deallocated using the
15
- //! [`Global`] allocator with `Layout::for_value(&*value)`.
16
- //!
17
7
//! # Examples
18
8
//!
19
9
//! Move a value from the stack to the heap by creating a [`Box`]:
61
51
//! for a `Cons`. By introducing a `Box`, which has a defined size, we know how
62
52
//! big `Cons` needs to be.
63
53
//!
54
+ //! # Memory layout
55
+ //!
56
+ //! For non-zero-sized values, a [`Box`] will use the [`Global`] allocator for
57
+ //! its allocation. It is valid to convert both ways between a [`Box`] and a
58
+ //! raw pointer allocated with the [`Global`] allocator, given that the
59
+ //! [`Layout`] used with the allocator is correct for the type. More precisely,
60
+ //! a `value: *mut T` that has been allocated with the [`Global`] allocator
61
+ //! with `Layout::for_value(&*value)` may be converted into a box using
62
+ //! `Box::<T>::from_raw(value)`. Conversely, the memory backing a `value: *mut
63
+ //! T` obtained from `Box::<T>::into_raw` may be deallocated using the
64
+ //! [`Global`] allocator with `Layout::for_value(&*value)`.
65
+ //!
66
+ //!
64
67
//! [dereferencing]: ../../std/ops/trait.Deref.html
65
68
//! [`Box`]: struct.Box.html
66
69
//! [`Global`]: ../alloc/struct.Global.html
@@ -128,11 +131,8 @@ impl<T: ?Sized> Box<T> {
128
131
/// After calling this function, the raw pointer is owned by the
129
132
/// resulting `Box`. Specifically, the `Box` destructor will call
130
133
/// the destructor of `T` and free the allocated memory. For this
131
- /// to be safe, the memory must have been allocated in the precise
132
- /// way that `Box` expects, namely, using the global allocator
133
- /// with the correct [`Layout`] for holding a value of type `T`. In
134
- /// particular, this will be satisfied for a pointer obtained
135
- /// from a previously existing `Box` using [`Box::into_raw`].
134
+ /// to be safe, the memory must have been allocated in accordance
135
+ /// with the [memory layout] used by `Box` .
136
136
///
137
137
/// # Safety
138
138
///
@@ -141,24 +141,27 @@ impl<T: ?Sized> Box<T> {
141
141
/// function is called twice on the same raw pointer.
142
142
///
143
143
/// # Examples
144
- /// Recreate a `Box` which was previously converted to a raw pointer using [`Box::into_raw`]:
144
+ /// Recreate a `Box` which was previously converted to a raw pointer
145
+ /// using [`Box::into_raw`]:
145
146
/// ```
146
147
/// let x = Box::new(5);
147
148
/// let ptr = Box::into_raw(x);
148
149
/// let x = unsafe { Box::from_raw(ptr) };
149
150
/// ```
150
151
/// Manually create a `Box` from scratch by using the global allocator:
151
152
/// ```
152
- /// use std::alloc::{Layout, alloc };
153
+ /// use std::alloc::{alloc, Layout };
153
154
///
154
- /// let ptr = unsafe{ alloc(Layout::new::<i32>()) } as *mut i32;
155
- /// unsafe{ *ptr = 5; }
156
- /// let x = unsafe{ Box::from_raw(ptr) };
155
+ /// unsafe {
156
+ /// let ptr = alloc(Layout::new::<i32>()) as *mut i32;
157
+ /// *ptr = 5;
158
+ /// let x = Box::from_raw(ptr);
159
+ /// }
157
160
/// ```
158
161
///
162
+ /// [memory layout]: index.html#memory-layout
159
163
/// [`Layout`]: ../alloc/struct.Layout.html
160
164
/// [`Box::into_raw`]: struct.Box.html#method.into_raw
161
- ///
162
165
#[ stable( feature = "box_raw" , since = "1.4.0" ) ]
163
166
#[ inline]
164
167
pub unsafe fn from_raw ( raw : * mut T ) -> Self {
@@ -171,9 +174,11 @@ impl<T: ?Sized> Box<T> {
171
174
///
172
175
/// After calling this function, the caller is responsible for the
173
176
/// memory previously managed by the `Box`. In particular, the
174
- /// caller should properly destroy `T` and release the memory. The
175
- /// easiest way to do so is to convert the raw pointer back into a `Box`
176
- /// with the [`Box::from_raw`] function.
177
+ /// caller should properly destroy `T` and release the memory, taking
178
+ /// into account the [memory layout] used by `Box`. The easiest way to
179
+ /// do this is to convert the raw pointer back into a `Box` with the
180
+ /// [`Box::from_raw`] function, allowing the `Box` destructor to perform
181
+ /// the cleanup.
177
182
///
178
183
/// Note: this is an associated function, which means that you have
179
184
/// to call it as `Box::into_raw(b)` instead of `b.into_raw()`. This
@@ -185,21 +190,24 @@ impl<T: ?Sized> Box<T> {
185
190
/// ```
186
191
/// let x = Box::new(String::from("Hello"));
187
192
/// let ptr = Box::into_raw(x);
188
- /// let x = unsafe{ Box::from_raw(ptr) };
193
+ /// let x = unsafe { Box::from_raw(ptr) };
189
194
/// ```
190
- /// Manual cleanup by running the destructor and deallocating the memory:
195
+ /// Manual cleanup by explicitly running the destructor and deallocating
196
+ /// the memory:
191
197
/// ```
192
- /// use std::alloc::{Layout, dealloc };
198
+ /// use std::alloc::{dealloc, Layout };
193
199
/// use std::ptr;
194
200
///
195
201
/// let x = Box::new(String::from("Hello"));
196
202
/// let p = Box::into_raw(x);
197
- /// unsafe{ ptr::drop_in_place(p); }
198
- /// unsafe{ dealloc(p as *mut u8, Layout::new::<String>()); }
203
+ /// unsafe {
204
+ /// ptr::drop_in_place(p);
205
+ /// dealloc(p as *mut u8, Layout::new::<String>());
206
+ /// }
199
207
/// ```
200
208
///
209
+ /// [memory layout]: index.html#memory-layout
201
210
/// [`Box::from_raw`]: struct.Box.html#method.from_raw
202
- ///
203
211
#[ stable( feature = "box_raw" , since = "1.4.0" ) ]
204
212
#[ inline]
205
213
pub fn into_raw ( b : Box < T > ) -> * mut T {
@@ -233,7 +241,7 @@ impl<T: ?Sized> Box<T> {
233
241
///
234
242
/// // Clean up the memory by converting the NonNull pointer back
235
243
/// // into a Box and letting the Box be dropped.
236
- /// let x = unsafe{ Box::from_raw(ptr.as_ptr()) };
244
+ /// let x = unsafe { Box::from_raw(ptr.as_ptr()) };
237
245
/// }
238
246
/// ```
239
247
#[ unstable( feature = "box_into_raw_non_null" , issue = "47336" ) ]
0 commit comments