@@ -168,7 +168,7 @@ impl Error for string::FromUtf16Error {
168
168
// copied from any.rs
169
169
impl Error + ' static {
170
170
/// Returns true if the boxed type is the same as `T`
171
- #[ unstable ( feature = "error_downcast" , reason = "recently added " ) ]
171
+ #[ stable ( feature = "error_downcast" , since = "1.3.0 " ) ]
172
172
#[ inline]
173
173
pub fn is < T : Error + ' static > ( & self ) -> bool {
174
174
// Get TypeId of the type this function is instantiated with
@@ -183,7 +183,7 @@ impl Error + 'static {
183
183
184
184
/// Returns some reference to the boxed value if it is of type `T`, or
185
185
/// `None` if it isn't.
186
- #[ unstable ( feature = "error_downcast" , reason = "recently added " ) ]
186
+ #[ stable ( feature = "error_downcast" , since = "1.3.0 " ) ]
187
187
#[ inline]
188
188
pub fn downcast_ref < T : Error + ' static > ( & self ) -> Option < & T > {
189
189
if self . is :: < T > ( ) {
@@ -201,7 +201,7 @@ impl Error + 'static {
201
201
202
202
/// Returns some mutable reference to the boxed value if it is of type `T`, or
203
203
/// `None` if it isn't.
204
- #[ unstable ( feature = "error_downcast" , reason = "recently added " ) ]
204
+ #[ stable ( feature = "error_downcast" , since = "1.3.0 " ) ]
205
205
#[ inline]
206
206
pub fn downcast_mut < T : Error + ' static > ( & mut self ) -> Option < & mut T > {
207
207
if self . is :: < T > ( ) {
@@ -220,21 +220,44 @@ impl Error + 'static {
220
220
221
221
impl Error + ' static + Send {
222
222
/// Forwards to the method defined on the type `Any`.
223
- #[ unstable ( feature = "error_downcast" , reason = "recently added " ) ]
223
+ #[ stable ( feature = "error_downcast" , since = "1.3.0 " ) ]
224
224
#[ inline]
225
225
pub fn is < T : Error + ' static > ( & self ) -> bool {
226
226
<Error + ' static >:: is :: < T > ( self )
227
227
}
228
228
229
229
/// Forwards to the method defined on the type `Any`.
230
- #[ unstable ( feature = "error_downcast" , reason = "recently added " ) ]
230
+ #[ stable ( feature = "error_downcast" , since = "1.3.0 " ) ]
231
231
#[ inline]
232
232
pub fn downcast_ref < T : Error + ' static > ( & self ) -> Option < & T > {
233
233
<Error + ' static >:: downcast_ref :: < T > ( self )
234
234
}
235
235
236
236
/// Forwards to the method defined on the type `Any`.
237
- #[ unstable( feature = "error_downcast" , reason = "recently added" ) ]
237
+ #[ stable( feature = "error_downcast" , since = "1.3.0" ) ]
238
+ #[ inline]
239
+ pub fn downcast_mut < T : Error + ' static > ( & mut self ) -> Option < & mut T > {
240
+ <Error + ' static >:: downcast_mut :: < T > ( self )
241
+ }
242
+ }
243
+
244
+ impl Error + ' static + Send + Sync {
245
+ /// Forwards to the method defined on the type `Any`.
246
+ #[ stable( feature = "error_downcast" , since = "1.3.0" ) ]
247
+ #[ inline]
248
+ pub fn is < T : Error + ' static > ( & self ) -> bool {
249
+ <Error + ' static >:: is :: < T > ( self )
250
+ }
251
+
252
+ /// Forwards to the method defined on the type `Any`.
253
+ #[ stable( feature = "error_downcast" , since = "1.3.0" ) ]
254
+ #[ inline]
255
+ pub fn downcast_ref < T : Error + ' static > ( & self ) -> Option < & T > {
256
+ <Error + ' static >:: downcast_ref :: < T > ( self )
257
+ }
258
+
259
+ /// Forwards to the method defined on the type `Any`.
260
+ #[ stable( feature = "error_downcast" , since = "1.3.0" ) ]
238
261
#[ inline]
239
262
pub fn downcast_mut < T : Error + ' static > ( & mut self ) -> Option < & mut T > {
240
263
<Error + ' static >:: downcast_mut :: < T > ( self )
@@ -243,7 +266,7 @@ impl Error + 'static + Send {
243
266
244
267
impl Error {
245
268
#[ inline]
246
- #[ unstable ( feature = "error_downcast" , reason = "recently added " ) ]
269
+ #[ stable ( feature = "error_downcast" , since = "1.3.0 " ) ]
247
270
/// Attempt to downcast the box to a concrete type.
248
271
pub fn downcast < T : Error + ' static > ( self : Box < Self > ) -> Result < Box < T > , Box < Error > > {
249
272
if self . is :: < T > ( ) {
@@ -264,13 +287,74 @@ impl Error {
264
287
265
288
impl Error + Send {
266
289
#[ inline]
267
- #[ unstable ( feature = "error_downcast" , reason = "recently added " ) ]
290
+ #[ stable ( feature = "error_downcast" , since = "1.3.0 " ) ]
268
291
/// Attempt to downcast the box to a concrete type.
269
- pub fn downcast < T : Error + ' static > ( self : Box < Self > ) -> Result < Box < T > , Box < Error + Send > > {
292
+ pub fn downcast < T : Error + ' static > ( self : Box < Self > )
293
+ -> Result < Box < T > , Box < Error + Send > > {
270
294
let err: Box < Error > = self ;
271
295
<Error >:: downcast ( err) . map_err ( |s| unsafe {
272
296
// reapply the Send marker
273
297
transmute :: < Box < Error > , Box < Error + Send > > ( s)
274
298
} )
275
299
}
276
300
}
301
+
302
+ impl Error + Send + Sync {
303
+ #[ inline]
304
+ #[ stable( feature = "error_downcast" , since = "1.3.0" ) ]
305
+ /// Attempt to downcast the box to a concrete type.
306
+ pub fn downcast < T : Error + ' static > ( self : Box < Self > )
307
+ -> Result < Box < T > , Box < Self > > {
308
+ let err: Box < Error > = self ;
309
+ <Error >:: downcast ( err) . map_err ( |s| unsafe {
310
+ // reapply the Send+Sync marker
311
+ transmute :: < Box < Error > , Box < Error + Send + Sync > > ( s)
312
+ } )
313
+ }
314
+ }
315
+
316
+ #[ cfg( test) ]
317
+ mod tests {
318
+ use prelude:: v1:: * ;
319
+ use super :: Error ;
320
+ use fmt;
321
+
322
+ #[ derive( Debug , PartialEq ) ]
323
+ struct A ;
324
+ #[ derive( Debug , PartialEq ) ]
325
+ struct B ;
326
+
327
+ impl fmt:: Display for A {
328
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
329
+ write ! ( f, "A" )
330
+ }
331
+ }
332
+ impl fmt:: Display for B {
333
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
334
+ write ! ( f, "B" )
335
+ }
336
+ }
337
+
338
+ impl Error for A {
339
+ fn description ( & self ) -> & str { "A-desc" }
340
+ }
341
+ impl Error for B {
342
+ fn description ( & self ) -> & str { "A-desc" }
343
+ }
344
+
345
+ #[ test]
346
+ fn downcasting ( ) {
347
+ let mut a = A ;
348
+ let mut a = & mut a as & mut ( Error + ' static ) ;
349
+ assert_eq ! ( a. downcast_ref:: <A >( ) , Some ( & A ) ) ;
350
+ assert_eq ! ( a. downcast_ref:: <B >( ) , None ) ;
351
+ assert_eq ! ( a. downcast_mut:: <A >( ) , Some ( & mut A ) ) ;
352
+ assert_eq ! ( a. downcast_mut:: <B >( ) , None ) ;
353
+
354
+ let a: Box < Error > = Box :: new ( A ) ;
355
+ match a. downcast :: < B > ( ) {
356
+ Ok ( ..) => panic ! ( "expected error" ) ,
357
+ Err ( e) => assert_eq ! ( * e. downcast:: <A >( ) . unwrap( ) , A ) ,
358
+ }
359
+ }
360
+ }
0 commit comments