Rename std::io::Error::try_downcast_inner to downcast

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-14 11:24:44 +10:00
parent 516da4c93f
commit 111253c519
No known key found for this signature in database
GPG Key ID: 591C0B03040416D6
2 changed files with 11 additions and 11 deletions

View File

@ -808,7 +808,7 @@ pub fn into_inner(self) -> Option<Box<dyn error::Error + Send + Sync>> {
/// # Examples
///
/// ```
/// #![feature(io_error_try_downcast_inner)]
/// #![feature(io_error_downcast)]
///
/// use std::fmt;
/// use std::io;
@ -829,14 +829,14 @@ pub fn into_inner(self) -> Option<Box<dyn error::Error + Send + Sync>> {
///
/// impl From<io::Error> for E {
/// fn from(err: io::Error) -> E {
/// err.try_downcast_inner::<E>()
/// err.downcast::<E>()
/// .map(|b| *b)
/// .unwrap_or_else(E::Io)
/// }
/// }
/// ```
#[unstable(feature = "io_error_try_downcast_inner", issue = "none")]
pub fn try_downcast_inner<E>(self) -> result::Result<Box<E>, Self>
#[unstable(feature = "io_error_downcast", issue = "none")]
pub fn downcast<E>(self) -> result::Result<Box<E>, Self>
where
E: error::Error + Send + Sync + 'static,
{

View File

@ -154,32 +154,32 @@ fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
impl error::Error for E {}
#[test]
fn test_try_downcast_inner() {
fn test_std_io_error_downcast() {
// Case 1: custom error, downcast succeeds
let io_error = Error::new(ErrorKind::Other, Bojji(true));
let e: Box<Bojji> = io_error.try_downcast_inner().unwrap();
let e: Box<Bojji> = io_error.downcast().unwrap();
assert!(e.0);
// Case 2: custom error, downcast fails
let io_error = Error::new(ErrorKind::Other, Bojji(true));
let io_error = io_error.try_downcast_inner::<E>().unwrap_err();
let io_error = io_error.downcast::<E>().unwrap_err();
// ensures that the custom error is intact
assert_eq!(ErrorKind::Other, io_error.kind());
let e: Box<Bojji> = io_error.try_downcast_inner().unwrap();
let e: Box<Bojji> = io_error.downcast().unwrap();
assert!(e.0);
// Case 3: os error
let errno = 20;
let io_error = Error::from_raw_os_error(errno);
let io_error = io_error.try_downcast_inner::<E>().unwrap_err();
let io_error = io_error.downcast::<E>().unwrap_err();
assert_eq!(errno, io_error.raw_os_error().unwrap());
// Case 4: simple
let kind = ErrorKind::OutOfMemory;
let io_error: Error = kind.into();
let io_error = io_error.try_downcast_inner::<E>().unwrap_err();
let io_error = io_error.downcast::<E>().unwrap_err();
assert_eq!(kind, io_error.kind());
@ -187,7 +187,7 @@ fn test_try_downcast_inner() {
const SIMPLE_MESSAGE: SimpleMessage =
SimpleMessage { kind: ErrorKind::Other, message: "simple message error test" };
let io_error = Error::from_static_message(&SIMPLE_MESSAGE);
let io_error = io_error.try_downcast_inner::<E>().unwrap_err();
let io_error = io_error.downcast::<E>().unwrap_err();
assert_eq!(SIMPLE_MESSAGE.kind, io_error.kind());
assert_eq!(SIMPLE_MESSAGE.message, &*format!("{io_error}"));