Skip to content

Commit

Permalink
Doc:std::convert explicitely list generic impls
Browse files Browse the repository at this point in the history
Also add a note about the necessary simplicity of the conversion.
Related issue: #29349
  • Loading branch information
Thomas Wickham committed Jan 14, 2016
1 parent e1f550e commit 58d2c79
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/libcore/convert.rs
Expand Up @@ -17,6 +17,26 @@
//! Like many traits, these are often used as bounds for generic functions, to
//! support arguments of multiple types.
//!
//! - Use `as` for reference-to-reference conversions
//! - Use `into` when you want to consume the value
//! - `from` is the more flexible way, which can convert values and references
//!
//! As a library writer, you should prefer implementing `From<T>` rather than
//! `Into<U>`, as `From` is more flexible (you can't `Into` a reference, where
//! you can impl `From` for a reference). `From` is also used for generic
//! implementations.
//!
//! **Note:** these traits are for trivial conversion. **They must not fail**. If
//! they can fail, use a dedicated method which return an `Option<T>` or
//! a `Result<T, E>`.
//!
//! # Generic impl
//!
//! - `AsRef` and `AsMut` auto-dereference if the inner type is a reference
//! - `From<U> for T` implies `Into<T> for U`
//! - `From` and `Into` are reflexive, which means that all types can `into()`
//! themselve and `from()` themselve
//!
//! See each trait for usage examples.

#![stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -30,6 +50,10 @@ use marker::Sized;
///
/// [book]: ../../book/borrow-and-asref.html
///
/// **Note:** these traits are for trivial conversion. **They must not fail**. If
/// they can fail, use a dedicated method which return an `Option<T>` or
/// a `Result<T, E>`.
///
/// # Examples
///
/// Both `String` and `&str` implement `AsRef<str>`:
Expand All @@ -45,6 +69,12 @@ use marker::Sized;
/// let s = "hello".to_string();
/// is_hello(s);
/// ```
///
/// # Generic Impls
///
/// - `AsRef` auto-dereference if the inner type is a reference or a mutable
/// reference
///
#[stable(feature = "rust1", since = "1.0.0")]
pub trait AsRef<T: ?Sized> {
/// Performs the conversion.
Expand All @@ -53,6 +83,16 @@ pub trait AsRef<T: ?Sized> {
}

/// A cheap, mutable reference-to-mutable reference conversion.
///
/// **Note:** these traits are for trivial conversion. **They must not fail**. If
/// they can fail, use a dedicated method which return an `Option<T>` or
/// a `Result<T, E>`.
///
/// # Generic Impls
///
/// - `AsMut` auto-dereference if the inner type is a reference or a mutable
/// reference
///
#[stable(feature = "rust1", since = "1.0.0")]
pub trait AsMut<T: ?Sized> {
/// Performs the conversion.
Expand All @@ -62,6 +102,10 @@ pub trait AsMut<T: ?Sized> {

/// A conversion that consumes `self`, which may or may not be expensive.
///
/// **Note:** these traits are for trivial conversion. **They must not fail**. If
/// they can fail, use a dedicated method which return an `Option<T>` or
/// a `Result<T, E>`.
///
/// # Examples
///
/// `String` implements `Into<Vec<u8>>`:
Expand All @@ -75,6 +119,12 @@ pub trait AsMut<T: ?Sized> {
/// let s = "hello".to_string();
/// is_hello(s);
/// ```
///
/// #Generic Impls
///
/// - `From<T> for U` implies `Into<U> for T`
/// - `into()` is reflexive, which means that `Into<T> for T` is implemented
///
#[stable(feature = "rust1", since = "1.0.0")]
pub trait Into<T>: Sized {
/// Performs the conversion.
Expand All @@ -84,6 +134,10 @@ pub trait Into<T>: Sized {

/// Construct `Self` via a conversion.
///
/// **Note:** these traits are for trivial conversion. **They must not fail**. If
/// they can fail, use a dedicated method which return an `Option<T>` or
/// a `Result<T, E>`.
///
/// # Examples
///
/// `String` implements `From<&str>`:
Expand All @@ -94,6 +148,11 @@ pub trait Into<T>: Sized {
///
/// assert_eq!(string, other_string);
/// ```
/// # Generic impls
///
/// - `From<T> for U` implies `Into<U> for T`
/// - `from()` is reflexive, which means that `From<T> for T` is implemented
///
#[stable(feature = "rust1", since = "1.0.0")]
pub trait From<T>: Sized {
/// Performs the conversion.
Expand Down

0 comments on commit 58d2c79

Please sign in to comment.