Skip to content

Commit 4d15fc3

Browse files
committed
---
yaml --- r: 128565 b: refs/heads/master c: 1028120 h: refs/heads/master i: 128563: fdc3831 v: v3
1 parent 7add43f commit 4d15fc3

Some content is hidden

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

56 files changed

+360
-904
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: f92015f71b3a49abd490e704a011d14786f6bf87
2+
refs/heads/master: 1028120c40018763de11a81d0bf9bc981622d0ae
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: a86d9ad15e339ab343a12513f9c90556f677b9ca
55
refs/heads/try: 414b31299172917f72458502d811da395f7b7b11

trunk/src/doc/guide-ffi.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,12 @@ Rust code:
263263
264264
~~~~no_run
265265
266-
#[repr(C)]
267266
struct RustObject {
268267
a: i32,
269268
// other members
270269
}
271270
272-
extern "C" fn callback(target: *mut RustObject, a:i32) {
271+
extern fn callback(target: *mut RustObject, a:i32) {
273272
println!("I'm called from C with value {0}", a);
274273
unsafe {
275274
// Update the value in RustObject with the value received from the callback
@@ -507,16 +506,16 @@ to define a block for all windows systems, not just x86 ones.
507506
508507
# Interoperability with foreign code
509508
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.
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`.
513512
514-
Rust's owned boxes (`Box<T>`) use non-nullable pointers as handles which point to the contained
513+
Rust's owned and managed boxes use non-nullable pointers as handles which point to the contained
515514
object. However, they should not be manually created because they are managed by internal
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.
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.
520519
521520
Vectors and strings share the same basic memory layout, and utilities are available in the `vec` and
522521
`str` modules for working with C APIs. However, strings are not terminated with `\0`. If you need a

trunk/src/doc/guide.md

Lines changed: 0 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -3668,173 +3668,6 @@ In order to truly understand this error, we have to learn a few new concepts:
36683668

36693669
## Ownership, borrowing, and lifetimes
36703670

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

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

trunk/src/doc/rust.md

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,9 +1308,6 @@ 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-
13141311
By using the `struct_inherit` feature gate, structures may use single inheritance. A Structure may only
13151312
inherit from a single other structure, called the _super-struct_. The inheriting structure (sub-struct)
13161313
acts as if all fields in the super-struct were present in the sub-struct. Fields declared in a sub-struct
@@ -1944,23 +1941,6 @@ interpreted:
19441941
- `linkage` - on a static, this specifies the [linkage
19451942
type](http://llvm.org/docs/LangRef.html#linkage-types).
19461943

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-
19641944
### Miscellaneous attributes
19651945

19661946
- `export_name` - on statics and functions, this determines the name of the
@@ -1978,6 +1958,12 @@ On `struct`s:
19781958
crate at compile-time and use any syntax extensions or lints that the crate
19791959
defines. They can both be specified, `#[phase(link, plugin)]` to use a crate
19801960
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.
19811967
- `simd` - on certain tuple structs, derive the arithmetic operators, which
19821968
lower to the target's SIMD instructions, if any; the `simd` feature gate
19831969
is necessary to use this attribute.

trunk/src/libcore/simd.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
#[experimental]
4141
#[simd]
4242
#[deriving(Show)]
43-
#[repr(C)]
4443
pub struct i8x16(pub i8, pub i8, pub i8, pub i8,
4544
pub i8, pub i8, pub i8, pub i8,
4645
pub i8, pub i8, pub i8, pub i8,
@@ -49,26 +48,22 @@ pub struct i8x16(pub i8, pub i8, pub i8, pub i8,
4948
#[experimental]
5049
#[simd]
5150
#[deriving(Show)]
52-
#[repr(C)]
5351
pub struct i16x8(pub i16, pub i16, pub i16, pub i16,
5452
pub i16, pub i16, pub i16, pub i16);
5553

5654
#[experimental]
5755
#[simd]
5856
#[deriving(Show)]
59-
#[repr(C)]
6057
pub struct i32x4(pub i32, pub i32, pub i32, pub i32);
6158

6259
#[experimental]
6360
#[simd]
6461
#[deriving(Show)]
65-
#[repr(C)]
6662
pub struct i64x2(pub i64, pub i64);
6763

6864
#[experimental]
6965
#[simd]
7066
#[deriving(Show)]
71-
#[repr(C)]
7267
pub struct u8x16(pub u8, pub u8, pub u8, pub u8,
7368
pub u8, pub u8, pub u8, pub u8,
7469
pub u8, pub u8, pub u8, pub u8,
@@ -77,30 +72,25 @@ pub struct u8x16(pub u8, pub u8, pub u8, pub u8,
7772
#[experimental]
7873
#[simd]
7974
#[deriving(Show)]
80-
#[repr(C)]
8175
pub struct u16x8(pub u16, pub u16, pub u16, pub u16,
8276
pub u16, pub u16, pub u16, pub u16);
8377

8478
#[experimental]
8579
#[simd]
8680
#[deriving(Show)]
87-
#[repr(C)]
8881
pub struct u32x4(pub u32, pub u32, pub u32, pub u32);
8982

9083
#[experimental]
9184
#[simd]
9285
#[deriving(Show)]
93-
#[repr(C)]
9486
pub struct u64x2(pub u64, pub u64);
9587

9688
#[experimental]
9789
#[simd]
9890
#[deriving(Show)]
99-
#[repr(C)]
10091
pub struct f32x4(pub f32, pub f32, pub f32, pub f32);
10192

10293
#[experimental]
10394
#[simd]
10495
#[deriving(Show)]
105-
#[repr(C)]
10696
pub struct f64x2(pub f64, pub f64);

0 commit comments

Comments
 (0)