Skip to content

Commit 54bd9e6

Browse files
committed
docs: don't claim struct layout is specified, but mention repr
1 parent 43f040d commit 54bd9e6

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

src/doc/guide-ffi.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ struct RustObject {
268268
// other members
269269
}
270270
271-
extern fn callback(target: *mut RustObject, a:i32) {
271+
extern "C" fn callback(target: *mut RustObject, a:i32) {
272272
println!("I'm called from C with value {0}", a);
273273
unsafe {
274274
// Update the value in RustObject with the value received from the callback
@@ -506,16 +506,16 @@ to define a block for all windows systems, not just x86 ones.
506506
507507
# Interoperability with foreign code
508508
509-
Rust guarantees that the layout of a `struct` is compatible with the platform's representation in C.
510-
A `#[packed]` attribute is available, which will lay out the struct members without padding.
511-
However, there are currently no guarantees about the layout of an `enum`.
509+
Rust guarantees that the layout of a `struct` is compatible with the platform's representation in C
510+
only if the `#[repr(C)]` attribute is applied to it. `#[repr(C, packed)]` can be used to lay out
511+
struct members without padding. `#[repr(C)]` can also be applied to an enum.
512512
513-
Rust's owned and managed boxes use non-nullable pointers as handles which point to the contained
513+
Rust's owned boxes (`Box<T>`) use non-nullable pointers as handles which point to the contained
514514
object. However, they should not be manually created because they are managed by internal
515-
allocators. References can safely be assumed to be non-nullable pointers directly to the
516-
type. However, breaking the borrow checking or mutability rules is not guaranteed to be safe, so
517-
prefer using raw pointers (`*`) if that's needed because the compiler can't make as many assumptions
518-
about them.
515+
allocators. References can safely be assumed to be non-nullable pointers directly to the type.
516+
However, breaking the borrow checking or mutability rules is not guaranteed to be safe, so prefer
517+
using raw pointers (`*`) if that's needed because the compiler can't make as many assumptions about
518+
them.
519519
520520
Vectors and strings share the same basic memory layout, and utilities are available in the `vec` and
521521
`str` modules for working with C APIs. However, strings are not terminated with `\0`. If you need a

src/doc/rust.md

+20-6
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,9 @@ struct Cookie;
13081308
let c = [Cookie, Cookie, Cookie, Cookie];
13091309
~~~~
13101310

1311+
The precise memory layout of a structure is not specified. One can specify a
1312+
particular layout using the [`repr` attribute](
1313+
13111314
By using the `struct_inherit` feature gate, structures may use single inheritance. A Structure may only
13121315
inherit from a single other structure, called the _super-struct_. The inheriting structure (sub-struct)
13131316
acts as if all fields in the super-struct were present in the sub-struct. Fields declared in a sub-struct
@@ -1941,6 +1944,23 @@ interpreted:
19411944
- `linkage` - on a static, this specifies the [linkage
19421945
type](http://llvm.org/docs/LangRef.html#linkage-types).
19431946

1947+
On `enum`s:
1948+
1949+
- `repr` - on C-like enums, this sets the underlying type used for
1950+
representation. Takes one argument, which is the primitive
1951+
type this enum should be represented for, or `C`, which specifies that it
1952+
should be the default `enum` size of the C ABI for that platform. Note that
1953+
enum representation in C is undefined, and this may be incorrect when the C
1954+
code is compiled with certain flags.
1955+
1956+
On `struct`s:
1957+
1958+
- `repr` - specifies the representation to use for this struct. Takes a list
1959+
of options. The currently accepted ones are `C` and `packed`, which may be
1960+
combined. `C` will use a C ABI comptible struct layout, and `packed` will
1961+
remove any padding between fields (note that this is very fragile and may
1962+
break platforms which require aligned access).
1963+
19441964
### Miscellaneous attributes
19451965

19461966
- `export_name` - on statics and functions, this determines the name of the
@@ -1958,12 +1978,6 @@ interpreted:
19581978
crate at compile-time and use any syntax extensions or lints that the crate
19591979
defines. They can both be specified, `#[phase(link, plugin)]` to use a crate
19601980
both at runtime and compiletime.
1961-
- `repr` - on C-like enums, this sets the underlying type used for
1962-
representation. Useful for FFI. Takes one argument, which is the primitive
1963-
type this enum should be represented for, or `C`, which specifies that it
1964-
should be the default `enum` size of the C ABI for that platform. Note that
1965-
enum representation in C is undefined, and this may be incorrect when the C
1966-
code is compiled with certain flags.
19671981
- `simd` - on certain tuple structs, derive the arithmetic operators, which
19681982
lower to the target's SIMD instructions, if any; the `simd` feature gate
19691983
is necessary to use this attribute.

0 commit comments

Comments
 (0)