-
Notifications
You must be signed in to change notification settings - Fork 13.7k
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
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.
Outdated
Show resolved
Hide resolved
|
||
//! | ||
//! ```c | ||
//! /* C header */ | ||
nikomatsakis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//! struct Foo* foo(); /* Returns ownership */ | ||
|
||
//! void bar(struct Foo*); /* `bar` takes ownership */ | ||
|
||
//! ``` | ||
//! | ||
//! ``` | ||
//! #[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.