Skip to content

Commit

Permalink
clarify that Box<T> should only be used when defined *in Rust*
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Matsakis committed Dec 11, 2019
1 parent cb1cc11 commit fafa489
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/liballoc/boxed.rs
Expand Up @@ -63,13 +63,14 @@
//! T` obtained from `Box::<T>::into_raw` may be deallocated using the
//! [`Global`] allocator with `Layout::for_value(&*value)`.
//!
//! 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 (i.e. the C type
//! `T*`). This means that you can have Rust code which passes ownership of a
//! `Box<T>` to C code by using `Box<T>` as the type on the Rust side, and
//! `T*` as the corresponding type on the C side. As an example, consider this
//! C header which declares functions that create and destroy some kind of
//! `Foo` value:
//! 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
//! (i.e. the C type `T*`). This means that if you have extern "C"
//! Rust functions that will be called from C, you can define those
//! Rust functions using `Box<T>` types, and use `T*` as corresponding
//! type on the C side. As an example, consider this C header which
//! declares functions that create and destroy some kind of `Foo`
//! value:
//!
//! ```c
//! /* C header */
Expand Down Expand Up @@ -108,6 +109,14 @@
//! is to only use `Box<T>` for pointers that originated from the global
//! allocator.
//!
//! **Important.** At least at present, you should avoid using
//! `Box<T>` types for functions that are defined in C but invoked
//! from Rust. In those cases, you should directly mirror the C types
//! as closely as possible. Using types like `Box<T>` where the C
//! definition is just using `T*` can lead to undefined behavior, as
//! described in [rust-lang/unsafe-code-guidelines#198][ucg#198].
//!
//! [ucg#198]: https://github.com/rust-lang/unsafe-code-guidelines/issues/198
//! [dereferencing]: ../../std/ops/trait.Deref.html
//! [`Box`]: struct.Box.html
//! [`Global`]: ../alloc/struct.Global.html
Expand Down

0 comments on commit fafa489

Please sign in to comment.