Skip to content

Rollup of 6 pull requests #30640

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

Merged
merged 17 commits into from
Dec 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
url = https://github.com/rust-lang/jemalloc.git
[submodule "src/rust-installer"]
path = src/rust-installer
url = https://github.com/rust-lang/rust-installer
url = https://github.com/rust-lang/rust-installer.git
[submodule "src/liblibc"]
path = src/liblibc
url = https://github.com/rust-lang/libc
url = https://github.com/rust-lang-nursery/libc.git
4 changes: 2 additions & 2 deletions src/doc/book/choosing-your-guarantees.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,11 @@ With the former, the `RefCell<T>` is wrapping the `Vec<T>`, so the `Vec<T>` in i
mutable. At the same time, there can only be one mutable borrow of the whole `Vec` at a given time.
This means that your code cannot simultaneously work on different elements of the vector from
different `Rc` handles. However, we are able to push and pop from the `Vec<T>` at will. This is
similar to an `&mut Vec<T>` with the borrow checking done at runtime.
similar to a `&mut Vec<T>` with the borrow checking done at runtime.

With the latter, the borrowing is of individual elements, but the overall vector is immutable. Thus,
we can independently borrow separate elements, but we cannot push or pop from the vector. This is
similar to an `&mut [T]`[^3], but, again, the borrow checking is at runtime.
similar to a `&mut [T]`[^3], but, again, the borrow checking is at runtime.

