Skip to content

Commit 7897e16

Browse files
committed
Convert docs clean up.
1 parent 7846dbe commit 7897e16

File tree

1 file changed

+114
-31
lines changed

1 file changed

+114
-31
lines changed

src/libcore/convert.rs

Lines changed: 114 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
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
20+
//! - Implement the `As*` traits for reference-to-reference conversions
21+
//! - Implement the [`Into`] trait when you want to consume the value in the conversion
2222
//! - The [`From`] trait is the most flexible, useful for value _and_ reference conversions
2323
//! - The [`TryFrom`] and [`TryInto`] traits behave like [`From`] and [`Into`], but allow for the
2424
//! conversion to fail
@@ -29,7 +29,7 @@
2929
//! equivalent [`Into`] or [`TryInto`] implementations for free, thanks to a blanket implementation
3030
//! in the standard library.
3131
//!
32-
//! # Generic impl
32+
//! # Generic Implementations
3333
//!
3434
//! - [`AsRef`] and [`AsMut`] auto-dereference if the inner type is a reference
3535
//! - [`From`]`<U> for T` implies [`Into`]`<T> for U`
@@ -50,10 +50,20 @@
5050

5151
use str::FromStr;
5252

53-
/// A cheap, reference-to-reference conversion.
53+
/// A cheap reference-to-reference conversion. Used to convert a value to a reference value
54+
/// within generic code.
5455
///
55-
/// `AsRef` is very similar to, but different than, [`Borrow`]. See
56-
/// [the book][book] for more.
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.
61+
/// The key difference between the two traits is the intention:
62+
///
63+
/// - 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
65+
///
66+
/// See [the book][book] for a more detailed comparison.
5767
///
5868
/// [book]: ../../book/borrow-and-asref.html
5969
/// [`Borrow`]: ../../std/borrow/trait.Borrow.html
@@ -64,7 +74,23 @@ use str::FromStr;
6474
/// [`Option<T>`]: ../../std/option/enum.Option.html
6575
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
6676
///
77+
/// # Generic Implementations
78+
///
79+
/// - `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`)
81+
///
6782
/// # Examples
83+
/// An example implementation of the trait is [`Path`].
84+
///
85+
/// [`Path`]: ../../std/struct.Path.html
86+
///
87+
/// ```
88+
/// impl AsRef<Path> for str {
89+
/// fn as_ref(&self) -> &Path {
90+
/// Path::new(self)
91+
/// }
92+
/// }
93+
/// ```
6894
///
6995
/// Both [`String`] and `&str` implement `AsRef<str>`:
7096
///
@@ -82,11 +108,6 @@ use str::FromStr;
82108
/// is_hello(s);
83109
/// ```
84110
///
85-
/// # Generic Impls
86-
///
87-
/// - `AsRef` auto-dereferences if the inner type is a reference or a mutable
88-
/// reference (e.g.: `foo.as_ref()` will work the same if `foo` has type `&mut Foo` or `&&mut Foo`)
89-
///
90111
#[stable(feature = "rust1", since = "1.0.0")]
91112
pub trait AsRef<T: ?Sized> {
92113
/// Performs the conversion.
@@ -96,12 +117,19 @@ pub trait AsRef<T: ?Sized> {
96117

97118
/// A cheap, mutable reference-to-mutable reference conversion.
98119
///
120+
/// This trait is similar to `AsRef` but used for converting mutable references.
121+
///
99122
/// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which
100123
/// returns an [`Option<T>`] or a [`Result<T, E>`].
101124
///
102125
/// [`Option<T>`]: ../../std/option/enum.Option.html
103126
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
104127
///
128+
/// # Generic Implementations
129+
///
130+
/// - `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`)
132+
///
105133
/// # Examples
106134
///
107135
/// [`Box<T>`] implements `AsMut<T>`:
@@ -118,10 +146,13 @@ pub trait AsRef<T: ?Sized> {
118146
/// assert_eq!(*boxed_num, 1);
119147
/// ```
120148
///
121-
/// # Generic Impls
149+
/// Implementing `AsMut`:
122150
///
123-
/// - `AsMut` auto-dereferences if the inner type is a reference or a mutable
124-
/// reference (e.g.: `foo.as_ref()` will work the same if `foo` has type `&mut Foo` or `&&mut Foo`)
151+
/// ```
152+
/// impl Type {
153+
/// let a = 1;
154+
/// }
155+
/// ```
125156
///
126157
#[stable(feature = "rust1", since = "1.0.0")]
127158
pub trait AsMut<T: ?Sized> {
@@ -130,7 +161,7 @@ pub trait AsMut<T: ?Sized> {
130161
fn as_mut(&mut self) -> &mut T;
131162
}
132163

133-
/// A conversion that consumes `self`, which may or may not be expensive.
164+
/// A conversion that consumes `self`, which may or may not be expensive. The reciprocal of [`From`][From].
134165
///
135166
/// **Note: this trait must not fail**. If the conversion can fail, use [`TryInto`] or a dedicated
136167
/// method which returns an [`Option<T>`] or a [`Result<T, E>`].
@@ -139,6 +170,11 @@ pub trait AsMut<T: ?Sized> {
139170
/// the [`From`][From] trait, which offers greater flexibility and provides an equivalent `Into`
140171
/// implementation for free, thanks to a blanket implementation in the standard library.
141172
///
173+
/// # Generic Implementations
174+
///
175+
/// - [`From<T>`][From]` for U` implies `Into<U> for T`
176+
/// - [`into`] is reflexive, which means that `Into<T> for T` is implemented
177+
///
142178
/// # Examples
143179
///
144180
/// [`String`] implements `Into<Vec<u8>>`:
@@ -153,11 +189,6 @@ pub trait AsMut<T: ?Sized> {
153189
/// is_hello(s);
154190
/// ```
155191
///
156-
/// # Generic Impls
157-
///
158-
/// - [`From<T>`][From]` for U` implies `Into<U> for T`
159-
/// - [`into`] is reflexive, which means that `Into<T> for T` is implemented
160-
///
161192
/// [`TryInto`]: trait.TryInto.html
162193
/// [`Option<T>`]: ../../std/option/enum.Option.html
163194
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
@@ -171,11 +202,24 @@ pub trait Into<T>: Sized {
171202
fn into(self) -> T;
172203
}
173204

174-
/// Construct `Self` via a conversion.
205+
/// Simple and safe type conversions in to `Self`. It is the reciprocal of `Into`.
206+
///
207+
/// This trait is useful when performing error handling as described by [the book][book] and is closely related to the `?` operator.
208+
///
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.
175214
///
176215
/// **Note: this trait must not fail**. If the conversion can fail, use [`TryFrom`] or a dedicated
177216
/// method which returns an [`Option<T>`] or a [`Result<T, E>`].
178217
///
218+
/// # Generic Implementations
219+
///
220+
/// - `From<T> for U` implies [`Into<U>`]` for T`
221+
/// - [`from`] is reflexive, which means that `From<T> for T` is implemented
222+
///
179223
/// # Examples
180224
///
181225
/// [`String`] implements `From<&str>`:
@@ -186,17 +230,42 @@ pub trait Into<T>: Sized {
186230
///
187231
/// assert_eq!(string, other_string);
188232
/// ```
189-
/// # Generic impls
190233
///
191-
/// - `From<T> for U` implies [`Into<U>`]` for T`
192-
/// - [`from`] is reflexive, which means that `From<T> for T` is implemented
234+
/// An example usage for error handling:
235+
///
236+
/// ```
237+
/// enum CliError {
238+
/// IoError(io::Error),
239+
/// ParseError(num::ParseIntError),
240+
/// }
241+
///
242+
/// impl From<io::Error> for MyError {
243+
/// fn from(error: io::Error) -> Self {
244+
/// CliError::IoError(error)
245+
/// }
246+
/// }
247+
///
248+
/// impl From<num::ParseIntError> for MyError {
249+
/// fn from(error: io::Error) -> Self {
250+
/// CliError::ParseError(error)
251+
/// }
252+
/// }
253+
///
254+
/// fn open_and_parse_file(file_name: &str) -> Result<i32, MyError> {
255+
/// let file = std::fs::File::open("test")?;
256+
/// let mut contents = String::new();
257+
/// file.read_to_string(&mut contents)?;
258+
/// let num: i32 = contents.trim().parse()?;
259+
/// }
260+
/// ```
193261
///
194262
/// [`TryFrom`]: trait.TryFrom.html
195263
/// [`Option<T>`]: ../../std/option/enum.Option.html
196264
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
197265
/// [`String`]: ../../std/string/struct.String.html
198266
/// [`Into<U>`]: trait.Into.html
199267
/// [`from`]: trait.From.html#tymethod.from
268+
/// [book]: ../../book/error-handling.html#the-from-trait
200269
#[stable(feature = "rust1", since = "1.0.0")]
201270
pub trait From<T>: Sized {
202271
/// Performs the conversion.
@@ -236,15 +305,19 @@ pub trait TryFrom<T>: Sized {
236305

237306
// As lifts over &
238307
#[stable(feature = "rust1", since = "1.0.0")]
239-
impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a T where T: AsRef<U> {
308+
impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a T
309+
where T: AsRef<U>
310+
{
240311
fn as_ref(&self) -> &U {
241312
<T as AsRef<U>>::as_ref(*self)
242313
}
243314
}
244315

245316
// As lifts over &mut
246317
#[stable(feature = "rust1", since = "1.0.0")]
247-
impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a mut T where T: AsRef<U> {
318+
impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a mut T
319+
where T: AsRef<U>
320+
{
248321
fn as_ref(&self) -> &U {
249322
<T as AsRef<U>>::as_ref(*self)
250323
}
@@ -260,7 +333,9 @@ impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a mut T where T: AsRef<U> {
260333

261334
// AsMut lifts over &mut
262335
#[stable(feature = "rust1", since = "1.0.0")]
263-
impl<'a, T: ?Sized, U: ?Sized> AsMut<U> for &'a mut T where T: AsMut<U> {
336+
impl<'a, T: ?Sized, U: ?Sized> AsMut<U> for &'a mut T
337+
where T: AsMut<U>
338+
{
264339
fn as_mut(&mut self) -> &mut U {
265340
(*self).as_mut()
266341
}
@@ -276,7 +351,9 @@ impl<'a, T: ?Sized, U: ?Sized> AsMut<U> for &'a mut T where T: AsMut<U> {
276351

277352
// From implies Into
278353
#[stable(feature = "rust1", since = "1.0.0")]
279-
impl<T, U> Into<U> for T where U: From<T> {
354+
impl<T, U> Into<U> for T
355+
where U: From<T>
356+
{
280357
fn into(self) -> U {
281358
U::from(self)
282359
}
@@ -285,13 +362,17 @@ impl<T, U> Into<U> for T where U: From<T> {
285362
// From (and thus Into) is reflexive
286363
#[stable(feature = "rust1", since = "1.0.0")]
287364
impl<T> From<T> for T {
288-
fn from(t: T) -> T { t }
365+
fn from(t: T) -> T {
366+
t
367+
}
289368
}
290369

291370

292371
// TryFrom implies TryInto
293372
#[unstable(feature = "try_from", issue = "33417")]
294-
impl<T, U> TryInto<U> for T where U: TryFrom<T> {
373+
impl<T, U> TryInto<U> for T
374+
where U: TryFrom<T>
375+
{
295376
type Error = U::Error;
296377

297378
fn try_into(self) -> Result<U, U::Error> {
@@ -327,7 +408,9 @@ impl AsRef<str> for str {
327408

328409
// FromStr implies TryFrom<&str>
329410
#[unstable(feature = "try_from", issue = "33417")]
330-
impl<'a, T> TryFrom<&'a str> for T where T: FromStr {
411+
impl<'a, T> TryFrom<&'a str> for T
412+
where T: FromStr
413+
{
331414
type Error = <T as FromStr>::Err;
332415

333416
fn try_from(s: &'a str) -> Result<T, Self::Error> {

0 commit comments

Comments
 (0)