mirror of
https://github.com/rust-lang/rust.git
synced 2024-10-30 14:01:51 +00:00
Box validity: update for new zero-sized rules
This commit is contained in:
parent
6cf068db56
commit
05ffd446a9
@ -53,22 +53,19 @@
|
||||
//!
|
||||
//! # Memory layout
|
||||
//!
|
||||
//! For non-zero-sized values, a [`Box`] will use the [`Global`] allocator for
|
||||
//! its allocation. It is valid to convert both ways between a [`Box`] and a
|
||||
//! raw pointer allocated with the [`Global`] allocator, given that the
|
||||
//! [`Layout`] used with the allocator is correct for the type. More precisely,
|
||||
//! a `value: *mut T` that has been allocated with the [`Global`] allocator
|
||||
//! with `Layout::for_value(&*value)` may be converted into a box using
|
||||
//! [`Box::<T>::from_raw(value)`]. Conversely, the memory backing a `value: *mut
|
||||
//! T` obtained from [`Box::<T>::into_raw`] may be deallocated using the
|
||||
//! [`Global`] allocator with [`Layout::for_value(&*value)`].
|
||||
//! For non-zero-sized values, a [`Box`] will use the [`Global`] allocator for its allocation. It is
|
||||
//! valid to convert both ways between a [`Box`] and a raw pointer allocated with the [`Global`]
|
||||
//! allocator, given that the [`Layout`] used with the allocator is correct for the type and the raw
|
||||
//! pointer points to a valid value of the right type. More precisely, a `value: *mut T` that has
|
||||
//! been allocated with the [`Global`] allocator with `Layout::for_value(&*value)` may be converted
|
||||
//! into a box using [`Box::<T>::from_raw(value)`]. Conversely, the memory backing a `value: *mut T`
|
||||
//! obtained from [`Box::<T>::into_raw`] may be deallocated using the [`Global`] allocator with
|
||||
//! [`Layout::for_value(&*value)`].
|
||||
//!
|
||||
//! For zero-sized values, the `Box` pointer still has to be [valid] for reads
|
||||
//! and writes and sufficiently aligned. In particular, casting any aligned
|
||||
//! non-zero integer literal to a raw pointer produces a valid pointer, but a
|
||||
//! pointer pointing into previously allocated memory that since got freed is
|
||||
//! not valid. The recommended way to build a Box to a ZST if `Box::new` cannot
|
||||
//! be used is to use [`ptr::NonNull::dangling`].
|
||||
//! For zero-sized values, the `Box` pointer has to be non-null and sufficiently aligned. The
|
||||
//! recommended way to build a Box to a ZST if `Box::new` cannot be used is to use
|
||||
//! [`ptr::NonNull::dangling`]. Even for zero-sized types, the pointee type must be inhabited
|
||||
//! to ensure that the Box points to a valid value of the given type.
|
||||
//!
|
||||
//! So long as `T: Sized`, a `Box<T>` is guaranteed to be represented
|
||||
//! as a single pointer and is also ABI-compatible with C pointers
|
||||
|
Loading…
Reference in New Issue
Block a user