35
35
//! - [`From`]`<U> for T` implies [`Into`]`<T> for U`
36
36
//! - [`TryFrom`]`<U> for T` implies [`TryInto`]`<T> for U`
37
37
//! - [`From`] and [`Into`] are reflexive, which means that all types can
38
- //! `into() ` themselves and `from() ` themselves
38
+ //! `into` themselves and `from` themselves
39
39
//!
40
40
//! See each trait for usage examples.
41
41
//!
@@ -59,14 +59,15 @@ use str::FromStr;
59
59
/// `AsRef` is to be used when wishing to convert to a reference of another
60
60
/// type.
61
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.
62
+ /// useful when wishing to abstract over the type of reference
63
+ /// (`&T`, `&mut T`) or allow both the referenced and owned type to be treated
64
+ /// in the same manner.
65
+ ///
65
66
/// The key difference between the two traits is the intention:
66
67
///
67
68
/// - Use `AsRef` when goal is to simply convert into a reference
68
69
/// - Use `Borrow` when goal is related to writing code that is agnostic to the
69
- /// type of borrow and if is reference or value
70
+ /// type of borrow and if is reference or value
70
71
///
71
72
/// See [the book][book] for a more detailed comparison.
72
73
///
@@ -82,8 +83,8 @@ use str::FromStr;
82
83
/// # Generic Implementations
83
84
///
84
85
/// - `AsRef` auto-dereferences if the inner type is a reference or a mutable
85
- /// reference (e.g.: `foo.as_ref()` will work the same if `foo` has type
86
- /// `&mut Foo` or `&&mut Foo`)
86
+ /// reference (e.g.: `foo.as_ref()` will work the same if `foo` has type
87
+ /// `&mut Foo` or `&&mut Foo`)
87
88
///
88
89
/// # Examples
89
90
///
@@ -124,8 +125,8 @@ pub trait AsRef<T: ?Sized> {
124
125
/// # Generic Implementations
125
126
///
126
127
/// - `AsMut` auto-dereferences if the inner type is a reference or a mutable
127
- /// reference (e.g.: `foo.as_ref()` will work the same if `foo` has type
128
- /// `&mut Foo` or `&&mut Foo`)
128
+ /// reference (e.g.: `foo.as_ref()` will work the same if `foo` has type
129
+ /// `&mut Foo` or `&&mut Foo`)
129
130
///
130
131
/// # Examples
131
132
///
@@ -203,9 +204,11 @@ pub trait Into<T>: Sized {
203
204
///
204
205
/// When constructing a function that is capable of failing the return type
205
206
/// will generally be of the form `Result<T, E>`.
207
+ ///
206
208
/// The `From` trait allows for simplification of error handling by providing a
207
209
/// means of returning a single error type that encapsulates numerous possible
208
210
/// erroneous situations.
211
+ ///
209
212
/// This trait is not limited to error handling, rather the general case for
210
213
/// this trait would be in any type conversions to have an explicit definition
211
214
/// of how they are performed.
@@ -310,8 +313,7 @@ pub trait TryFrom<T>: Sized {
310
313
311
314
// As lifts over &
312
315
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
313
- impl < ' a , T : ?Sized , U : ?Sized > AsRef < U > for & ' a T
314
- where T : AsRef < U >
316
+ impl < ' a , T : ?Sized , U : ?Sized > AsRef < U > for & ' a T where T : AsRef < U >
315
317
{
316
318
fn as_ref ( & self ) -> & U {
317
319
<T as AsRef < U > >:: as_ref ( * self )
@@ -320,8 +322,7 @@ impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a T
320
322
321
323
// As lifts over &mut
322
324
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
323
- impl < ' a , T : ?Sized , U : ?Sized > AsRef < U > for & ' a mut T
324
- where T : AsRef < U >
325
+ impl < ' a , T : ?Sized , U : ?Sized > AsRef < U > for & ' a mut T where T : AsRef < U >
325
326
{
326
327
fn as_ref ( & self ) -> & U {
327
328
<T as AsRef < U > >:: as_ref ( * self )
@@ -338,8 +339,7 @@ impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a mut T
338
339
339
340
// AsMut lifts over &mut
340
341
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
341
- impl < ' a , T : ?Sized , U : ?Sized > AsMut < U > for & ' a mut T
342
- where T : AsMut < U >
342
+ impl < ' a , T : ?Sized , U : ?Sized > AsMut < U > for & ' a mut T where T : AsMut < U >
343
343
{
344
344
fn as_mut ( & mut self ) -> & mut U {
345
345
( * self ) . as_mut ( )
@@ -356,8 +356,7 @@ impl<'a, T: ?Sized, U: ?Sized> AsMut<U> for &'a mut T
356
356
357
357
// From implies Into
358
358
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
359
- impl < T , U > Into < U > for T
360
- where U : From < T >
359
+ impl < T , U > Into < U > for T where U : From < T >
361
360
{
362
361
fn into ( self ) -> U {
363
362
U :: from ( self )
@@ -367,16 +366,13 @@ impl<T, U> Into<U> for T
367
366
// From (and thus Into) is reflexive
368
367
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
369
368
impl < T > From < T > for T {
370
- fn from ( t : T ) -> T {
371
- t
372
- }
369
+ fn from ( t : T ) -> T { t }
373
370
}
374
371
375
372
376
373
// TryFrom implies TryInto
377
374
#[ unstable( feature = "try_from" , issue = "33417" ) ]
378
- impl < T , U > TryInto < U > for T
379
- where U : TryFrom < T >
375
+ impl < T , U > TryInto < U > for T where U : TryFrom < T >
380
376
{
381
377
type Error = U :: Error ;
382
378
@@ -413,8 +409,7 @@ impl AsRef<str> for str {
413
409
414
410
// FromStr implies TryFrom<&str>
415
411
#[ unstable( feature = "try_from" , issue = "33417" ) ]
416
- impl < ' a , T > TryFrom < & ' a str > for T
417
- where T : FromStr
412
+ impl < ' a , T > TryFrom < & ' a str > for T where T : FromStr
418
413
{
419
414
type Error = <T as FromStr >:: Err ;
420
415
0 commit comments