In concurrent programs, we have a similar situation with `Arc<Mutex<T>>`, which provides shared
mutability and ownership.
Expand Down
6 changes: 3 additions & 3 deletions src/doc/book/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ hello.rs:4 }
```

This [unfortunate error](https://github.com/rust-lang/rust/issues/22547) is
correct: documentation comments apply to the thing after them, and there's
correct; documentation comments apply to the thing after them, and there's
nothing after that last comment.

[rc-new]: https://doc.rust-lang.org/nightly/std/rc/struct.Rc.html#method.new
Expand Down Expand Up @@ -385,7 +385,7 @@ error handling. Lets say you want the following,

```rust,ignore
/// use std::io;
/// let mut input = String::new();
/// let mut input = String::new();
/// try!(io::stdin().read_line(&mut input));
```

Expand All @@ -398,7 +398,7 @@ don't return anything so this will give a mismatched types error.
/// ```
/// use std::io;
/// # fn foo() -> io::Result<()> {
/// let mut input = String::new();
/// let mut input = String::new();
/// try!(io::stdin().read_line(&mut input));
/// # Ok(())
/// # }
Expand Down
2 changes: 1 addition & 1 deletion src/doc/book/lifetimes.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Then in our parameter list, we use the lifetimes we’ve named:
...(x: &'a i32)
```

If we wanted an `&mut` reference, we’d do this:
If we wanted a `&mut` reference, we’d do this:

```rust,ignore
...(x: &'a mut i32)
Expand Down
26 changes: 13 additions & 13 deletions src/doc/book/match.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,24 @@ match x {
`match` takes an expression and then branches based on its value. Each ‘arm’ of
the branch is of the form `val => expression`. When the value matches, that arm’s
expression will be evaluated. It’s called `match` because of the term ‘pattern
matching’, which `match` is an implementation of. There’s an [entire section on
matching’, which `match` is an implementation of. There’s a [separate section on
patterns][patterns] that covers all the patterns that are possible here.

[patterns]: patterns.html

So what’s the big advantage? Well, there are a few. First of all, `match`
enforces ‘exhaustiveness checking’. Do you see that last arm, the one with the
underscore (`_`)? If we remove that arm, Rust will give us an error:
One of the many advantages of `match` is it enforces ‘exhaustiveness checking’.
For example if we remove the last arm with the underscore `_`, the compiler will
give us an error:

```text
error: non-exhaustive patterns: `_` not covered
```

In other words, Rust is trying to tell us we forgot a value. Because `x` is an
integer, Rust knows that it can have a number of different values – for
example, `6`. Without the `_`, however, there is no arm that could match, and
so Rust refuses to compile the code. `_` acts like a ‘catch-all arm’. If none
of the other arms match, the arm with `_` will, and since we have this
catch-all arm, we now have an arm for every possible value of `x`, and so our
program will compile successfully.
Rust is telling us that we forgot a value. The compiler infers from `x` that it
can have any positive 32bit value; for example 1 to 2,147,483,647. The `_` acts
as a 'catch-all', and will catch all possible values that *aren't* specified in
an arm of `match`. As you can see with the previous example, we provide `match`
arms for integers 1-5, if `x` is 6 or any other value, then it is caught by `_`.

`match` is also an expression, which means we can use it on the right-hand
side of a `let` binding or directly where an expression is used:
Expand All @@ -60,7 +58,8 @@ let number = match x {
};
```

Sometimes it’s a nice way of converting something from one type to another.
Sometimes it’s a nice way of converting something from one type to another; in
this example the integers are converted to `String`.

# Matching on enums

Expand Down Expand Up @@ -91,7 +90,8 @@ fn process_message(msg: Message) {

Again, the Rust compiler checks exhaustiveness, so it demands that you
have a match arm for every variant of the enum. If you leave one off, it
will give you a compile-time error unless you use `_`.
will give you a compile-time error unless you use `_` or provide all possible
arms.

Unlike the previous uses of `match`, you can’t use the normal `if`
statement to do this. You can use the [`if let`][if-let] statement,
Expand Down
2 changes: 1 addition & 1 deletion src/doc/book/references-and-borrowing.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ the thing `y` points at. You’ll notice that `x` had to be marked `mut` as well
If it wasn’t, we couldn’t take a mutable borrow to an immutable value.

You'll also notice we added an asterisk (`*`) in front of `y`, making it `*y`,
this is because `y` is an `&mut` reference. You'll also need to use them for
this is because `y` is a `&mut` reference. You'll also need to use them for
accessing the contents of a reference as well.

Otherwise, `&mut` references are just like references. There _is_ a large
Expand Down
20 changes: 13 additions & 7 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,16 +1113,22 @@ pub trait Iterator {
Take{iter: self, n: n}
}

/// An iterator similar to `fold()`, with internal state.
///
/// `scan()` accumulates a final value, similar to [`fold()`], but instead
/// of passing along an accumulator, it maintains the accumulator internally.
/// An iterator adaptor similar to [`fold()`] that holds internal state and
/// produces a new iterator.
///
/// [`fold()`]: #method.fold
///
/// On each iteraton of `scan()`, you can assign to the internal state, and
/// a mutable reference to the state is passed as the first argument to the
/// closure, allowing you to modify it on each iteration.
/// `scan()` takes two arguments: an initial value which seeds the internal
/// state, and a closure with two arguments, the first being a mutable
/// reference to the internal state and the second an iterator element.
/// The closure can assign to the internal state to share state between
/// iterations.
///
/// On iteration, the closure will be applied to each element of the
/// iterator and the return value from the closure, an [`Option`], is
/// yielded by the iterator.
///
/// [`Option`]: ../option/enum.Option.html
///
/// # Examples
///
Expand Down
51 changes: 36 additions & 15 deletions src/libstd/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,30 @@ fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
/// A handle to the standard input stream of a process.
///
/// Each handle is a shared reference to a global buffer of input data to this
/// process. A handle can be `lock`'d to gain full access to `BufRead` methods
/// process. A handle can be `lock`'d to gain full access to [`BufRead`] methods
/// (e.g. `.lines()`). Writes to this handle are otherwise locked with respect
/// to other writes.
///
/// This handle implements the `Read` trait, but beware that concurrent reads
/// of `Stdin` must be executed with care.
///
/// Created by the function `io::stdin()`.
/// Created by the [`io::stdin`] method.
///
/// [`io::stdin`]: fn.stdin.html
/// [`BufRead`]: trait.BufRead.html
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Stdin {
inner: Arc<Mutex<BufReader<Maybe<StdinRaw>>>>,
}

/// A locked reference to the `Stdin` handle.
///
/// This handle implements both the `Read` and `BufRead` traits and is
/// constructed via the `lock` method on `Stdin`.
/// This handle implements both the [`Read`] and [`BufRead`] traits, and
/// is constructed via the [`Stdin::lock`] method.
///
/// [`Read`]: trait.Read.html
/// [`BufRead`]: trait.BufRead.html
/// [`Stdin::lock`]: struct.Stdin.html#method.lock
#[stable(feature = "rust1", since = "1.0.0")]
pub struct StdinLock<'a> {
inner: MutexGuard<'a, BufReader<Maybe<StdinRaw>>>,
Expand All @@ -159,7 +166,7 @@ pub struct StdinLock<'a> {
///
/// Each handle returned is a reference to a shared global buffer whose access
/// is synchronized via a mutex. If you need more explicit control over
/// locking, see the [lock() method][lock].
/// locking, see the [`lock() method`][lock].
///
/// [lock]: struct.Stdin.html#method.lock
///
Expand Down Expand Up @@ -221,8 +228,11 @@ impl Stdin {
/// guard.
///
/// The lock is released when the returned lock goes out of scope. The
/// returned guard also implements the `Read` and `BufRead` traits for
/// returned guard also implements the [`Read`] and [`BufRead`] traits for
/// accessing the underlying data.
///
/// [Read]: trait.Read.html
/// [BufRead]: trait.BufRead.html
#[stable(feature = "rust1", since = "1.0.0")]
pub fn lock(&self) -> StdinLock {
StdinLock { inner: self.inner.lock().unwrap_or_else(|e| e.into_inner()) }
Expand All @@ -231,7 +241,9 @@ impl Stdin {
/// Locks this handle and reads a line of input into the specified buffer.
///
/// For detailed semantics of this method, see the documentation on
/// `BufRead::read_line`.
/// [`BufRead::read_line`].
///
/// [`BufRead::read_line`]: trait.BufRead.html#method.read_line
///
/// # Examples
///
Expand Down Expand Up @@ -314,7 +326,9 @@ const OUT_MAX: usize = ::usize::MAX;
/// output stream. Access is also synchronized via a lock and explicit control
/// over locking is available via the `lock` method.
///
/// Created by the function `io::stdout()`.
/// Created by the [`io::stdout`] method.
///
/// [`io::stdout`]: fn.stdout.html
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Stdout {
// FIXME: this should be LineWriter or BufWriter depending on the state of
Expand All @@ -325,8 +339,11 @@ pub struct Stdout {

/// A locked reference to the `Stdout` handle.
///
/// This handle implements the `Write` trait and is constructed via the `lock`
/// method on `Stdout`.
/// This handle implements the [`Write`] trait, and is constructed via
/// the [`Stdout::lock`] method.
///
/// [`Write`]: trait.Write.html
/// [`Stdout::lock`]: struct.Stdout.html#method.lock
#[stable(feature = "rust1", since = "1.0.0")]
pub struct StdoutLock<'a> {
inner: ReentrantMutexGuard<'a, RefCell<LineWriter<Maybe<StdoutRaw>>>>,
Expand All @@ -336,9 +353,9 @@ pub struct StdoutLock<'a> {
///
/// Each handle returned is a reference to a shared global buffer whose access
/// is synchronized via a mutex. If you need more explicit control over
/// locking, see the [lock() method][lock].
/// locking, see the [Stdout::lock] method.
///
/// [lock]: struct.Stdout.html#method.lock
/// [Stdout::lock]: struct.Stdout.html#method.lock
///
/// # Examples
///
Expand Down Expand Up @@ -424,16 +441,20 @@ impl<'a> Write for StdoutLock<'a> {

/// A handle to the standard error stream of a process.
///
/// For more information, see `stderr`
/// For more information, see the [`io::stderr`] method.
///
/// [`io::stderr`]: fn.stderr.html
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Stderr {
inner: Arc<ReentrantMutex<RefCell<Maybe<StderrRaw>>>>,
}

/// A locked reference to the `Stderr` handle.
///
/// This handle implements the `Write` trait and is constructed via the `lock`
/// method on `Stderr`.
/// This handle implements the `Write` trait and is constructed via
/// the [`Stderr::lock`] method.
///
/// [`Stderr::lock`]: struct.Stderr.html#method.lock
#[stable(feature = "rust1", since = "1.0.0")]
pub struct StderrLock<'a> {
inner: ReentrantMutexGuard<'a, RefCell<Maybe<StderrRaw>>>,
Expand Down
21 changes: 11 additions & 10 deletions src/libstd/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@ use time::Duration;
/// use std::net::UdpSocket;
///
/// # fn foo() -> std::io::Result<()> {
/// let mut socket = try!(UdpSocket::bind("127.0.0.1:34254"));
/// {
/// let mut socket = try!(UdpSocket::bind("127.0.0.1:34254"));
///
/// let mut buf = [0; 10];
/// let (amt, src) = try!(socket.recv_from(&mut buf));
/// // read from the socket
/// let mut buf = [0; 10];
/// let (amt, src) = try!(socket.recv_from(&mut buf));
///
/// // Send a reply to the socket we received data from
/// let buf = &mut buf[..amt];
/// buf.reverse();
/// try!(socket.send_to(buf, &src));
///
/// drop(socket); // close the socket
/// # Ok(())
/// // send a reply to the socket we received data from
/// let buf = &mut buf[..amt];
/// buf.reverse();
/// try!(socket.send_to(buf, &src));
/// # Ok(())
/// } // the socket is closed here
/// # }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down