Skip to content

Clarify Box<T> representation and its use in FFI #62514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 12, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@
//! T` obtained from `Box::<T>::into_raw` may be deallocated using the
//! [`Global`] allocator with `Layout::for_value(&*value)`.
//!
//! `Box<T>` has the same representation as `*mut T`. In particular, when
//! `T: Sized`, this means that `Box<T>` has the same representation as
//! a C pointer, making the following code valid in FFI:
//!
//! ```c
//! /* C header */
//! struct Foo* foo(); /* Returns ownership */

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Returns" is ambiguous - can be construed as returning ownership to the function. I would say "Returns ownership to the caller." or "Gives ownership to the caller.".

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proper C syntax is struct Foo* foo(void);.

//! void bar(struct Foo*); /* `bar` takes ownership */

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment has the format "bar takes.." but the comment above is not "foo returns ...". So to be consistent, would change the comment to "Takes ownership from the caller.".

//! ```
//!
//! ```
//! #[repr(C)]
//! pub struct Foo;
//!
//! #[no_mangle]
//! pub extern "C" fn foo() -> Box<Foo> {
//! Box::new(Foo)
//! }
//!
//! #[no_mangle]
//! pub extern "C" fn bar(_: Option<Box<Foo>>) {}
//! ```
//!
//! [dereferencing]: ../../std/ops/trait.Deref.html
//! [`Box`]: struct.Box.html
Expand Down