Skip to content

Commit e41e694

Browse files
author
Jethro Beekman
committed
Clarify guarantees for Box allocation
1 parent 8ae730a commit e41e694

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

Diff for: src/liballoc/alloc.rs

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ extern "Rust" {
3434
/// This type implements the [`Alloc`] trait by forwarding calls
3535
/// to the allocator registered with the `#[global_allocator]` attribute
3636
/// if there is one, or the `std` crate’s default.
37+
///
38+
/// Note: while this type is unstable, the functionality it provides can be
39+
/// accessed through the [free functions in `alloc`](index.html#functions).
3740
#[unstable(feature = "allocator_api", issue = "32838")]
3841
#[derive(Copy, Clone, Default, Debug)]
3942
pub struct Global;

Diff for: src/liballoc/boxed.rs

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
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+
//!
717
//! # Examples
818
//!
919
//! Move a value from the stack to the heap by creating a [`Box`]:

0 commit comments

Comments
 (0)