Skip to content

Commit 2d26783

Browse files
authored
Unrolled build for rust-lang#120117
Rollup merge of rust-lang#120117 - NobodyXu:99262/update-api-and-doc, r=m-ou-se Update `std::io::Error::downcast` return type and update its doc according to decision made by rust libs-api team in rust-lang#99262 (comment)
2 parents cdd4ff8 + baa2cf5 commit 2d26783

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

library/std/src/io/error.rs

+31-5
Original file line numberDiff line numberDiff line change
@@ -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
}

library/std/src/io/error/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl error::Error for E {}
157157
fn test_std_io_error_downcast() {
158158
// Case 1: custom error, downcast succeeds
159159
let io_error = Error::new(ErrorKind::Other, Bojji(true));
160-
let e: Box<Bojji> = io_error.downcast().unwrap();
160+
let e: Bojji = io_error.downcast().unwrap();
161161
assert!(e.0);
162162

163163
// Case 2: custom error, downcast fails
@@ -166,7 +166,7 @@ fn test_std_io_error_downcast() {
166166

167167
// ensures that the custom error is intact
168168
assert_eq!(ErrorKind::Other, io_error.kind());
169-
let e: Box<Bojji> = io_error.downcast().unwrap();
169+
let e: Bojji = io_error.downcast().unwrap();
170170
assert!(e.0);
171171

172172
// Case 3: os error

0 commit comments

Comments
 (0)