17
17
//! Like many traits, these are often used as bounds for generic functions, to
18
18
//! support arguments of multiple types.
19
19
//!
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
+ //!
20
38
//! See each trait for usage examples.
21
39
22
40
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -30,6 +48,9 @@ use marker::Sized;
30
48
///
31
49
/// [book]: ../../book/borrow-and-asref.html
32
50
///
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
+ ///
33
54
/// # Examples
34
55
///
35
56
/// Both `String` and `&str` implement `AsRef<str>`:
@@ -45,6 +66,12 @@ use marker::Sized;
45
66
/// let s = "hello".to_string();
46
67
/// is_hello(s);
47
68
/// ```
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
+ ///
48
75
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
49
76
pub trait AsRef < T : ?Sized > {
50
77
/// Performs the conversion.
@@ -53,6 +80,15 @@ pub trait AsRef<T: ?Sized> {
53
80
}
54
81
55
82
/// 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
+ ///
56
92
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
57
93
pub trait AsMut < T : ?Sized > {
58
94
/// Performs the conversion.
@@ -62,6 +98,13 @@ pub trait AsMut<T: ?Sized> {
62
98
63
99
/// A conversion that consumes `self`, which may or may not be expensive.
64
100
///
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
+ ///
65
108
/// # Examples
66
109
///
67
110
/// `String` implements `Into<Vec<u8>>`:
@@ -75,6 +118,12 @@ pub trait AsMut<T: ?Sized> {
75
118
/// let s = "hello".to_string();
76
119
/// is_hello(s);
77
120
/// ```
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
+ ///
78
127
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
79
128
pub trait Into < T > : Sized {
80
129
/// Performs the conversion.
@@ -84,6 +133,9 @@ pub trait Into<T>: Sized {
84
133
85
134
/// Construct `Self` via a conversion.
86
135
///
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
+ ///
87
139
/// # Examples
88
140
///
89
141
/// `String` implements `From<&str>`:
@@ -94,6 +146,11 @@ pub trait Into<T>: Sized {
94
146
///
95
147
/// assert_eq!(string, other_string);
96
148
/// ```
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
+ ///
97
154
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
98
155
pub trait From < T > : Sized {
99
156
/// Performs the conversion.
0 commit comments