44//! heap allocation in Rust. Boxes provide ownership for this allocation, and
55//! drop their contents when they go out of scope.
66//!
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- //!
177//! # Examples
188//!
199//! Move a value from the stack to the heap by creating a [`Box`]:
6151//! for a `Cons`. By introducing a `Box`, which has a defined size, we know how
6252//! big `Cons` needs to be.
6353//!
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+ //!
6467//! [dereferencing]: ../../std/ops/trait.Deref.html
6568//! [`Box`]: struct.Box.html
6669//! [`Global`]: ../alloc/struct.Global.html
@@ -128,11 +131,8 @@ impl<T: ?Sized> Box<T> {
128131 /// After calling this function, the raw pointer is owned by the
129132 /// resulting `Box`. Specifically, the `Box` destructor will call
130133 /// 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` .
136136 ///
137137 /// # Safety
138138 ///
@@ -141,24 +141,27 @@ impl<T: ?Sized> Box<T> {
141141 /// function is called twice on the same raw pointer.
142142 ///
143143 /// # 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`]:
145146 /// ```
146147 /// let x = Box::new(5);
147148 /// let ptr = Box::into_raw(x);
148149 /// let x = unsafe { Box::from_raw(ptr) };
149150 /// ```
150151 /// Manually create a `Box` from scratch by using the global allocator:
151152 /// ```
152- /// use std::alloc::{Layout, alloc };
153+ /// use std::alloc::{alloc, Layout };
153154 ///
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+ /// }
157160 /// ```
158161 ///
162+ /// [memory layout]: index.html#memory-layout
159163 /// [`Layout`]: ../alloc/struct.Layout.html
160164 /// [`Box::into_raw`]: struct.Box.html#method.into_raw
161- ///
162165 #[ stable( feature = "box_raw" , since = "1.4.0" ) ]
163166 #[ inline]
164167 pub unsafe fn from_raw ( raw : * mut T ) -> Self {
@@ -171,9 +174,11 @@ impl<T: ?Sized> Box<T> {
171174 ///
172175 /// After calling this function, the caller is responsible for the
173176 /// 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.
177182 ///
178183 /// Note: this is an associated function, which means that you have
179184 /// to call it as `Box::into_raw(b)` instead of `b.into_raw()`. This
@@ -185,21 +190,24 @@ impl<T: ?Sized> Box<T> {
185190 /// ```
186191 /// let x = Box::new(String::from("Hello"));
187192 /// let ptr = Box::into_raw(x);
188- /// let x = unsafe{ Box::from_raw(ptr) };
193+ /// let x = unsafe { Box::from_raw(ptr) };
189194 /// ```
190- /// Manual cleanup by running the destructor and deallocating the memory:
195+ /// Manual cleanup by explicitly running the destructor and deallocating
196+ /// the memory:
191197 /// ```
192- /// use std::alloc::{Layout, dealloc };
198+ /// use std::alloc::{dealloc, Layout };
193199 /// use std::ptr;
194200 ///
195201 /// let x = Box::new(String::from("Hello"));
196202 /// 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+ /// }
199207 /// ```
200208 ///
209+ /// [memory layout]: index.html#memory-layout
201210 /// [`Box::from_raw`]: struct.Box.html#method.from_raw
202- ///
203211 #[ stable( feature = "box_raw" , since = "1.4.0" ) ]
204212 #[ inline]
205213 pub fn into_raw ( b : Box < T > ) -> * mut T {
@@ -233,7 +241,7 @@ impl<T: ?Sized> Box<T> {
233241 ///
234242 /// // Clean up the memory by converting the NonNull pointer back
235243 /// // 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()) };
237245 /// }
238246 /// ```
239247 #[ unstable( feature = "box_into_raw_non_null" , issue = "47336" ) ]
0 commit comments