Skip to content

Commit 18eccc4

Browse files
committed
---
yaml --- r: 129467 b: refs/heads/snap-stage3 c: 19a44c7 h: refs/heads/master i: 129465: c594752 129463: dc3a76a v: v3
1 parent 9b3c721 commit 18eccc4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1131
-340
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 566b470e138e929e8a93d613372db1ba177c494f
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 69fbef1d87ffc4807ff75676f30c7ea30bb11a96
4+
refs/heads/snap-stage3: 19a44c73c246ad98f285aa801433beca674b1ad4
55
refs/heads/try: 80b45ddbd351f0a4a939c3a3c4e20b4defec4b35
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/doc/guide-ffi.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,13 @@ Rust code:
263263
264264
~~~~no_run
265265
266+
#[repr(C)]
266267
struct RustObject {
267268
a: i32,
268269
// other members
269270
}
270271
271-
extern fn callback(target: *mut RustObject, a:i32) {
272+
extern "C" fn callback(target: *mut RustObject, a:i32) {
272273
println!("I'm called from C with value {0}", a);
273274
unsafe {
274275
// Update the value in RustObject with the value received from the callback
@@ -506,16 +507,16 @@ to define a block for all windows systems, not just x86 ones.
506507
507508
# Interoperability with foreign code
508509
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`.
510+
Rust guarantees that the layout of a `struct` is compatible with the platform's representation in C
511+
only if the `#[repr(C)]` attribute is applied to it. `#[repr(C, packed)]` can be used to lay out
512+
struct members without padding. `#[repr(C)]` can also be applied to an enum.
512513
513-
Rust's owned and managed boxes use non-nullable pointers as handles which point to the contained
514+
Rust's owned boxes (`Box<T>`) use non-nullable pointers as handles which point to the contained
514515
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.
516+
allocators. References can safely be assumed to be non-nullable pointers directly to the type.
517+
However, breaking the borrow checking or mutability rules is not guaranteed to be safe, so prefer
518+
using raw pointers (`*`) if that's needed because the compiler can't make as many assumptions about
519+
them.
519520
520521
Vectors and strings share the same basic memory layout, and utilities are available in the `vec` and
521522
`str` modules for working with C APIs. However, strings are not terminated with `\0`. If you need a

branches/snap-stage3/src/doc/guide.md

Lines changed: 171 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,8 +1073,8 @@ destructuring `let`.
10731073
## Enums
10741074

10751075
Finally, Rust has a "sum type", an **enum**. Enums are an incredibly useful
1076-
feature of Rust, and are used throughout the standard library. Enums look
1077-
like this:
1076+
feature of Rust, and are used throughout the standard library. This is an enum
1077+
that is provided by the Rust standard library:
10781078

10791079
```{rust}
10801080
enum Ordering {
@@ -1084,9 +1084,8 @@ enum Ordering {
10841084
}
10851085
```
10861086

1087-
This is an enum that is provided by the Rust standard library. An `Ordering`
1088-
can only be _one_ of `Less`, `Equal`, or `Greater` at any given time. Here's
1089-
an example:
1087+
An `Ordering` can only be _one_ of `Less`, `Equal`, or `Greater` at any given
1088+
time. Here's an example:
10901089

10911090
```{rust}
10921091
fn cmp(a: int, b: int) -> Ordering {
@@ -3668,6 +3667,173 @@ In order to truly understand this error, we have to learn a few new concepts:
36683667

36693668
## Ownership, borrowing, and lifetimes
36703669

3670+
Whenever a resource of some kind is created, something must be responsible
3671+
for destroying that resource as well. Given that we're discussing pointers
3672+
right now, let's discuss this in the context of memory allocation, though
3673+
it applies to other resources as well.
3674+
3675+
When you allocate heap memory, you need a mechanism to free that memory. Many
3676+
languages let the programmer control the allocation, and then use a garbage
3677+
collector to handle the deallocation. This is a valid, time-tested strategy,
3678+
but it's not without its drawbacks. Because the programmer does not have to
3679+
think as much about deallocation, allocation becomes something commonplace,
3680+
because it's easy. And if you need precise control over when something is
3681+
deallocated, leaving it up to your runtime can make this difficult.
3682+
3683+
Rust chooses a different path, and that path is called **ownership**. Any
3684+
binding that creates a resource is the **owner** of that resource. Being an
3685+
owner gives you three privileges, with two restrictions:
3686+
3687+
1. You control when that resource is deallocated.
3688+
2. You may lend that resource, immutably, to as many borrowers as you'd like.
3689+
3. You may lend that resource, mutably, to a single borrower. **BUT**
3690+
4. Once you've done so, you may not also lend it out otherwise, mutably or
3691+
immutably.
3692+
5. You may not lend it out mutably if you're currently lending it to someone.
3693+
3694+
What's up with all this 'lending' and 'borrowing'? When you allocate memory,
3695+
you get a pointer to that memory. This pointer allows you to manipulate said
3696+
memory. If you are the owner of a pointer, then you may allow another
3697+
binding to temporarily borrow that pointer, and then they can manipulate the
3698+
memory. The length of time that the borrower is borrowing the pointer
3699+
from you is called a **lifetime**.
3700+
3701+
If two distinct bindings share a pointer, and the memory that pointer points to
3702+
is immutable, then there are no problems. But if it's mutable, both pointers
3703+
can attempt to write to the memory at the same time, causing a **race
3704+
condition**. Therefore, if someone wants to mutate something that they've
3705+
borrowed from you, you must not have lent out that pointer to anyone else.
3706+
3707+
Rust has a sophisticated system called the **borrow checker** to make sure that
3708+
everyone plays by these rules. At compile time, it verifies that none of these
3709+
rules are broken. If there's no problem, our program compiles successfully, and
3710+
there is no runtime overhead for any of this. The borrow checker works only at
3711+
compile time. If the borrow checker did find a problem, it will report a
3712+
**lifetime error**, and your program will refuse to compile.
3713+
3714+
That's a lot to take in. It's also one of the _most_ important concepts in
3715+
all of Rust. Let's see this syntax in action:
3716+
3717+
```{rust}
3718+
{
3719+
let x = 5i; // x is the owner of this integer, which is memory on the stack.
3720+
3721+
// other code here...
3722+
3723+
} // privilege 1: when x goes out of scope, this memory is deallocated
3724+
3725+
/// this function borrows an integer. It's given back automatically when the
3726+
/// function returns.
3727+
fn foo(x: &int) -> &int { x }
3728+
3729+
{
3730+
let x = 5i; // x is the owner of this integer, which is memory on the stack.
3731+
3732+
// privilege 2: you may lend that resource, to as many borrowers as you'd like
3733+
let y = &x;
3734+
let z = &x;
3735+
3736+
foo(&x); // functions can borrow too!
3737+
3738+
let a = &x; // we can do this alllllll day!
3739+
}
3740+
3741+
{
3742+
let mut x = 5i; // x is the owner of this integer, which is memory on the stack.
3743+
3744+
let y = &mut x; // privilege 3: you may lend that resource to a single borrower,
3745+
// mutably
3746+
}
3747+
```
3748+
3749+
If you are a borrower, you get a few privileges as well, but must also obey a
3750+
restriction:
3751+
3752+
1. If the borrow is immutable, you may read the data the pointer points to.
3753+
2. If the borrow is mutable, you may read and write the data the pointer points to.
3754+
3. You may lend the pointer to someone else in an immutable fashion, **BUT**
3755+
4. When you do so, they must return it to you before you must give your own
3756+
borrow back.
3757+
3758+
This last requirement can seem odd, but it also makes sense. If you have to
3759+
return something, and you've lent it to someone, they need to give it back to
3760+
you for you to give it back! If we didn't, then the owner could deallocate
3761+
the memory, and the person we've loaned it out to would have a pointer to
3762+
invalid memory. This is called a 'dangling pointer.'
3763+
3764+
Let's re-examine the error that led us to talk about all of this, which was a
3765+
violation of the restrictions placed on owners who lend something out mutably.
3766+
The code:
3767+
3768+
```{rust,ignore}
3769+
let mut x = 5i;
3770+
let y = &mut x;
3771+
let z = &mut x;
3772+
```
3773+
3774+
The error:
3775+
3776+
```{notrust,ignore}
3777+
error: cannot borrow `x` as mutable more than once at a time
3778+
let z = &mut x;
3779+
^
3780+
note: previous borrow of `x` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `x` until the borrow ends
3781+
let y = &mut x;
3782+
^
3783+
note: previous borrow ends here
3784+
fn main() {
3785+
let mut x = 5i;
3786+
let y = &mut x;
3787+
let z = &mut x;
3788+
}
3789+
^
3790+
```
3791+
3792+
This error comes in three parts. Let's go over each in turn.
3793+
3794+
```{notrust,ignore}
3795+
error: cannot borrow `x` as mutable more than once at a time
3796+
let z = &mut x;
3797+
^
3798+
```
3799+
3800+
This error states the restriction: you cannot lend out something mutable more
3801+
than once at the same time. The borrow checker knows the rules!
3802+
3803+
```{notrust,ignore}
3804+
note: previous borrow of `x` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `x` until the borrow ends
3805+
let y = &mut x;
3806+
^
3807+
```
3808+
3809+
Some compiler errors come with notes to help you fix the error. This error comes
3810+
with two notes, and this is the first. This note informs us of exactly where
3811+
the first mutable borrow occurred. The error showed us the second. So now we
3812+
see both parts of the problem. It also alludes to rule #3, by reminding us that
3813+
we can't change `x` until the borrow is over.
3814+
3815+
```{notrust,ignore}
3816+
note: previous borrow ends here
3817+
fn main() {
3818+
let mut x = 5i;
3819+
let y = &mut x;
3820+
let z = &mut x;
3821+
}
3822+
^
3823+
```
3824+
3825+
Here's the second note, which lets us know where the first borrow would be over.
3826+
This is useful, because if we wait to try to borrow `x` after this borrow is
3827+
over, then everything will work.
3828+
3829+
These rules are very simple, but that doesn't mean that they're easy. For more
3830+
advanced patterns, please consult the [Lifetime Guide](guide-lifetimes.html).
3831+
You'll also learn what this type signature with the `'a` syntax is:
3832+
3833+
```{rust,ignore}
3834+
pub fn as_maybe_owned(&self) -> MaybeOwned<'a> { ... }
3835+
```
3836+
36713837
## Boxes
36723838

36733839
All of our references so far have been to variables we've created on the stack.

branches/snap-stage3/src/doc/rust.md

Lines changed: 20 additions & 6 deletions
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](#ffi-attributes).
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.

branches/snap-stage3/src/libcore/mem.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ use ptr;
1919

2020
pub use intrinsics::transmute;
2121

22+
/// Moves a thing into the void.
23+
///
24+
/// The forget function will take ownership of the provided value but neglect
25+
/// to run any required cleanup or memory management operations on it.
26+
///
27+
/// This function is the unsafe version of the `drop` function because it does
28+
/// not run any destructors.
29+
#[stable]
30+
pub use intrinsics::forget;
31+
2232
/// Returns the size of a type in bytes.
2333
#[inline]
2434
#[stable]
@@ -337,17 +347,6 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
337347
#[stable]
338348
pub fn drop<T>(_x: T) { }
339349

340-
/// Moves a thing into the void.
341-
///
342-
/// The forget function will take ownership of the provided value but neglect
343-
/// to run any required cleanup or memory management operations on it.
344-
///
345-
/// This function is the unsafe version of the `drop` function because it does
346-
/// not run any destructors.
347-
#[inline]
348-
#[stable]
349-
pub unsafe fn forget<T>(thing: T) { intrinsics::forget(thing) }
350-
351350
/// Interprets `src` as `&U`, and then reads `src` without moving the contained
352351
/// value.
353352
///

branches/snap-stage3/src/libcore/simd.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#[experimental]
4141
#[simd]
4242
#[deriving(Show)]
43+
#[repr(C)]
4344
pub struct i8x16(pub i8, pub i8, pub i8, pub i8,
4445
pub i8, pub i8, pub i8, pub i8,
4546
pub i8, pub i8, pub i8, pub i8,
@@ -48,22 +49,26 @@ pub struct i8x16(pub i8, pub i8, pub i8, pub i8,
4849
#[experimental]
4950
#[simd]
5051
#[deriving(Show)]
52+
#[repr(C)]
5153
pub struct i16x8(pub i16, pub i16, pub i16, pub i16,
5254
pub i16, pub i16, pub i16, pub i16);
5355

5456
#[experimental]
5557
#[simd]
5658
#[deriving(Show)]
59+
#[repr(C)]
5760
pub struct i32x4(pub i32, pub i32, pub i32, pub i32);
5861

5962
#[experimental]
6063
#[simd]
6164
#[deriving(Show)]
65+
#[repr(C)]
6266
pub struct i64x2(pub i64, pub i64);
6367

6468
#[experimental]
6569
#[simd]
6670
#[deriving(Show)]
71+
#[repr(C)]
6772
pub struct u8x16(pub u8, pub u8, pub u8, pub u8,
6873
pub u8, pub u8, pub u8, pub u8,
6974
pub u8, pub u8, pub u8, pub u8,
@@ -72,25 +77,30 @@ pub struct u8x16(pub u8, pub u8, pub u8, pub u8,
7277
#[experimental]
7378
#[simd]
7479
#[deriving(Show)]
80+
#[repr(C)]
7581
pub struct u16x8(pub u16, pub u16, pub u16, pub u16,
7682
pub u16, pub u16, pub u16, pub u16);
7783

7884
#[experimental]
7985
#[simd]
8086
#[deriving(Show)]
87+
#[repr(C)]
8188
pub struct u32x4(pub u32, pub u32, pub u32, pub u32);
8289

8390
#[experimental]
8491
#[simd]
8592
#[deriving(Show)]
93+
#[repr(C)]
8694
pub struct u64x2(pub u64, pub u64);
8795

8896
#[experimental]
8997
#[simd]
9098
#[deriving(Show)]
99+
#[repr(C)]
91100
pub struct f32x4(pub f32, pub f32, pub f32, pub f32);
92101

93102
#[experimental]
94103
#[simd]
95104
#[deriving(Show)]
105+
#[repr(C)]
96106
pub struct f64x2(pub f64, pub f64);

0 commit comments

Comments
 (0)