|
26 | 26 | //! As a library author, you should prefer implementing [`From<T>`][`From`] or
|
27 | 27 | //! [`TryFrom<T>`][`TryFrom`] rather than [`Into<U>`][`Into`] or [`TryInto<U>`][`TryInto`],
|
28 | 28 | //! as [`From`] and [`TryFrom`] provide greater flexibility and offer
|
29 |
| -//! equivalent [`Into`] or [`TryInto`] implementations for free, thanks to a blanket implementation |
30 |
| -//! in the standard library. |
| 29 | +//! equivalent [`Into`] or [`TryInto`] implementations for free, thanks to a |
| 30 | +//! blanket implementation in the standard library. |
31 | 31 | //!
|
32 | 32 | //! # Generic Implementations
|
33 | 33 | //!
|
34 | 34 | //! - [`AsRef`] and [`AsMut`] auto-dereference if the inner type is a reference
|
35 | 35 | //! - [`From`]`<U> for T` implies [`Into`]`<T> for U`
|
36 | 36 | //! - [`TryFrom`]`<U> for T` implies [`TryInto`]`<T> for U`
|
37 |
| -//! - [`From`] and [`Into`] are reflexive, which means that all types can `into()` |
38 |
| -//! themselves and `from()` themselves |
| 37 | +//! - [`From`] and [`Into`] are reflexive, which means that all types can |
| 38 | +//! `into()` themselves and `from()` themselves |
39 | 39 | //!
|
40 | 40 | //! See each trait for usage examples.
|
41 | 41 | //!
|
|
50 | 50 |
|
51 | 51 | use str::FromStr;
|
52 | 52 |
|
53 |
| -/// A cheap reference-to-reference conversion. Used to convert a value to a reference value |
54 |
| -/// within generic code. |
55 |
| -/// |
56 |
| -/// `AsRef` is very similar to, but serves a slightly different purpose than, [`Borrow`]. |
57 |
| -/// |
58 |
| -/// `AsRef` is to be used when wishing to convert to a reference of another type. |
59 |
| -/// `Borrow` is more related to the notion of taking the reference. It is useful when wishing to abstract |
60 |
| -/// over the type of reference (`&T`, `&mut T`) or allow both the referenced and owned type to be treated in the same manner. |
| 53 | +/// A cheap reference-to-reference conversion. Used to convert a value to a |
| 54 | +/// reference value within generic code. |
| 55 | +/// |
| 56 | +/// `AsRef` is very similar to, but serves a slightly different purpose than, |
| 57 | +/// [`Borrow`]. |
| 58 | +/// |
| 59 | +/// `AsRef` is to be used when wishing to convert to a reference of another |
| 60 | +/// type. |
| 61 | +/// `Borrow` is more related to the notion of taking the reference. It is |
| 62 | +/// useful when wishing to abstract |
| 63 | +/// over the type of reference (`&T`, `&mut T`) or allow both the referenced |
| 64 | +/// and owned type to be treated in the same manner. |
61 | 65 | /// The key difference between the two traits is the intention:
|
62 | 66 | ///
|
63 | 67 | /// - Use `AsRef` when goal is to simply convert into a reference
|
64 |
| -/// - Use `Borrow` when goal is related to writing code that is agnostic to the type of borrow and if is reference or value |
| 68 | +/// - Use `Borrow` when goal is related to writing code that is agnostic to the |
| 69 | +/// type of borrow and if is reference or value |
65 | 70 | ///
|
66 | 71 | /// See [the book][book] for a more detailed comparison.
|
67 | 72 | ///
|
68 | 73 | /// [book]: ../../book/borrow-and-asref.html
|
69 | 74 | /// [`Borrow`]: ../../std/borrow/trait.Borrow.html
|
70 | 75 | ///
|
71 |
| -/// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which |
72 |
| -/// returns an [`Option<T>`] or a [`Result<T, E>`]. |
| 76 | +/// **Note: this trait must not fail**. If the conversion can fail, use a |
| 77 | +/// dedicated method which returns an [`Option<T>`] or a [`Result<T, E>`]. |
73 | 78 | ///
|
74 | 79 | /// [`Option<T>`]: ../../std/option/enum.Option.html
|
75 | 80 | /// [`Result<T, E>`]: ../../std/result/enum.Result.html
|
76 | 81 | ///
|
77 | 82 | /// # Generic Implementations
|
78 | 83 | ///
|
79 | 84 | /// - `AsRef` auto-dereferences if the inner type is a reference or a mutable
|
80 |
| -/// reference (e.g.: `foo.as_ref()` will work the same if `foo` has type `&mut Foo` or `&&mut Foo`) |
| 85 | +/// reference (e.g.: `foo.as_ref()` will work the same if `foo` has type |
| 86 | +/// `&mut Foo` or `&&mut Foo`) |
81 | 87 | ///
|
82 | 88 | /// # Examples
|
83 | 89 | /// An example implementation of the trait is [`Path`].
|
84 | 90 | ///
|
85 |
| -/// [`Path`]: ../../std/struct.Path.html |
| 91 | +/// [`Path`]: ../../std/path/struct.Path.html |
86 | 92 | ///
|
87 | 93 | /// ```
|
88 | 94 | /// impl AsRef<Path> for str {
|
@@ -119,16 +125,17 @@ pub trait AsRef<T: ?Sized> {
|
119 | 125 | ///
|
120 | 126 | /// This trait is similar to `AsRef` but used for converting mutable references.
|
121 | 127 | ///
|
122 |
| -/// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which |
123 |
| -/// returns an [`Option<T>`] or a [`Result<T, E>`]. |
| 128 | +/// **Note: this trait must not fail**. If the conversion can fail, use a |
| 129 | +/// dedicated method which returns an [`Option<T>`] or a [`Result<T, E>`]. |
124 | 130 | ///
|
125 | 131 | /// [`Option<T>`]: ../../std/option/enum.Option.html
|
126 | 132 | /// [`Result<T, E>`]: ../../std/result/enum.Result.html
|
127 | 133 | ///
|
128 | 134 | /// # Generic Implementations
|
129 | 135 | ///
|
130 | 136 | /// - `AsMut` auto-dereferences if the inner type is a reference or a mutable
|
131 |
| -/// reference (e.g.: `foo.as_ref()` will work the same if `foo` has type `&mut Foo` or `&&mut Foo`) |
| 137 | +/// reference (e.g.: `foo.as_ref()` will work the same if `foo` has type |
| 138 | +/// `&mut Foo` or `&&mut Foo`) |
132 | 139 | ///
|
133 | 140 | /// # Examples
|
134 | 141 | ///
|
@@ -161,14 +168,17 @@ pub trait AsMut<T: ?Sized> {
|
161 | 168 | fn as_mut(&mut self) -> &mut T;
|
162 | 169 | }
|
163 | 170 |
|
164 |
| -/// A conversion that consumes `self`, which may or may not be expensive. The reciprocal of [`From`][From]. |
| 171 | +/// A conversion that consumes `self`, which may or may not be expensive. The |
| 172 | +/// reciprocal of [`From`][From]. |
165 | 173 | ///
|
166 |
| -/// **Note: this trait must not fail**. If the conversion can fail, use [`TryInto`] or a dedicated |
167 |
| -/// method which returns an [`Option<T>`] or a [`Result<T, E>`]. |
| 174 | +/// **Note: this trait must not fail**. If the conversion can fail, use |
| 175 | +/// [`TryInto`] or a dedicated method which returns an [`Option<T>`] or a |
| 176 | +/// [`Result<T, E>`]. |
168 | 177 | ///
|
169 |
| -/// Library authors should not directly implement this trait, but should prefer implementing |
170 |
| -/// the [`From`][From] trait, which offers greater flexibility and provides an equivalent `Into` |
171 |
| -/// implementation for free, thanks to a blanket implementation in the standard library. |
| 178 | +/// Library authors should not directly implement this trait, but should prefer |
| 179 | +/// implementing the [`From`][From] trait, which offers greater flexibility and |
| 180 | +/// provides an equivalent `Into` implementation for free, thanks to a blanket |
| 181 | +/// implementation in the standard library. |
172 | 182 | ///
|
173 | 183 | /// # Generic Implementations
|
174 | 184 | ///
|
@@ -202,18 +212,24 @@ pub trait Into<T>: Sized {
|
202 | 212 | fn into(self) -> T;
|
203 | 213 | }
|
204 | 214 |
|
205 |
| -/// Simple and safe type conversions in to `Self`. It is the reciprocal of `Into`. |
| 215 | +/// Simple and safe type conversions in to `Self`. It is the reciprocal of |
| 216 | +/// `Into`. |
206 | 217 | ///
|
207 |
| -/// This trait is useful when performing error handling as described by [the book][book] and is closely related to the `?` operator. |
| 218 | +/// This trait is useful when performing error handling as described by |
| 219 | +/// [the book][book] and is closely related to the `?` operator. |
208 | 220 | ///
|
209 |
| -/// When constructing a function that is capable of failing the return type will generally be of the form `Result<T, E>`. |
210 |
| -/// The `From` trait allows for simplification of error handling by providing a means of returning a single error type that encapsulates |
211 |
| -/// numerous possible erroneous situations. |
212 |
| -/// This trait is not limited to error handling, rather the general case for this trait would be in any type conversions to have an |
213 |
| -/// explicit definition of how they are performed. |
| 221 | +/// When constructing a function that is capable of failing the return type |
| 222 | +/// will generally be of the form `Result<T, E>`. |
| 223 | +/// The `From` trait allows for simplification of error handling by providing a |
| 224 | +/// means of returning a single error type that encapsulates numerous possible |
| 225 | +/// erroneous situations. |
| 226 | +/// This trait is not limited to error handling, rather the general case for |
| 227 | +/// this trait would be in any type conversions to have an explicit definition |
| 228 | +/// of how they are performed. |
214 | 229 | ///
|
215 |
| -/// **Note: this trait must not fail**. If the conversion can fail, use [`TryFrom`] or a dedicated |
216 |
| -/// method which returns an [`Option<T>`] or a [`Result<T, E>`]. |
| 230 | +/// **Note: this trait must not fail**. If the conversion can fail, use |
| 231 | +/// [`TryFrom`] or a dedicated method which returns an [`Option<T>`] or a |
| 232 | +/// [`Result<T, E>`]. |
217 | 233 | ///
|
218 | 234 | /// # Generic Implementations
|
219 | 235 | ///
|
@@ -265,19 +281,21 @@ pub trait Into<T>: Sized {
|
265 | 281 | /// [`String`]: ../../std/string/struct.String.html
|
266 | 282 | /// [`Into<U>`]: trait.Into.html
|
267 | 283 | /// [`from`]: trait.From.html#tymethod.from
|
268 |
| -/// [book]: ../../book/error-handling.html#the-from-trait |
| 284 | +/// [book]: ../../book/error-handling.html |
269 | 285 | #[stable(feature = "rust1", since = "1.0.0")]
|
270 | 286 | pub trait From<T>: Sized {
|
271 | 287 | /// Performs the conversion.
|
272 | 288 | #[stable(feature = "rust1", since = "1.0.0")]
|
273 | 289 | fn from(T) -> Self;
|
274 | 290 | }
|
275 | 291 |
|
276 |
| -/// An attempted conversion that consumes `self`, which may or may not be expensive. |
| 292 | +/// An attempted conversion that consumes `self`, which may or may not be |
| 293 | +/// expensive. |
277 | 294 | ///
|
278 |
| -/// Library authors should not directly implement this trait, but should prefer implementing |
279 |
| -/// the [`TryFrom`] trait, which offers greater flexibility and provides an equivalent `TryInto` |
280 |
| -/// implementation for free, thanks to a blanket implementation in the standard library. |
| 295 | +/// Library authors should not directly implement this trait, but should prefer |
| 296 | +/// implementing the [`TryFrom`] trait, which offers greater flexibility and |
| 297 | +/// provides an equivalent `TryInto` implementation for free, thanks to a |
| 298 | +/// blanket implementation in the standard library. |
281 | 299 | ///
|
282 | 300 | /// [`TryFrom`]: trait.TryFrom.html
|
283 | 301 | #[unstable(feature = "try_from", issue = "33417")]
|
|
0 commit comments