Skip to content
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

Rollup of 10 pull requests #24637

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
1db3df8
Fix the dependency for the pretty-rpass-full tests
rprichard Apr 17, 2015
566d653
Fix run-make/simd-ffi to work with parallel make check.
rprichard Apr 17, 2015
be08d35
Simplify alloc::arc::Arc example in doc-comment
frewsxcv Apr 20, 2015
2137088
TRPL editing: enums
steveklabnik Apr 18, 2015
836c8a8
TRPL editing: match
steveklabnik Apr 18, 2015
9661efa
TRPL editing: patterns
steveklabnik Apr 18, 2015
44db46a
TRPL editing: move patterns back
steveklabnik Apr 18, 2015
7f43c57
TRPL editing: structs
steveklabnik Apr 18, 2015
f81b1fc
TRPL edits: method syntax
steveklabnik Apr 18, 2015
a3a2049
Move drop back in TRPL TOC
steveklabnik Apr 18, 2015
73d3d68
TRPL editing: vectors
steveklabnik Apr 18, 2015
5b54a4f
TRPL copyedits: strings
steveklabnik Apr 18, 2015
1646ebd
move Deref coercions to syntax and semantics
steveklabnik Apr 18, 2015
e289b68
TRPL edits: traits
steveklabnik Apr 18, 2015
1c48227
Remove inverse example from generics part of TRPL
steveklabnik Apr 18, 2015
14af257
TRPL edits: generics
steveklabnik Apr 18, 2015
1dd8a65
Make iterator struct docs more consistent.
steveklabnik Apr 20, 2015
89ef637
remove bad example from PartialEq docs
steveklabnik Apr 20, 2015
ac09864
Clean up Box documentation.
steveklabnik Apr 20, 2015
fbd3261
Fix small typo in TRPL intro
steveklabnik Apr 20, 2015
9a65045
TRPL: casting
steveklabnik Apr 20, 2015
0f20c9d
Remove feature gated examples from the reference.
steveklabnik Apr 20, 2015
1e29abe
Add common macros to TRPL
steveklabnik Apr 17, 2015
09a0834
Rollup merge of #24516 - steveklabnik:gh22621, r=alexcrichton
steveklabnik Apr 20, 2015
f328bb5
Rollup merge of #24537 - rprichard:fix-parallel-check, r=brson
steveklabnik Apr 20, 2015
4cf58fb
Rollup merge of #24571 - steveklabnik:editing, r=alexcrichton
steveklabnik Apr 20, 2015
4b3310b
Rollup merge of #24577 - steveklabnik:gh24573, r=alexcrichton
steveklabnik Apr 20, 2015
a707915
Rollup merge of #24625 - frewsxcv:patch-16, r=steveklabnik
steveklabnik Apr 20, 2015
2587814
Rollup merge of #24627 - steveklabnik:gh24008, r=alexcrichton
steveklabnik Apr 20, 2015
746a386
Rollup merge of #24628 - steveklabnik:gh24173, r=alexcrichton
steveklabnik Apr 20, 2015
40bd2df
Rollup merge of #24629 - steveklabnik:gh24511, r=alexcrichton
steveklabnik Apr 20, 2015
e8db262
Rollup merge of #24630 - steveklabnik:quickfix, r=alexcrichton
steveklabnik Apr 20, 2015
fb57ba0
Rollup merge of #24632 - steveklabnik:casting_guide, r=alexcrichton
steveklabnik Apr 20, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions mk/tests.mk
Original file line number Diff line number Diff line change
Expand Up @@ -753,20 +753,22 @@ PRETTY_DEPS_pretty-rpass-full = $(RPASS_FULL_TESTS)
PRETTY_DEPS_pretty-rfail = $(RFAIL_TESTS)
PRETTY_DEPS_pretty-bench = $(BENCH_TESTS)
PRETTY_DEPS_pretty-pretty = $(PRETTY_TESTS)
# The stage- and host-specific dependencies are for e.g. macro_crate_test which pulls in
# external crates.
PRETTY_DEPS$(1)_H_$(3)_pretty-rpass =
PRETTY_DEPS$(1)_H_$(3)_pretty-rpass-full = $$(HLIB$(1)_H_$(3))/stamp.syntax $$(HLIB$(1)_H_$(3))/stamp.rustc
PRETTY_DEPS$(1)_H_$(3)_pretty-rfail =
PRETTY_DEPS$(1)_H_$(3)_pretty-bench =
PRETTY_DEPS$(1)_H_$(3)_pretty-pretty =
PRETTY_DIRNAME_pretty-rpass = run-pass
PRETTY_DIRNAME_pretty-rpass-valgrind = run-pass-valgrind
PRETTY_DIRNAME_pretty-rpass-full = run-pass-fulldeps
PRETTY_DIRNAME_pretty-rfail = run-fail
PRETTY_DIRNAME_pretty-bench = bench
PRETTY_DIRNAME_pretty-pretty = pretty

