-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Clarify Box<T>
representation and its use in FFI
#62514
Changes from 2 commits
318c5d6
aea9423
812ec6a
ead1159
fe6ddd5
1a26df7
cb1cc11
fafa489
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,27 @@ | |
//! T` obtained from `Box::<T>::into_raw` may be deallocated using the | ||
//! [`Global`] allocator with `Layout::for_value(&*value)`. | ||
//! | ||
//! `Box<T>` has the same ABI as `&mut T`. In particular, when `T: Sized`, | ||
//! this allows using `Box<T>` in FFI: | ||
RalfJung marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to make sure this does not get lost: This should talk about alignment, say that a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't each instance of a type required to be aligned according to its type? If you think that it is important to state nonetheless, does that wording look right: "A There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Validity of But you are right that being aligned is not sufficient: For a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for explaining. Please let me know if you would like me to add a link to this other section of the docs or anything else to this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I think a link to https://doc.rust-lang.org/nightly/std/boxed/index.html#memory-layout would be good. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR actually appends documentation to this "Memory layout" section, so I think a (self referential) link is not needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm inclined to agree with @stephaneyfx that a self-referential link doesn't make sense. I think the key point is that you can "round-trip" a box through to a C function (where it is a C pointer) and back again. In this case, the pointer will naturally obey all the criteria. It's not really safe to take some arbitrary C pointer and cast it to a box unless it came from the global allocator (which in turn guarantees its alignment). Maybe we can reword the "Box is interopable" with FFI to emphasize this use case? (Which is precisely the one that the example shows) |
||
//! | ||
//! ```c | ||
//! /* C header */ | ||
nikomatsakis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//! struct Foo* foo(); /* Returns ownership */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Proper C syntax is |
||
//! void bar(struct Foo*); /* `bar` takes ownership */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment has the format " |
||
//! ``` | ||
//! | ||
//! ``` | ||
//! #[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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the key point here that it has the same ABI/representation as a C pointer? It feels like there is a "logical step" missing in this sentence -- e.g.,
Box<T>
is the same as&mut T
, which in turn is the same as a raw pointer or C pointer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something like:
So long as
T: Sized
, aBox<T>
is guaranteed to be represented as a single pointer and is also ABI-compatible with C pointers (i.e., the C typeT*
). This means that you have Rust code which passes ownership of aBox<T>
to C code by usingBox<T>
as the type on the Rust side, andT*
as the corresponding type on the C side. As an example, consider this C header, which declares functions that create/return some kind ofFoo
value:...
These two functions might be implemented in Rust as follows. Here, the
struct Foo*
types fromC
are translated toBox<Foo>
, which captures the ownership constraints. Note also that the nullable argument todestroy_foo
is represented in Rust asOption<Box<Foo>>
, sinceBox<Foo>
cannot be null....
Even though
Box<T>
has the same representation and C ABI as a C pointer, this does not mean that you can convert an arbitraryT*
into aBox<T>
and expect things to work.Box<T>
values will always be fully aligned, non-null pointers. Moreover, the destructor forBox<T>
will attempt to free the value with the global allocator. In general the best practice is to only useBox<T>
for pointers that originated from the global allocator.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nikomatsakis No problem for the delay and thank you for the thorough review.
The original commit that started this PR didn't mention references and focused on C pointers, but previous reviews recommended changing that.
I integrated your wording (slightly edited). Please let me know if that's ok. Thank you.