-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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 1 commit
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,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 */ | ||
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 | ||
|
Uh oh!
There was an error while loading. Please reload this page.