define DEF_PRETTY_FULLDEPS
PRETTY_DEPS$(1)_T_$(2)_H_$(3)_pretty-rpass-full = $$(CSREQ$(1)_T_$(2)_H_$(3))
endef

$(foreach host,$(CFG_HOST), \
$(foreach target,$(CFG_TARGET), \
$(foreach stage,$(STAGES), \
$(eval $(call DEF_PRETTY_FULLDEPS,$(stage),$(target),$(host))))))

define DEF_RUN_PRETTY_TEST

PRETTY_ARGS$(1)-T-$(2)-H-$(3)-$(4) := \
Expand All @@ -780,7 +782,7 @@ check-stage$(1)-T-$(2)-H-$(3)-$(4)-exec: $$(call TEST_OK_FILE,$(1),$(2),$(3),$(4
$$(call TEST_OK_FILE,$(1),$(2),$(3),$(4)): \
$$(TEST_SREQ$(1)_T_$(2)_H_$(3)) \
$$(PRETTY_DEPS_$(4)) \
$$(PRETTY_DEPS$(1)_H_$(3)_$(4))
$$(PRETTY_DEPS$(1)_T_$(2)_H_$(3)_$(4))
@$$(call E, run pretty-rpass [$(2)]: $$<)
$$(Q)touch $$@.start_time
$$(Q)$$(call CFG_RUN_CTEST_$(2),$(1),$$<,$(3)) \
Expand Down
137 changes: 22 additions & 115 deletions src/doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,7 @@ Use declarations support a number of convenient shortcuts:

An example of `use` declarations:

```
# #![feature(core)]
```rust
use std::option::Option::{Some, None};
use std::collections::hash_map::{self, HashMap};

Expand Down Expand Up @@ -1031,16 +1030,17 @@ declarations.
An example of what will and will not work for `use` items:

```
# #![feature(core)]
# #![allow(unused_imports)]
use foo::core::iter; // good: foo is at the root of the crate
use foo::baz::foobaz; // good: foo is at the root of the crate

mod foo {
extern crate core;

use foo::core::iter; // good: foo is at crate root
// use core::iter; // bad: core is not at the crate root
mod example {
pub mod iter {}
}

use foo::example::iter; // good: foo is at crate root
// use example::iter; // bad: core is not at the crate root
use self::baz::foobaz; // good: self refers to module 'foo'
use foo::bar::foobar; // good: foo is at crate root

Expand Down Expand Up @@ -1368,17 +1368,14 @@ a = Animal::Cat;

Enumeration constructors can have either named or unnamed fields:

```
# #![feature(struct_variant)]
# fn main() {
```rust
enum Animal {
Dog (String, f64),
Cat { name: String, weight: f64 }
}

let mut a: Animal = Animal::Dog("Cocoa".to_string(), 37.2);
a = Animal::Cat { name: "Spotty".to_string(), weight: 2.7 };
# }
```

In this example, `Cat` is a _struct-like enum variant_,
Expand Down Expand Up @@ -1718,17 +1715,6 @@ Functions within external blocks are declared in the same way as other Rust
functions, with the exception that they may not have a body and are instead
terminated by a semicolon.

```
# #![feature(libc)]
extern crate libc;
use libc::{c_char, FILE};

extern {
fn fopen(filename: *const c_char, mode: *const c_char) -> *mut FILE;
}
# fn main() {}
```

Functions within external blocks may be called by Rust code, just like
functions defined in Rust. The Rust compiler automatically translates between
the Rust ABI and the foreign ABI.
Expand All @@ -1739,7 +1725,7 @@ By default external blocks assume that the library they are calling uses the
standard C "cdecl" ABI. Other ABIs may be specified using an `abi` string, as
shown here:

```{.ignore}
```ignore
// Interface to the Windows API
extern "stdcall" { }
```
Expand Down Expand Up @@ -3231,55 +3217,7 @@ expression.

In a pattern whose head expression has an `enum` type, a placeholder (`_`)
stands for a *single* data field, whereas a wildcard `..` stands for *all* the
fields of a particular variant. For example:

```
#![feature(box_patterns)]
#![feature(box_syntax)]
enum List<X> { Nil, Cons(X, Box<List<X>>) }

fn main() {
let x: List<i32> = List::Cons(10, box List::Cons(11, box List::Nil));

match x {
List::Cons(_, box List::Nil) => panic!("singleton list"),
List::Cons(..) => return,
List::Nil => panic!("empty list")
}
}
```

The first pattern matches lists constructed by applying `Cons` to any head
value, and a tail value of `box Nil`. The second pattern matches _any_ list
constructed with `Cons`, ignoring the values of its arguments. The difference
between `_` and `..` is that the pattern `C(_)` is only type-correct if `C` has
exactly one argument, while the pattern `C(..)` is type-correct for any enum
variant `C`, regardless of how many arguments `C` has.

Used inside an array pattern, `..` stands for any number of elements, when the
`advanced_slice_patterns` feature gate is turned on. This wildcard can be used
at most once for a given array, which implies that it cannot be used to
specifically match elements that are at an unknown distance from both ends of a
array, like `[.., 42, ..]`. If preceded by a variable name, it will bind the
corresponding slice to the variable. Example:

```
# #![feature(advanced_slice_patterns, slice_patterns)]
fn is_symmetric(list: &[u32]) -> bool {
match list {
[] | [_] => true,
[x, inside.., y] if x == y => is_symmetric(inside),
_ => false
}
}

fn main() {
let sym = &[0, 1, 4, 2, 4, 1, 0];
let not_sym = &[0, 1, 7, 2, 4, 1, 0];
assert!(is_symmetric(sym));
assert!(!is_symmetric(not_sym));
}
```
fields of a particular variant.

A `match` behaves differently depending on whether or not the head expression
is an [lvalue or an rvalue](#lvalues,-rvalues-and-temporaries). If the head
Expand All @@ -3298,30 +3236,15 @@ the inside of the match.
An example of a `match` expression:

```
#![feature(box_patterns)]
#![feature(box_syntax)]
# fn process_pair(a: i32, b: i32) { }
# fn process_ten() { }

enum List<X> { Nil, Cons(X, Box<List<X>>) }

fn main() {
let x: List<i32> = List::Cons(10, box List::Cons(11, box List::Nil));
let x = 1;

match x {
List::Cons(a, box List::Cons(b, _)) => {
process_pair(a, b);
}
List::Cons(10, _) => {
process_ten();
}
List::Nil => {
return;
}
_ => {
panic!();
}
}
match x {
1 => println!("one"),
2 => println!("two"),
3 => println!("three"),
4 => println!("four"),
5 => println!("five"),
_ => println!("something else"),
}
```

Expand All @@ -3334,28 +3257,12 @@ Subpatterns can also be bound to variables by the use of the syntax `variable @
subpattern`. For example:

```
#![feature(box_patterns)]
#![feature(box_syntax)]

enum List { Nil, Cons(u32, Box<List>) }
let x = 1;

fn is_sorted(list: &List) -> bool {
match *list {
List::Nil | List::Cons(_, box List::Nil) => true,
List::Cons(x, ref r @ box List::Cons(_, _)) => {
match *r {
box List::Cons(y, _) => (x <= y) && is_sorted(&**r),
_ => panic!()
}
}
}
}

fn main() {
let a = List::Cons(6, box List::Cons(7, box List::Cons(42, box List::Nil)));
assert!(is_sorted(&a));
match x {
e @ 1 ... 5 => println!("got a range element {}", e),
_ => println!("anything"),
}

```

Patterns can also dereference pointers by using the `&`, `&mut` and `box`
Expand Down
2 changes: 1 addition & 1 deletion src/doc/trpl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ fn main() {

Rust has [move semantics][move] by default, so if we want to make a copy of some
data, we call the `clone()` method. In this example, `y` is no longer a reference
to the vector stored in `x`, but a copy of its first element, `"hello"`. Now
to the vector stored in `x`, but a copy of its first element, `"Hello"`. Now
that we don’t have a reference, our `push()` works just fine.

[move]: move-semantics.html
Expand Down
8 changes: 4 additions & 4 deletions src/doc/trpl/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* [Concurrency](concurrency.md)
* [Error Handling](error-handling.md)
* [FFI](ffi.md)
* [Deref coercions](deref-coercions.md)
* [Syntax and Semantics](syntax-and-semantics.md)
* [Variable Bindings](variable-bindings.md)
* [Functions](functions.md)
Expand All @@ -30,15 +29,15 @@
* [Move semantics](move-semantics.md)
* [Enums](enums.md)
* [Match](match.md)
* [Patterns](patterns.md)
* [Structs](structs.md)
* [Patterns](patterns.md)
* [Method Syntax](method-syntax.md)
* [Drop](drop.md)
* [Vectors](vectors.md)
* [Strings](strings.md)
* [Generics](generics.md)
* [Traits](traits.md)
* [Operators and Overloading](operators-and-overloading.md)
* [Generics](generics.md)
* [Drop](drop.md)
* [if let](if-let.md)
* [Trait Objects](trait-objects.md)
* [Closures](closures.md)
Expand All @@ -53,6 +52,7 @@
* [Casting between types](casting-between-types.md)
* [Associated Types](associated-types.md)
* [Unsized Types](unsized-types.md)
* [Deref coercions](deref-coercions.md)
* [Macros](macros.md)
* [`unsafe` Code](unsafe-code.md)
* [Nightly Rust](nightly-rust.md)
Expand Down
88 changes: 87 additions & 1 deletion src/doc/trpl/casting-between-types.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,89 @@
% Casting Between Types

Coming Soon
Rust, with its focus on safety, provides two different ways of casting
different types between each other. The first, `as`, is for safe casts.
In contrast, `transmute` allows for arbitrary casting, and is one of the
most dangerous features of Rust!

# `as`

The `as` keyword does basic casting:

```rust
let x: i32 = 5;

let y = x as i64;
```

It only allows certain kinds of casting, however:

```rust,ignore
let a = [0u8, 0u8, 0u8, 0u8];

let b = a as u32; // four eights makes 32
```

This errors with:

```text
error: non-scalar cast: `[u8; 4]` as `u32`
let b = a as u32; // four eights makes 32
^~~~~~~~
```

It’s a ‘non-scalar cast’ because we have multiple values here: the four
elements of the array. These kinds of casts are very dangerous, because they
make assumptions about the way that multiple underlying strucutres are
implemented. For this, we need something more dangerous.

# `transmute`

The `transmute` function is provided by a [compiler intrinsic][intrinsics], and
what it does is very simple, but very scary. It tells Rust to treat a value of
one type as though it were another type. It does this regardless of the
typechecking system, and just completely trusts you.

[intrinsic]: intrinsics.html

In our previous example, we know that an array of four `u8`s represents a `u32`
properly, and so we want to do the cast. Using `transmute` instead of `as`,
Rust lets us:

```rust
use std::mem;

unsafe {
let a = [0u8, 0u8, 0u8, 0u8];

let b = mem::transmute::<[u8; 4], u32>(a);
}
```

We have to wrap the operation in an `unsafe` block, but this will compile
successfully. Technically, only the `mem::transmute` call itself needs to be in
the block, but it's nice in this case to enclose everything related, so you
know where to look. In this case, the details about `a` are also important, and
so they're in the block. You'll see code in either style, sometimes the context
is too far away, and wrapping all of the code in `unsafe` isn't a great idea.

While `transmute` does very little checking, it will at least make sure that
the types are the same size. This errors:

```rust,ignore
use std::mem;

unsafe {
let a = [0u8, 0u8, 0u8, 0u8];

let b = mem::transmute::<[u8; 4], u64>(a);
}
```

with:

```text
error: transmute called on types with different sizes: [u8; 4] (32 bits) to u64
(64 bits)
```

Other than that, you're on your own!
Loading