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 19 pull requests #31147

Closed
wants to merge 43 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
25d068e
Showed the difference between `map` and `and_then` with an example.
sandeep-datta Jan 17, 2016
0f8fc2c
Improve CStr::from_ptr example in docs
andreabedini Jan 19, 2016
9d77694
Fix type parameter default error to mention type and trait definitions
bluss Jan 18, 2016
796f158
Add test for #30123
brson Jan 19, 2016
ccba72e
Add examples of the Entry API to the HashMap documentation.
nathankleyn Jan 19, 2016
6849b6d
Remove leftover import of `std::str` in doc test
andreabedini Jan 19, 2016
a6778a2
Improve E0317 long diagnostics
Manishearth Jan 20, 2016
5f20143
Fixed line wrapping.
sandeep-datta Jan 20, 2016
132ec2c
Correct code in E0382 explanation
apasel422 Jan 20, 2016
6271ee9
tweak colors for a11y
steveklabnik Jan 20, 2016
5763b86
Add alt tags for logos
steveklabnik Jan 20, 2016
257a1ec
tweak struct colors
steveklabnik Jan 20, 2016
c449f04
tweak trait css
steveklabnik Jan 20, 2016
c158fd9
Add Alexis thesis to bibliography
brson Jan 20, 2016
52c89ee
doc: improve grammar
tshepang Jan 21, 2016
2a7bef6
doc: this sentence did not read well
tshepang Jan 21, 2016
48aa5ef
Fix typo in "Getting Started" section of the book
apasel422 Jan 22, 2016
c94b14a
update link to unwind in book
steveklabnik Jan 22, 2016
9624b68
book: Clarify that trait or type must be in same crate as impl
kamalmarhubi Jan 22, 2016
a559577
Forward reference crates and modules chapter
kamalmarhubi Jan 22, 2016
97f9cad
E0210: Add a warning about type aliases
marcbowes Jan 22, 2016
f81a11b
Document that BTreeMap iteration is in order
mbrubeck Jan 22, 2016
6a6e9a9
Fix the missing line in the guessing-game.md
vessd Jan 23, 2016
ac27ec2
Semaphore not sempahore
angelsl Jan 23, 2016
1201943
Rollup merge of #30971 - SDX2000:docfixes, r=steveklabnik
steveklabnik Jan 23, 2016
68e51c3
Rollup merge of #30997 - bluss:trait-default, r=nikomatsakis
steveklabnik Jan 23, 2016
4f00383
Rollup merge of #31019 - andreabedini:patch-1, r=alexcrichton
steveklabnik Jan 23, 2016
967bae4
Rollup merge of #31031 - brson:issue-30123, r=nikomatsakis
steveklabnik Jan 23, 2016
789701a
Rollup merge of #31035 - nathankleyn:improve-visibility-of-entry-api,…
steveklabnik Jan 23, 2016
01273f9
Rollup merge of #31045 - Manishearth:diag-prim-shadow, r=steveklabnik
steveklabnik Jan 23, 2016
2ff14a4
Rollup merge of #31050 - apasel422:issue-31048, r=Manishearth
steveklabnik Jan 23, 2016
cc92be6
Rollup merge of #31054 - steveklabnik:a11y, r=alexcrichton
steveklabnik Jan 23, 2016
fc51b68
Rollup merge of #31055 - steveklabnik:alt-tags, r=alexcrichton
steveklabnik Jan 23, 2016
0b40c6c
Rollup merge of #31061 - brson:bib, r=steveklabnik
steveklabnik Jan 23, 2016
bdd1507
Rollup merge of #31088 - tshepang:grammar, r=brson
steveklabnik Jan 23, 2016
9dfb4ac
Rollup merge of #31090 - tshepang:improve-sentence, r=brson
steveklabnik Jan 23, 2016
b026f7a
Rollup merge of #31111 - apasel422:issue-31103, r=steveklabnik
steveklabnik Jan 23, 2016
14f048d
Rollup merge of #31113 - steveklabnik:master, r=alexcrichton
steveklabnik Jan 23, 2016
b697b3e
Rollup merge of #31128 - kamalmarhubi:book-trait-impl-clarify, r=stev…
steveklabnik Jan 23, 2016
7c22c1a
Rollup merge of #31130 - marcbowes:master, r=nrc
steveklabnik Jan 23, 2016
96dbd8a
Rollup merge of #31136 - mbrubeck:btree-doc, r=steveklabnik
steveklabnik Jan 23, 2016
bd4c0b5
Rollup merge of #31145 - D101101:patch-1, r=steveklabnik
steveklabnik Jan 23, 2016
deefde0
Rollup merge of #31146 - angelsl:patch-1, r=steveklabnik
steveklabnik Jan 23, 2016
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
1 change: 1 addition & 0 deletions src/doc/book/bibliography.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ Language](http://www.cs.indiana.edu/~eholk/papers/hips2013.pdf). Early GPU work
Rust](http://munksgaard.me/papers/laumann-munksgaard-larsen.pdf). Philip
Munksgaard's master's thesis. Research for Servo.
* [Ownership is Theft: Experiences Building an Embedded OS in Rust - Amit Levy, et. al.](http://amitlevy.com/papers/tock-plos2015.pdf)
* [You can't spell trust without Rust](https://raw.githubusercontent.com/Gankro/thesis/master/thesis.pdf). Alexis Beingessner's master's thesis.
31 changes: 26 additions & 5 deletions src/doc/book/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,28 @@ fn file_name(file_path: &str) -> Option<&str> {
```

You might think that we could use the `map` combinator to reduce the case
analysis, but its type doesn't quite fit. Namely, `map` takes a function that
does something only with the inner value. The result of that function is then
*always* [rewrapped with `Some`](#code-option-map). Instead, we need something
like `map`, but which allows the caller to return another `Option`. Its generic
implementation is even simpler than `map`:
analysis, but its type doesn't quite fit...

```rust
fn file_path_ext(file_path: &str) -> Option<&str> {
file_name(file_path).map(|x| extension(x)) //Compilation error
}
```

The `map` function here wraps the value returned by the `extension` function
inside an `Option<_>` and since the `extension` function itself returns an
`Option<&str>` the expression `file_name(file_path).map(|x| extension(x))`
actually returns an `Option<Option<&str>>`.

But since `file_path_ext` just returns `Option<&str>` (and not
`Option<Option<&str>>`) we get a compilation error.

The result of the function taken by map as input is *always* [rewrapped with
`Some`](#code-option-map). Instead, we need something like `map`, but which
allows the caller to return a `Option<_>` directly without wrapping it in
another `Option<_>`.

Its generic implementation is even simpler than `map`:

```rust
fn and_then<F, T, A>(option: Option<T>, f: F) -> Option<A>
Expand All @@ -377,6 +394,10 @@ fn file_path_ext(file_path: &str) -> Option<&str> {
}
```

Side note: Since `and_then` essentially works like `map` but returns an
`Option<_>` instead of an `Option<Option<_>>` it is known as `flatmap` in some
other languages.

The `Option` type has many other combinators [defined in the standard
library][5]. It is a good idea to skim this list and familiarize
yourself with what's available—they can often reduce case analysis
Expand Down
2 changes: 1 addition & 1 deletion src/doc/book/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ If we're on Linux or a Mac, all we need to do is open a terminal and type this:
$ curl -sSf https://static.rust-lang.org/rustup.sh | sh
```

This will download a script, and stat the installation. If it all goes well,
This will download a script, and start the installation. If it all goes well,
you’ll see this appear:

```text
Expand Down
1 change: 1 addition & 0 deletions src/doc/book/guessing-game.md
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ let guess: u32 = match guess.trim().parse() {
```

This is how you generally move from ‘crash on error’ to ‘actually handle the
error’, by switching from `expect()` to a `match` statement. The `Result`
returned by `parse()` is an `enum` like `Ordering`, but in this case, each
variant has some data associated with it: `Ok` is a success, and `Err` is a
failure. Each contains more information: the successfully parsed integer, or an
Expand Down
15 changes: 8 additions & 7 deletions src/doc/book/no-stdlib.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ The compiler currently makes a few assumptions about symbols which are available
in the executable to call. Normally these functions are provided by the standard
library, but without it you must define your own.

The first of these two functions, `eh_personality`, is used by the
failure mechanisms of the compiler. This is often mapped to GCC's
personality function (see the
[libstd implementation](../std/rt/unwind/index.html) for more
information), but crates which do not trigger a panic can be assured
that this function is never called. The second function, `panic_fmt`, is
also used by the failure mechanisms of the compiler.
The first of these two functions, `eh_personality`, is used by the failure
mechanisms of the compiler. This is often mapped to GCC's personality function
(see the [libstd implementation][unwind] for more information), but crates
which do not trigger a panic can be assured that this function is never
called. The second function, `panic_fmt`, is also used by the failure
mechanisms of the compiler.

[unwind]: https://github.com/rust-lang/rust/blob/master/src/libstd/sys/common/unwind/gcc.rs
16 changes: 11 additions & 5 deletions src/doc/book/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,22 @@ This will compile without error.
This means that even if someone does something bad like add methods to `i32`,
it won’t affect you, unless you `use` that trait.

There’s one more restriction on implementing traits: either the trait, or the
type you’re writing the `impl` for, must be defined by you. So, we could
implement the `HasArea` type for `i32`, because `HasArea` is in our code. But
if we tried to implement `ToString`, a trait provided by Rust, for `i32`, we could
not, because neither the trait nor the type are in our code.
There’s one more restriction on implementing traits: either the trait
or the type you’re implementing it for must be defined by you. Or more
precisely, one of them must be defined in the same crate as the `impl`
you're writing. For more on Rust's module and package system, see the
chapter on [crates and modules][cm].

So, we could implement the `HasArea` type for `i32`, because we defined
`HasArea` in our code. But if we tried to implement `ToString`, a trait
provided by Rust, for `i32`, we could not, because neither the trait nor
the type are defined in our crate.

One last thing about traits: generic functions with a trait bound use
‘monomorphization’ (mono: one, morph: form), so they are statically dispatched.
What’s that mean? Check out the chapter on [trait objects][to] for more details.

[cm]: crates-and-modules.html
[to]: trait-objects.html

# Multiple trait bounds
Expand Down
2 changes: 1 addition & 1 deletion src/doc/version_info.html.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div id="versioninfo">
<img src="https://www.rust-lang.org/logos/rust-logo-32x32-blk.png" width="32" height="32" alt><br>
<img src="https://www.rust-lang.org/logos/rust-logo-32x32-blk.png" width="32" height="32" alt="Rust logo"><br>
<span class="white-sticker"><a href="https://www.rust-lang.org">Rust</a> VERSION</span><br>
<a href="https://github.com/rust-lang/rust/commit/STAMP"
class="hash white-sticker">SHORT_HASH</a>
Expand Down
20 changes: 10 additions & 10 deletions src/libcollections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1194,17 +1194,17 @@ unsafe fn unwrap_unchecked<T>(val: Option<T>) -> T {
}

impl<K, V> BTreeMap<K, V> {
/// Gets an iterator over the entries of the map.
/// Gets an iterator over the entries of the map, sorted by key.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
///
/// let mut map = BTreeMap::new();
/// map.insert(1, "a");
/// map.insert(2, "b");
/// map.insert(3, "c");
/// map.insert(2, "b");
/// map.insert(1, "a");
///
/// for (key, value) in map.iter() {
/// println!("{}: {}", key, value);
Expand All @@ -1224,7 +1224,7 @@ impl<K, V> BTreeMap<K, V> {
}
}

/// Gets a mutable iterator over the entries of the map.
/// Gets a mutable iterator over the entries of the map, sorted by key.
///
/// # Examples
///
Expand Down Expand Up @@ -1257,16 +1257,16 @@ impl<K, V> BTreeMap<K, V> {
}
}

/// Gets an iterator over the keys of the map.
/// Gets an iterator over the keys of the map, in sorted order.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
///
/// let mut a = BTreeMap::new();
/// a.insert(1, "a");
/// a.insert(2, "b");
/// a.insert(1, "a");
///
/// let keys: Vec<_> = a.keys().cloned().collect();
/// assert_eq!(keys, [1, 2]);
Expand All @@ -1276,19 +1276,19 @@ impl<K, V> BTreeMap<K, V> {
Keys { inner: self.iter() }
}

/// Gets an iterator over the values of the map.
/// Gets an iterator over the values of the map, in order by key.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
///
/// let mut a = BTreeMap::new();
/// a.insert(1, "a");
/// a.insert(2, "b");
/// a.insert(1, "hello");
/// a.insert(2, "goodbye");
///
/// let values: Vec<&str> = a.values().cloned().collect();
/// assert_eq!(values, ["a", "b"]);
/// assert_eq!(values, ["hello", "goodbye"]);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn values<'a>(&'a self) -> Values<'a, K, V> {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_borrowck/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn main() {
let mut x = Rc::new(RefCell::new(MyStruct{ s: 5u32 }));
let y = x.clone();
x.borrow_mut().s = 6;
println!("{}", x.borrow.s);
println!("{}", x.borrow().s);
}
```

Expand Down
36 changes: 35 additions & 1 deletion src/librustc_resolve/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,41 @@ https://doc.rust-lang.org/reference.html#statements
E0317: r##"
User-defined types or type parameters cannot shadow the primitive types.
This error indicates you tried to define a type, struct or enum with the same
name as an existing primitive type.
name as an existing primitive type:

```
struct u8 {
// ...
}
```

To fix this, simply name it something else.

Such an error may also occur if you define a type parameter which shadows a
primitive type. An example would be something like:

```
impl<u8> MyTrait for Option<u8> {
// ...
}
```

In such a case, if you meant for `u8` to be a generic type parameter (i.e. any
type can be used in its place), use something like `T` instead:

```
impl<T> MyTrait for Option<T> {
// ...
}
```

On the other hand, if you wished to refer to the specific type `u8`, remove it
from the type parameter list:

```
impl MyTrait for Option<u8> {
// ...
}

See the Types section of the reference for more information about the primitive
types:
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1918,8 +1918,8 @@ fn get_or_create_type_parameter_def<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
lint::builtin::INVALID_TYPE_PARAM_DEFAULT,
param.id,
param.span,
format!("defaults for type parameters are only allowed on type definitions, \
like `struct` or `enum`"));
format!("defaults for type parameters are only allowed in `struct`, \
`enum`, `type`, or `trait` definitions."));
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/librustc_typeck/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2269,6 +2269,8 @@ struct MyType<T>(T);
impl<T> ForeignTrait for MyType<T> { ... } // Ok
```

Please note that a type alias is not sufficient.

For another example of an error, suppose there's another trait defined in `foo`
named `ForeignTrait2` that takes two type parameters. Then this `impl` results
in the same rule violation:
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ r##"<!DOCTYPE html>
"".to_string()
} else {
format!("<a href='{}{}/index.html'>\
<img src='{}' alt='' width='100'></a>",
<img src='{}' alt='logo' width='100'></a>",
page.root_path, layout.krate,
layout.logo)
},
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ a {
}

.content span.enum, .content a.enum, .block a.current.enum { color: #5e9766; }
.content span.struct, .content a.struct, .block a.current.struct { color: #e53700; }
.content span.struct, .content a.struct, .block a.current.struct { color: #df3600; }
.content a.type { color: #e57300; }
.content a.macro { color: #068000; }
.block a.current.crate { font-weight: 500; }
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/static/styles/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ a {
}

.docblock a, .stability a {
color: #4e8bca;
color: #3873AD;
}

a.test-arrow {
color: #f5f5f5;
}

.content span.trait, .content a.trait, .block a.current.trait { color: #8866ff; }
.content span.trait, .content a.trait, .block a.current.trait { color: #7c5af3; }

.search-input {
color: #555;
Expand Down
29 changes: 29 additions & 0 deletions src/libstd/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,35 @@ fn test_resize_policy() {
/// }
/// ```
///
/// `HashMap` also implements an [`Entry API`](#method.entry), which allows
/// for more complex methods of getting, setting, updating and removing keys and
/// their values:
///
/// ```
/// use std::collections::HashMap;
///
/// // type inference lets us omit an explicit type signature (which
/// // would be `HashMap<&str, u8>` in this example).
/// let mut player_stats = HashMap::new();
///
/// fn random_stat_buff() -> u8 {
/// // could actually return some random value here - let's just return
/// // some fixed value for now
/// 42
/// }
///
/// // insert a key only if it doesn't already exist
/// player_stats.entry("health").or_insert(100);
///
/// // insert a key using a function that provides a new value only if it
/// // doesn't already exist
/// player_stats.entry("defence").or_insert_with(random_stat_buff);
///
/// // update a key, guarding against the key possibly not being set
/// let stat = player_stats.entry("attack").or_insert(100);
/// *stat += random_stat_buff();
/// ```
///
/// The easiest way to use `HashMap` with a custom type as key is to derive `Eq` and `Hash`.
/// We must also derive `PartialEq`.
///
Expand Down
4 changes: 1 addition & 3 deletions src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,16 +407,14 @@ impl CStr {
/// # fn main() {
/// use std::ffi::CStr;
/// use std::os::raw::c_char;
/// use std::str;
///
/// extern {
/// fn my_string() -> *const c_char;
/// }
///
/// unsafe {
/// let slice = CStr::from_ptr(my_string());
/// println!("string returned: {}",
/// str::from_utf8(slice.to_bytes()).unwrap());
/// println!("string returned: {}", slice.to_str().unwrap());
/// }
/// # }
/// ```
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ impl OpenOptions {
/// This option, when true, will indicate that the file should be
/// `write`-able if opened.
///
/// If a file already exist, any write calls on the file will overwrite its
/// If the file already exists, any write calls on it will overwrite its
/// contents, without truncating it.
///
/// # Examples
Expand Down Expand Up @@ -487,8 +487,8 @@ impl OpenOptions {
/// This option indicates whether a new file will be created if the file
/// does not yet already exist.
///
/// The file must be opened with write or append access in order to create
/// a new file.
/// In order for the file to be created, `write` or `append` access must
/// be used.
///
/// # Examples
///
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sync/semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
of resources is currently unclear",
issue = "27798")]
#![rustc_deprecated(since = "1.7.0",
reason = "easily confused with system sempahores and not \
reason = "easily confused with system semaphores and not \
used enough to pull its weight")]
#![allow(deprecated)]

Expand Down
Loading