Skip to content

Commit 55b5ab8

Browse files
authored
Rollup merge of rust-lang#58183 - jethrogb:jb/alloc-box-guarantees, r=SimonSapin
Clarify guarantees for `Box` allocation This basically says `Box` does the obvious things for its allocations. See also: https://users.rust-lang.org/t/alloc-crate-guarantees/24981 This may require a T-libs FCP? Not sure. r? @sfackler
2 parents ae64668 + e41e694 commit 55b5ab8

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

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;

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)