@@ -816,12 +816,12 @@ impl Error {
816816 }
817817 }
818818
819- /// Attempt to downgrade the inner error to `E` if any.
819+ /// Attempt to downcast the inner error to `E` if any.
820820 ///
821821 /// If this [`Error`] was constructed via [`new`] then this function will
822822 /// attempt to perform downgrade on it, otherwise it will return [`Err`].
823823 ///
824- /// If downgrade succeeds, it will return [`Ok`], otherwise it will also
824+ /// If the downcast succeeds, it will return [`Ok`], otherwise it will also
825825 /// return [`Err`].
826826 ///
827827 /// [`new`]: Error::new
@@ -852,13 +852,39 @@ impl Error {
852852 /// impl From<io::Error> for E {
853853 /// fn from(err: io::Error) -> E {
854854 /// err.downcast::<E>()
855- /// .map(|b| *b)
856855 /// .unwrap_or_else(E::Io)
857856 /// }
858857 /// }
858+ ///
859+ /// impl From<E> for io::Error {
860+ /// fn from(err: E) -> io::Error {
861+ /// match err {
862+ /// E::Io(io_error) => io_error,
863+ /// e => io::Error::new(io::ErrorKind::Other, e),
864+ /// }
865+ /// }
866+ /// }
867+ ///
868+ /// # fn main() {
869+ /// let e = E::SomeOtherVariant;
870+ /// // Convert it to an io::Error
871+ /// let io_error = io::Error::from(e);
872+ /// // Cast it back to the original variant
873+ /// let e = E::from(io_error);
874+ /// assert!(matches!(e, E::SomeOtherVariant));
875+ ///
876+ /// let io_error = io::Error::from(io::ErrorKind::AlreadyExists);
877+ /// // Convert it to E
878+ /// let e = E::from(io_error);
879+ /// // Cast it back to the original variant
880+ /// let io_error = io::Error::from(e);
881+ /// assert_eq!(io_error.kind(), io::ErrorKind::AlreadyExists);
882+ /// assert!(io_error.get_ref().is_none());
883+ /// assert!(io_error.raw_os_error().is_none());
884+ /// # }
859885 /// ```
860886 #[ unstable( feature = "io_error_downcast" , issue = "99262" ) ]
861- pub fn downcast < E > ( self ) -> result:: Result < Box < E > , Self >
887+ pub fn downcast < E > ( self ) -> result:: Result < E , Self >
862888 where
863889 E : error:: Error + Send + Sync + ' static ,
864890 {
@@ -872,7 +898,7 @@ impl Error {
872898 // And the compiler should be able to eliminate the branch
873899 // that produces `Err` here since b.error.is::<E>()
874900 // returns true.
875- Ok ( res. unwrap ( ) )
901+ Ok ( * res. unwrap ( ) )
876902 }
877903 repr_data => Err ( Self { repr : Repr :: new ( repr_data) } ) ,
878904 }
0 commit comments