Skip to content

Commit 58d2c79

Browse files
author
Thomas Wickham
committed
Doc:std::convert explicitely list generic impls
Also add a note about the necessary simplicity of the conversion. Related issue: rust-lang#29349
1 parent e1f550e commit 58d2c79

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

src/libcore/convert.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@
1717
//! Like many traits, these are often used as bounds for generic functions, to
1818
//! support arguments of multiple types.
1919
//!
20+
//! - Use `as` for reference-to-reference conversions
21+
//! - Use `into` when you want to consume the value
22+
//! - `from` is the more flexible way, which can convert values and references
23+
//!
24+
//! As a library writer, you should prefer implementing `From<T>` rather than
25+
//! `Into<U>`, as `From` is more flexible (you can't `Into` a reference, where
26+
//! you can impl `From` for a reference). `From` is also used for generic
27+
//! implementations.
28+
//!
29+
//! **Note:** these traits are for trivial conversion. **They must not fail**. If
30+
//! they can fail, use a dedicated method which return an `Option<T>` or
31+
//! a `Result<T, E>`.
32+
//!
33+
//! # Generic impl
34+
//!
35+
//! - `AsRef` and `AsMut` auto-dereference if the inner type is a reference
36+
//! - `From<U> for T` implies `Into<T> for U`
37+
//! - `From` and `Into` are reflexive, which means that all types can `into()`
38+
//! themselve and `from()` themselve
39+
//!
2040
//! See each trait for usage examples.
2141
2242
#![stable(feature = "rust1", since = "1.0.0")]
@@ -30,6 +50,10 @@ use marker::Sized;
3050
///
3151
/// [book]: ../../book/borrow-and-asref.html
3252
///
53+
/// **Note:** these traits are for trivial conversion. **They must not fail**. If
54+
/// they can fail, use a dedicated method which return an `Option<T>` or
55+
/// a `Result<T, E>`.
56+
///
3357
/// # Examples
3458
///
3559
/// Both `String` and `&str` implement `AsRef<str>`:
@@ -45,6 +69,12 @@ use marker::Sized;
4569
/// let s = "hello".to_string();
4670
/// is_hello(s);
4771
/// ```
72+
///
73+
/// # Generic Impls
74+
///
75+
/// - `AsRef` auto-dereference if the inner type is a reference or a mutable
76+
/// reference
77+
///
4878
#[stable(feature = "rust1", since = "1.0.0")]
4979
pub trait AsRef<T: ?Sized> {
5080
/// Performs the conversion.
@@ -53,6 +83,16 @@ pub trait AsRef<T: ?Sized> {
5383
}
5484

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

63103
/// A conversion that consumes `self`, which may or may not be expensive.
64104
///
105+
/// **Note:** these traits are for trivial conversion. **They must not fail**. If
106+
/// they can fail, use a dedicated method which return an `Option<T>` or
107+
/// a `Result<T, E>`.
108+
///
65109
/// # Examples
66110
///
67111
/// `String` implements `Into<Vec<u8>>`:
@@ -75,6 +119,12 @@ pub trait AsMut<T: ?Sized> {
75119
/// let s = "hello".to_string();
76120
/// is_hello(s);
77121
/// ```
122+
///
123+
/// #Generic Impls
124+
///
125+
/// - `From<T> for U` implies `Into<U> for T`
126+
/// - `into()` is reflexive, which means that `Into<T> for T` is implemented
127+
///
78128
#[stable(feature = "rust1", since = "1.0.0")]
79129
pub trait Into<T>: Sized {
80130
/// Performs the conversion.
@@ -84,6 +134,10 @@ pub trait Into<T>: Sized {
84134

85135
/// Construct `Self` via a conversion.
86136
///
137+
/// **Note:** these traits are for trivial conversion. **They must not fail**. If
138+
/// they can fail, use a dedicated method which return an `Option<T>` or
139+
/// a `Result<T, E>`.
140+
///
87141
/// # Examples
88142
///
89143
/// `String` implements `From<&str>`:
@@ -94,6 +148,11 @@ pub trait Into<T>: Sized {
94148
///
95149
/// assert_eq!(string, other_string);
96150
/// ```
151+
/// # Generic impls
152+
///
153+
/// - `From<T> for U` implies `Into<U> for T`
154+
/// - `from()` is reflexive, which means that `From<T> for T` is implemented
155+
///
97156
#[stable(feature = "rust1", since = "1.0.0")]
98157
pub trait From<T>: Sized {
99158
/// Performs the conversion.

0 commit comments

Comments
 (0)