37
37
//! Moreover, a switch to, e.g. `P<'a, T>` would be easy and mostly automated.
38
38
39
39
use std:: fmt:: { self , Display , Debug } ;
40
- use std:: hash :: { Hash , Hasher } ;
40
+ use std:: iter :: FromIterator ;
41
41
use std:: ops:: Deref ;
42
- use std:: ptr;
42
+ use std:: { ptr, slice , vec } ;
43
43
44
44
use serialize:: { Encodable , Decodable , Encoder , Decoder } ;
45
45
46
46
/// An owned smart pointer.
47
- pub struct P < T > {
47
+ #[ derive( Hash , PartialEq , Eq , PartialOrd , Ord ) ]
48
+ pub struct P < T : ?Sized > {
48
49
ptr : Box < T >
49
50
}
50
51
@@ -92,14 +93,6 @@ impl<T: 'static + Clone> Clone for P<T> {
92
93
}
93
94
}
94
95
95
- impl < T : PartialEq > PartialEq for P < T > {
96
- fn eq ( & self , other : & P < T > ) -> bool {
97
- * * self == * * other
98
- }
99
- }
100
-
101
- impl < T : Eq > Eq for P < T > { }
102
-
103
96
impl < T : Debug > Debug for P < T > {
104
97
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
105
98
Debug :: fmt ( & * * self , f)
@@ -111,19 +104,12 @@ impl<T: Display> Display for P<T> {
111
104
}
112
105
}
113
106
114
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
115
107
impl < T > fmt:: Pointer for P < T > {
116
108
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
117
109
fmt:: Pointer :: fmt ( & self . ptr , f)
118
110
}
119
111
}
120
112
121
- impl < T : Hash > Hash for P < T > {
122
- fn hash < H : Hasher > ( & self , state : & mut H ) {
123
- ( * * self ) . hash ( state) ;
124
- }
125
- }
126
-
127
113
impl < T : ' static + Decodable > Decodable for P < T > {
128
114
fn decode < D : Decoder > ( d : & mut D ) -> Result < P < T > , D :: Error > {
129
115
Decodable :: decode ( d) . map ( P )
@@ -135,3 +121,87 @@ impl<T: Encodable> Encodable for P<T> {
135
121
( * * self ) . encode ( s)
136
122
}
137
123
}
124
+
125
+
126
+ impl < T : fmt:: Debug > fmt:: Debug for P < [ T ] > {
127
+ fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
128
+ self . ptr . fmt ( fmt)
129
+ }
130
+ }
131
+
132
+ impl < T > P < [ T ] > {
133
+ pub fn empty ( ) -> P < [ T ] > {
134
+ P { ptr : Default :: default ( ) }
135
+ }
136
+
137
+ #[ inline( never) ]
138
+ pub fn from_vec ( v : Vec < T > ) -> P < [ T ] > {
139
+ P { ptr : v. into_boxed_slice ( ) }
140
+ }
141
+
142
+ #[ inline( never) ]
143
+ pub fn into_vec ( self ) -> Vec < T > {
144
+ self . ptr . into_vec ( )
145
+ }
146
+
147
+ pub fn as_slice < ' a > ( & ' a self ) -> & ' a [ T ] {
148
+ & * self . ptr
149
+ }
150
+
151
+ pub fn move_iter ( self ) -> vec:: IntoIter < T > {
152
+ self . into_vec ( ) . into_iter ( )
153
+ }
154
+
155
+ pub fn map < U , F : FnMut ( & T ) -> U > ( & self , f : F ) -> P < [ U ] > {
156
+ self . iter ( ) . map ( f) . collect ( )
157
+ }
158
+ }
159
+
160
+ impl < T > Deref for P < [ T ] > {
161
+ type Target = [ T ] ;
162
+
163
+ fn deref ( & self ) -> & [ T ] {
164
+ self . as_slice ( )
165
+ }
166
+ }
167
+
168
+ impl < T > Default for P < [ T ] > {
169
+ fn default ( ) -> P < [ T ] > {
170
+ P :: empty ( )
171
+ }
172
+ }
173
+
174
+ impl < T : Clone > Clone for P < [ T ] > {
175
+ fn clone ( & self ) -> P < [ T ] > {
176
+ P :: from_vec ( self . to_vec ( ) )
177
+ }
178
+ }
179
+
180
+ impl < T > FromIterator < T > for P < [ T ] > {
181
+ fn from_iter < I : IntoIterator < Item =T > > ( iter : I ) -> P < [ T ] > {
182
+ P :: from_vec ( iter. into_iter ( ) . collect ( ) )
183
+ }
184
+ }
185
+
186
+ impl < ' a , T > IntoIterator for & ' a P < [ T ] > {
187
+ type Item = & ' a T ;
188
+ type IntoIter = slice:: Iter < ' a , T > ;
189
+ fn into_iter ( self ) -> Self :: IntoIter {
190
+ self . ptr . into_iter ( )
191
+ }
192
+ }
193
+
194
+ impl < T : Encodable > Encodable for P < [ T ] > {
195
+ fn encode < S : Encoder > ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
196
+ Encodable :: encode ( & * * self , s)
197
+ }
198
+ }
199
+
200
+ impl < T : Decodable > Decodable for P < [ T ] > {
201
+ fn decode < D : Decoder > ( d : & mut D ) -> Result < P < [ T ] > , D :: Error > {
202
+ Ok ( P :: from_vec ( match Decodable :: decode ( d) {
203
+ Ok ( t) => t,
204
+ Err ( e) => return Err ( e)
205
+ } ) )
206
+ }
207
+ }
0 commit comments