Skip to content

Commit 2849ca6

Browse files
committed
Auto merge of #30901 - mackwic:doc-core-convert, r=steveklabnik
Also add a note about the necessary simplicity of the conversion. Related issue: #29349 r? @steveklabnik
2 parents 28bed3f + a0cd465 commit 2849ca6

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/libcore/convert.rs

+57
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@
1717
//! Like many traits, these are often used as bounds for generic functions, to
1818
//! support arguments of multiple types.
1919
//!
20+
//! - Impl the `As*` traits for reference-to-reference conversions
21+
//! - Impl the `Into` trait when you want to consume the value in the conversion
22+
//! - The `From` trait is the most flexible, usefull for values _and_ references conversions
23+
//!
24+
//! As a library writer, you should prefer implementing `From<T>` rather than
25+
//! `Into<U>`, as `From` provides greater flexibility and offer the equivalent `Into`
26+
//! implementation for free, thanks to a blanket implementation in the standard library.
27+
//!
28+
//! **Note: these traits must not fail**. If the conversion can fail, you must use a dedicated
29+
//! method which return an `Option<T>` or a `Result<T, E>`.
30+
//!
31+
//! # Generic impl
32+
//!
33+
//! - `AsRef` and `AsMut` auto-dereference if the inner type is a reference
34+
//! - `From<U> for T` implies `Into<T> for U`
35+
//! - `From` and `Into` are reflexive, which means that all types can `into()`
36+
//! themselves and `from()` themselves
37+
//!
2038
//! See each trait for usage examples.
2139
2240
#![stable(feature = "rust1", since = "1.0.0")]
@@ -30,6 +48,9 @@ use marker::Sized;
3048
///
3149
/// [book]: ../../book/borrow-and-asref.html
3250
///
51+
/// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which
52+
/// return an `Option<T>` or a `Result<T, E>`.
53+
///
3354
/// # Examples
3455
///
3556
/// Both `String` and `&str` implement `AsRef<str>`:
@@ -45,6 +66,12 @@ use marker::Sized;
4566
/// let s = "hello".to_string();
4667
/// is_hello(s);
4768
/// ```
69+
///
70+
/// # Generic Impls
71+
///
72+
/// - `AsRef` auto-dereference if the inner type is a reference or a mutable
73+
/// reference (eg: `foo.as_ref()` will work the same if `foo` has type `&mut Foo` or `&&mut Foo`)
74+
///
4875
#[stable(feature = "rust1", since = "1.0.0")]
4976
pub trait AsRef<T: ?Sized> {
5077
/// Performs the conversion.
@@ -53,6 +80,15 @@ pub trait AsRef<T: ?Sized> {
5380
}
5481

5582
/// A cheap, mutable reference-to-mutable reference conversion.
83+
///
84+
/// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which
85+
/// return an `Option<T>` or a `Result<T, E>`.
86+
///
87+
/// # Generic Impls
88+
///
89+
/// - `AsMut` auto-dereference if the inner type is a reference or a mutable
90+
/// reference (eg: `foo.as_ref()` will work the same if `foo` has type `&mut Foo` or `&&mut Foo`)
91+
///
5692
#[stable(feature = "rust1", since = "1.0.0")]
5793
pub trait AsMut<T: ?Sized> {
5894
/// Performs the conversion.
@@ -62,6 +98,13 @@ pub trait AsMut<T: ?Sized> {
6298

6399
/// A conversion that consumes `self`, which may or may not be expensive.
64100
///
101+
/// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which
102+
/// return an `Option<T>` or a `Result<T, E>`.
103+
///
104+
/// Library writer should not implement directly this trait, but should prefer the implementation
105+
/// of the `From` trait, which offer greater flexibility and provide the equivalent `Into`
106+
/// implementation for free, thanks to a blanket implementation in the standard library.
107+
///
65108
/// # Examples
66109
///
67110
/// `String` implements `Into<Vec<u8>>`:
@@ -75,6 +118,12 @@ pub trait AsMut<T: ?Sized> {
75118
/// let s = "hello".to_string();
76119
/// is_hello(s);
77120
/// ```
121+
///
122+
/// # Generic Impls
123+
///
124+
/// - `From<T> for U` implies `Into<U> for T`
125+
/// - `into()` is reflexive, which means that `Into<T> for T` is implemented
126+
///
78127
#[stable(feature = "rust1", since = "1.0.0")]
79128
pub trait Into<T>: Sized {
80129
/// Performs the conversion.
@@ -84,6 +133,9 @@ pub trait Into<T>: Sized {
84133

85134
/// Construct `Self` via a conversion.
86135
///
136+
/// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which
137+
/// return an `Option<T>` or a `Result<T, E>`.
138+
///
87139
/// # Examples
88140
///
89141
/// `String` implements `From<&str>`:
@@ -94,6 +146,11 @@ pub trait Into<T>: Sized {
94146
///
95147
/// assert_eq!(string, other_string);
96148
/// ```
149+
/// # Generic impls
150+
///
151+
/// - `From<T> for U` implies `Into<U> for T`
152+
/// - `from()` is reflexive, which means that `From<T> for T` is implemented
153+
///
97154
#[stable(feature = "rust1", since = "1.0.0")]
98155
pub trait From<T>: Sized {
99156
/// Performs the conversion.

0 commit comments

Comments
 (0)