@@ -168,7 +168,7 @@ impl Error for string::FromUtf16Error {
168168// copied from any.rs
169169impl Error + ' static {
170170 /// 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 " ) ]
172172 #[ inline]
173173 pub fn is < T : Error + ' static > ( & self ) -> bool {
174174 // Get TypeId of the type this function is instantiated with
@@ -183,7 +183,7 @@ impl Error + 'static {
183183
184184 /// Returns some reference to the boxed value if it is of type `T`, or
185185 /// `None` if it isn't.
186- #[ unstable ( feature = "error_downcast" , reason = "recently added " ) ]
186+ #[ stable ( feature = "error_downcast" , since = "1.3.0 " ) ]
187187 #[ inline]
188188 pub fn downcast_ref < T : Error + ' static > ( & self ) -> Option < & T > {
189189 if self . is :: < T > ( ) {
@@ -201,7 +201,7 @@ impl Error + 'static {
201201
202202 /// Returns some mutable reference to the boxed value if it is of type `T`, or
203203 /// `None` if it isn't.
204- #[ unstable ( feature = "error_downcast" , reason = "recently added " ) ]
204+ #[ stable ( feature = "error_downcast" , since = "1.3.0 " ) ]
205205 #[ inline]
206206 pub fn downcast_mut < T : Error + ' static > ( & mut self ) -> Option < & mut T > {
207207 if self . is :: < T > ( ) {
@@ -220,21 +220,44 @@ impl Error + 'static {
220220
221221impl Error + ' static + Send {
222222 /// 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 " ) ]
224224 #[ inline]
225225 pub fn is < T : Error + ' static > ( & self ) -> bool {
226226 <Error + ' static >:: is :: < T > ( self )
227227 }
228228
229229 /// 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 " ) ]
231231 #[ inline]
232232 pub fn downcast_ref < T : Error + ' static > ( & self ) -> Option < & T > {
233233 <Error + ' static >:: downcast_ref :: < T > ( self )
234234 }
235235
236236 /// 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" ) ]
238261 #[ inline]
239262 pub fn downcast_mut < T : Error + ' static > ( & mut self ) -> Option < & mut T > {
240263 <Error + ' static >:: downcast_mut :: < T > ( self )
@@ -243,7 +266,7 @@ impl Error + 'static + Send {
243266
244267impl Error {
245268 #[ inline]
246- #[ unstable ( feature = "error_downcast" , reason = "recently added " ) ]
269+ #[ stable ( feature = "error_downcast" , since = "1.3.0 " ) ]
247270 /// Attempt to downcast the box to a concrete type.
248271 pub fn downcast < T : Error + ' static > ( self : Box < Self > ) -> Result < Box < T > , Box < Error > > {
249272 if self . is :: < T > ( ) {
@@ -264,13 +287,74 @@ impl Error {
264287
265288impl Error + Send {
266289 #[ inline]
267- #[ unstable ( feature = "error_downcast" , reason = "recently added " ) ]
290+ #[ stable ( feature = "error_downcast" , since = "1.3.0 " ) ]
268291 /// 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 > > {
270294 let err: Box < Error > = self ;
271295 <Error >:: downcast ( err) . map_err ( |s| unsafe {
272296 // reapply the Send marker
273297 transmute :: < Box < Error > , Box < Error + Send > > ( s)
274298 } )
275299 }
276300}
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