14
14
//! conversions from one type to another. They follow the standard
15
15
//! Rust conventions of `as`/`to`/`into`/`from`.
16
16
17
- #![ unstable( feature = "convert" ,
18
- reason = "recently added, experimental traits" ) ]
17
+ #![ stable( feature = "rust1" , since = "1.0.0" ) ]
19
18
20
19
use marker:: Sized ;
21
20
22
21
/// A cheap, reference-to-reference conversion.
22
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
23
23
pub trait AsRef < T : ?Sized > {
24
24
/// Perform the conversion.
25
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
25
26
fn as_ref ( & self ) -> & T ;
26
27
}
27
28
28
29
/// A cheap, mutable reference-to-mutable reference conversion.
30
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
29
31
pub trait AsMut < T : ?Sized > {
30
32
/// Perform the conversion.
33
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
31
34
fn as_mut ( & mut self ) -> & mut T ;
32
35
}
33
36
34
37
/// A conversion that consumes `self`, which may or may not be
35
38
/// expensive.
39
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
36
40
pub trait Into < T > : Sized {
37
41
/// Perform the conversion.
42
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
38
43
fn into ( self ) -> T ;
39
44
}
40
45
41
46
/// Construct `Self` via a conversion.
47
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
42
48
pub trait From < T > {
43
49
/// Perform the conversion.
50
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
44
51
fn from ( T ) -> Self ;
45
52
}
46
53
47
54
////////////////////////////////////////////////////////////////////////////////
48
55
// GENERIC IMPLS
49
56
////////////////////////////////////////////////////////////////////////////////
50
57
51
- // As implies Into
52
- impl < ' a , T : ?Sized , U : ?Sized > Into < & ' a U > for & ' a T where T : AsRef < U > {
53
- fn into ( self ) -> & ' a U {
54
- self . as_ref ( )
55
- }
56
- }
57
-
58
58
// As lifts over &
59
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
59
60
impl < ' a , T : ?Sized , U : ?Sized > AsRef < U > for & ' a T where T : AsRef < U > {
60
61
fn as_ref ( & self ) -> & U {
61
62
<T as AsRef < U > >:: as_ref ( * self )
62
63
}
63
64
}
64
65
65
66
// As lifts over &mut
67
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
66
68
impl < ' a , T : ?Sized , U : ?Sized > AsRef < U > for & ' a mut T where T : AsRef < U > {
67
69
fn as_ref ( & self ) -> & U {
68
70
<T as AsRef < U > >:: as_ref ( * self )
@@ -77,14 +79,8 @@ impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a mut T where T: AsRef<U> {
77
79
// }
78
80
// }
79
81
80
- // AsMut implies Into
81
- impl < ' a , T : ?Sized , U : ?Sized > Into < & ' a mut U > for & ' a mut T where T : AsMut < U > {
82
- fn into ( self ) -> & ' a mut U {
83
- ( * self ) . as_mut ( )
84
- }
85
- }
86
-
87
82
// AsMut lifts over &mut
83
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
88
84
impl < ' a , T : ?Sized , U : ?Sized > AsMut < U > for & ' a mut T where T : AsMut < U > {
89
85
fn as_mut ( & mut self ) -> & mut U {
90
86
( * self ) . as_mut ( )
@@ -100,28 +96,38 @@ impl<'a, T: ?Sized, U: ?Sized> AsMut<U> for &'a mut T where T: AsMut<U> {
100
96
// }
101
97
102
98
// From implies Into
99
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
103
100
impl < T , U > Into < U > for T where U : From < T > {
104
101
fn into ( self ) -> U {
105
102
U :: from ( self )
106
103
}
107
104
}
108
105
106
+ // From (and thus Into) is reflexive
107
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
108
+ impl < T > From < T > for T {
109
+ fn from ( t : T ) -> T { t }
110
+ }
111
+
109
112
////////////////////////////////////////////////////////////////////////////////
110
113
// CONCRETE IMPLS
111
114
////////////////////////////////////////////////////////////////////////////////
112
115
116
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
113
117
impl < T > AsRef < [ T ] > for [ T ] {
114
118
fn as_ref ( & self ) -> & [ T ] {
115
119
self
116
120
}
117
121
}
118
122
123
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
119
124
impl < T > AsMut < [ T ] > for [ T ] {
120
125
fn as_mut ( & mut self ) -> & mut [ T ] {
121
126
self
122
127
}
123
128
}
124
129
130
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
125
131
impl AsRef < str > for str {
126
132
fn as_ref ( & self ) -> & str {
127
133
self
0 commit comments