Skip to content

Add urls #36363

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 4 commits into from
Sep 14, 2016
Merged

Add urls #36363

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
25 changes: 16 additions & 9 deletions src/libcore/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
//! assign them or pass them as arguments, the receiver will get a copy,
//! leaving the original value in place. These types do not require
//! allocation to copy and do not have finalizers (i.e. they do not
//! contain owned boxes or implement `Drop`), so the compiler considers
//! contain owned boxes or implement [`Drop`]), so the compiler considers
//! them cheap and safe to copy. For other types copies must be made
//! explicitly, by convention implementing the `Clone` trait and calling
//! the `clone` method.
//! explicitly, by convention implementing the [`Clone`] trait and calling
//! the [`clone`][clone] method.
//!
//! [`Clone`]: trait.Clone.html
//! [clone]: trait.Clone.html#tymethod.clone
//! [`Drop`]: ../../std/ops/trait.Drop.html
//!
//! Basic usage example:
//!
Expand Down Expand Up @@ -46,22 +50,22 @@

/// A common trait for the ability to explicitly duplicate an object.
///
/// Differs from `Copy` in that `Copy` is implicit and extremely inexpensive, while
/// Differs from [`Copy`] in that [`Copy`] is implicit and extremely inexpensive, while
/// `Clone` is always explicit and may or may not be expensive. In order to enforce
/// these characteristics, Rust does not allow you to reimplement `Copy`, but you
/// these characteristics, Rust does not allow you to reimplement [`Copy`], but you
/// may reimplement `Clone` and run arbitrary code.
///
/// Since `Clone` is more general than `Copy`, you can automatically make anything
/// `Copy` be `Clone` as well.
/// Since `Clone` is more general than [`Copy`], you can automatically make anything
/// [`Copy`] be `Clone` as well.
///
/// ## Derivable
///
/// This trait can be used with `#[derive]` if all fields are `Clone`. The `derive`d
/// implementation of `clone()` calls `clone()` on each field.
/// implementation of [`clone()`] calls [`clone()`] on each field.
///
/// ## How can I implement `Clone`?
///
/// Types that are `Copy` should have a trivial implementation of `Clone`. More formally:
/// Types that are [`Copy`] should have a trivial implementation of `Clone`. More formally:
/// if `T: Copy`, `x: T`, and `y: &T`, then `let x = y.clone();` is equivalent to `let x = *y;`.
/// Manual implementations should be careful to uphold this invariant; however, unsafe code
/// must not rely on it to ensure memory safety.
Expand All @@ -70,6 +74,9 @@
/// library only implements `Clone` up until arrays of size 32. In this case, the implementation of
/// `Clone` cannot be `derive`d, but can be implemented as:
///
/// [`Copy`]: ../../std/marker/trait.Copy.html
/// [`clone()`]: trait.Clone.html#tymethod.clone
///
/// ```
/// #[derive(Copy)]
/// struct Stats {
Expand Down
51 changes: 35 additions & 16 deletions src/libcore/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub trait Unsize<T: ?Sized> {
/// }
/// ```
///
/// The `PointList` `struct` cannot implement `Copy`, because `Vec<T>` is not `Copy`. If we
/// The `PointList` `struct` cannot implement `Copy`, because [`Vec<T>`] is not `Copy`. If we
/// attempt to derive a `Copy` implementation, we'll get an error:
///
/// ```text
Expand All @@ -136,10 +136,10 @@ pub trait Unsize<T: ?Sized> {
/// ## When can my type _not_ be `Copy`?
///
/// Some types can't be copied safely. For example, copying `&mut T` would create an aliased
/// mutable reference, and copying `String` would result in two attempts to free the same buffer.
/// mutable reference, and copying [`String`] would result in two attempts to free the same buffer.
///
/// Generalizing the latter case, any type implementing `Drop` can't be `Copy`, because it's
/// managing some resource besides its own `size_of::<T>()` bytes.
/// Generalizing the latter case, any type implementing [`Drop`] can't be `Copy`, because it's
/// managing some resource besides its own [`size_of::<T>()`] bytes.
///
/// ## What if I derive `Copy` on a type that can't?
///
Expand All @@ -156,8 +156,7 @@ pub trait Unsize<T: ?Sized> {
///
/// ## Derivable
///
/// This trait can be used with `#[derive]` if all of its components implement `Copy` and the type
/// implements `Clone`. The implementation will copy the bytes of each field using `memcpy`.
/// This trait can be used with `#[derive]` if all of its components implement `Copy` and the type.
///
/// ## How can I implement `Copy`?
///
Expand All @@ -178,6 +177,11 @@ pub trait Unsize<T: ?Sized> {
///
/// There is a small difference between the two: the `derive` strategy will also place a `Copy`
/// bound on type parameters, which isn't always desired.
///
/// [`Vec<T>`]: ../../std/vec/struct.Vec.html
/// [`String`]: ../../std/string/struct.String.html
/// [`Drop`]: ../../std/ops/trait.Drop.html
/// [`size_of::<T>()`]: ../../std/mem/fn.size_of.html
#[stable(feature = "rust1", since = "1.0.0")]
#[lang = "copy"]
pub trait Copy : Clone {
Expand All @@ -190,11 +194,11 @@ pub trait Copy : Clone {
/// thread-safe. In other words, there is no possibility of data races
/// when passing `&T` references between threads.
///
/// As one would expect, primitive types like `u8` and `f64` are all
/// As one would expect, primitive types like [`u8`] and [`f64`] are all
/// `Sync`, and so are simple aggregate types containing them (like
/// tuples, structs and enums). More instances of basic `Sync` types
/// include "immutable" types like `&T` and those with simple
/// inherited mutability, such as `Box<T>`, `Vec<T>` and most other
/// inherited mutability, such as [`Box<T>`], [`Vec<T>`] and most other
/// collection types. (Generic parameters need to be `Sync` for their
/// container to be `Sync`.)
///
Expand All @@ -206,27 +210,42 @@ pub trait Copy : Clone {
/// race.
///
/// Types that are not `Sync` are those that have "interior
/// mutability" in a non-thread-safe way, such as `Cell` and `RefCell`
/// in `std::cell`. These types allow for mutation of their contents
/// mutability" in a non-thread-safe way, such as [`Cell`] and [`RefCell`]
/// in [`std::cell`]. These types allow for mutation of their contents
/// even when in an immutable, aliasable slot, e.g. the contents of
/// `&Cell<T>` can be `.set`, and do not ensure data races are
/// [`&Cell<T>`][`Cell`] can be [`.set`], and do not ensure data races are
/// impossible, hence they cannot be `Sync`. A higher level example
/// of a non-`Sync` type is the reference counted pointer
/// `std::rc::Rc`, because any reference `&Rc<T>` can clone a new
/// [`std::rc::Rc`][`Rc`], because any reference [`&Rc<T>`][`Rc`] can clone a new
/// reference, which modifies the reference counts in a non-atomic
/// way.
///
/// For cases when one does need thread-safe interior mutability,
/// types like the atomics in `std::sync` and `Mutex` & `RWLock` in
/// the `sync` crate do ensure that any mutation cannot cause data
/// types like the atomics in [`std::sync`][`sync`] and [`Mutex`] / [`RwLock`] in
/// the [`sync`] crate do ensure that any mutation cannot cause data
/// races. Hence these types are `Sync`.
///
/// Any types with interior mutability must also use the `std::cell::UnsafeCell`
/// Any types with interior mutability must also use the [`std::cell::UnsafeCell`]
/// wrapper around the value(s) which can be mutated when behind a `&`
/// reference; not doing this is undefined behavior (for example,
/// `transmute`-ing from `&T` to `&mut T` is invalid).
/// [`transmute`]-ing from `&T` to `&mut T` is invalid).
///
/// This trait is automatically derived when the compiler determines it's appropriate.
///
/// [`u8`]: ../../std/primitive.u8.html
/// [`f64`]: ../../std/primitive.f64.html
/// [`Vec<T>`]: ../../std/vec/struct.Vec.html
/// [`Box<T>`]: ../../std/boxed/struct.Box.html
/// [`Cell`]: ../../std/cell/struct.Cell.html
/// [`RefCell`]: ../../std/cell/struct.RefCell.html
/// [`std::cell`]: ../../std/cell/index.html
/// [`.set`]: ../../std/cell/struct.Cell.html#method.set
/// [`Rc`]: ../../std/rc/struct.Rc.html
/// [`sync`]: ../../std/sync/index.html
/// [`Mutex`]: ../../std/sync/struct.Mutex.html
/// [`RwLock`]: ../../std/sync/struct.RwLock.html
/// [`std::cell::UnsafeCell`]: ../../std/cell/struct.UnsafeCell.html
/// [`transmute`]: ../../std/mem/fn.transmute.html
#[stable(feature = "rust1", since = "1.0.0")]
#[lang = "sync"]
#[rustc_on_unimplemented = "`{Self}` cannot be shared between threads safely"]
Expand Down
78 changes: 50 additions & 28 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

//! Optional values.
//!
//! Type `Option` represents an optional value: every `Option`
//! is either `Some` and contains a value, or `None`, and
//! does not. `Option` types are very common in Rust code, as
//! Type [`Option`] represents an optional value: every [`Option`]
//! is either [`Some`] and contains a value, or [`None`], and
//! does not. [`Option`] types are very common in Rust code, as
//! they have a number of uses:
//!
//! * Initial values
Expand All @@ -26,8 +26,8 @@
//! * Nullable pointers
//! * Swapping things out of difficult situations
//!
//! Options are commonly paired with pattern matching to query the presence
//! of a value and take action, always accounting for the `None` case.
//! [`Option`]s are commonly paired with pattern matching to query the presence
//! of a value and take action, always accounting for the [`None`] case.
//!
//! ```
//! fn divide(numerator: f64, denominator: f64) -> Option<f64> {
Expand Down Expand Up @@ -57,13 +57,13 @@
//!
//! Rust's pointer types must always point to a valid location; there are
//! no "null" pointers. Instead, Rust has *optional* pointers, like
//! the optional owned box, `Option<Box<T>>`.
//! the optional owned box, [`Option`]`<`[`Box<T>`]`>`.
//!
//! The following example uses `Option` to create an optional box of
//! `i32`. Notice that in order to use the inner `i32` value first the
//! The following example uses [`Option`] to create an optional box of
//! [`i32`]. Notice that in order to use the inner [`i32`] value first the
//! `check_optional` function needs to use pattern matching to
//! determine whether the box has a value (i.e. it is `Some(...)`) or
//! not (`None`).
//! determine whether the box has a value (i.e. it is [`Some(...)`][`Some`]) or
//! not ([`None`]).
//!
//! ```
//! let optional: Option<Box<i32>> = None;
Expand All @@ -80,14 +80,14 @@
//! }
//! ```
//!
//! This usage of `Option` to create safe nullable pointers is so
//! This usage of [`Option`] to create safe nullable pointers is so
//! common that Rust does special optimizations to make the
//! representation of `Option<Box<T>>` a single pointer. Optional pointers
//! representation of [`Option`]`<`[`Box<T>`]`>` a single pointer. Optional pointers
//! in Rust are stored as efficiently as any other pointer type.
//!
//! # Examples
//!
//! Basic pattern matching on `Option`:
//! Basic pattern matching on [`Option`]:
//!
//! ```
//! let msg = Some("howdy");
Expand All @@ -101,7 +101,7 @@
//! let unwrapped_msg = msg.unwrap_or("default message");
//! ```
//!
//! Initialize a result to `None` before a loop:
//! Initialize a result to [`None`] before a loop:
//!
//! ```
//! enum Kingdom { Plant(u32, &'static str), Animal(u32, &'static str) }
Expand Down Expand Up @@ -136,6 +136,12 @@
//! None => println!("there are no animals :("),
//! }
//! ```
//!
//! [`Option`]: enum.Option.html
//! [`Some`]: enum.Option.html#variant.Some
//! [`None`]: enum.Option.html#variant.None
//! [`Box<T>`]: ../../std/boxed/struct.Box.html
//! [`i32`]: ../../std/primitive.i32.html

#![stable(feature = "rust1", since = "1.0.0")]

Expand All @@ -156,7 +162,7 @@ pub enum Option<T> {
None,
/// Some value `T`
#[stable(feature = "rust1", since = "1.0.0")]
Some(#[stable(feature = "rust1", since = "1.0.0")] T)
Some(#[stable(feature = "rust1", since = "1.0.0")] T),
}

/////////////////////////////////////////////////////////////////////////////
Expand All @@ -168,7 +174,7 @@ impl<T> Option<T> {
// Querying the contained values
/////////////////////////////////////////////////////////////////////////

/// Returns `true` if the option is a `Some` value
/// Returns `true` if the option is a `Some` value.
///
/// # Examples
///
Expand All @@ -188,7 +194,7 @@ impl<T> Option<T> {
}
}

/// Returns `true` if the option is a `None` value
/// Returns `true` if the option is a `None` value.
///
/// # Examples
///
Expand All @@ -209,15 +215,17 @@ impl<T> Option<T> {
// Adapter for working with references
/////////////////////////////////////////////////////////////////////////

/// Converts from `Option<T>` to `Option<&T>`
/// Converts from `Option<T>` to `Option<&T>`.
///
/// # Examples
///
/// Convert an `Option<String>` into an `Option<usize>`, preserving the original.
/// The `map` method takes the `self` argument by value, consuming the original,
/// The [`map`] method takes the `self` argument by value, consuming the original,
/// so this technique uses `as_ref` to first take an `Option` to a reference
/// to the value inside the original.
///
/// [`map`]: enum.Option.html#method.map
///
/// ```
/// let num_as_str: Option<String> = Some("10".to_string());
/// // First, cast `Option<String>` to `Option<&String>` with `as_ref`,
Expand All @@ -234,7 +242,7 @@ impl<T> Option<T> {
}
}

/// Converts from `Option<T>` to `Option<&mut T>`
/// Converts from `Option<T>` to `Option<&mut T>`.
///
/// # Examples
///
Expand Down Expand Up @@ -357,7 +365,7 @@ impl<T> Option<T> {
// Transforming contained values
/////////////////////////////////////////////////////////////////////////

/// Maps an `Option<T>` to `Option<U>` by applying a function to a contained value
/// Maps an `Option<T>` to `Option<U>` by applying a function to a contained value.
///
/// # Examples
///
Expand Down Expand Up @@ -423,8 +431,12 @@ impl<T> Option<T> {
}
}

/// Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to
/// `Ok(v)` and `None` to `Err(err)`.
/// Transforms the `Option<T>` into a [`Result<T, E>`], mapping `Some(v)` to
/// [`Ok(v)`] and `None` to [`Err(err)`][Err].
///
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
/// [`Ok(v)`]: ../../std/result/enum.Result.html#variant.Ok
/// [Err]: ../../std/result/enum.Result.html#variant.Err
///
/// # Examples
///
Expand All @@ -444,8 +456,12 @@ impl<T> Option<T> {
}
}

/// Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to
/// `Ok(v)` and `None` to `Err(err())`.
/// Transforms the `Option<T>` into a [`Result<T, E>`], mapping `Some(v)` to
/// [`Ok(v)`] and `None` to [`Err(err())`][Err].
///
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
/// [`Ok(v)`]: ../../std/result/enum.Result.html#variant.Ok
/// [Err]: ../../std/result/enum.Result.html#variant.Err
///
/// # Examples
///
Expand Down Expand Up @@ -789,7 +805,9 @@ impl<A> DoubleEndedIterator for Item<A> {
impl<A> ExactSizeIterator for Item<A> {}
impl<A> FusedIterator for Item<A> {}

/// An iterator over a reference of the contained item in an Option.
/// An iterator over a reference of the contained item in an [`Option`].
///
/// [`Option`]: enum.Option.html
#[stable(feature = "rust1", since = "1.0.0")]
#[derive(Debug)]
pub struct Iter<'a, A: 'a> { inner: Item<&'a A> }
Expand Down Expand Up @@ -823,7 +841,9 @@ impl<'a, A> Clone for Iter<'a, A> {
}
}

/// An iterator over a mutable reference of the contained item in an Option.
/// An iterator over a mutable reference of the contained item in an [`Option`].
///
/// [`Option`]: enum.Option.html
#[stable(feature = "rust1", since = "1.0.0")]
#[derive(Debug)]
pub struct IterMut<'a, A: 'a> { inner: Item<&'a mut A> }
Expand All @@ -850,7 +870,9 @@ impl<'a, A> ExactSizeIterator for IterMut<'a, A> {}
#[unstable(feature = "fused", issue = "35602")]
impl<'a, A> FusedIterator for IterMut<'a, A> {}

/// An iterator over the item contained inside an Option.
/// An iterator over the item contained inside an [`Option`].
///
/// [`Option`]: enum.Option.html
#[derive(Clone, Debug)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct IntoIter<A> { inner: Item<A> }
Expand Down
Loading