Refactor From<io::Error> for BinstallError

Avoid one `expect`.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-21 14:10:27 +10:00
parent c5a2a89361
commit 2f38925ee4
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -234,18 +234,17 @@ impl Termination for BinstallError {
impl From<std::io::Error> for BinstallError { impl From<std::io::Error> for BinstallError {
fn from(err: std::io::Error) -> Self { fn from(err: std::io::Error) -> Self {
let is_inner_binstall_err = err if err.get_ref().is_some() {
.get_ref() let kind = err.kind();
.map(|e| e.is::<BinstallError>())
.unwrap_or_default();
if is_inner_binstall_err {
let inner = err let inner = err
.into_inner() .into_inner()
.expect("err.get_ref() returns Some, so err.into_inner() should als return Some"); .expect("err.get_ref() returns Some, so err.into_inner() should also return Some");
*inner
inner
.downcast() .downcast()
.expect("The inner err is tested to be BinstallError") .map(|b| *b)
.unwrap_or_else(|err| BinstallError::Io(std::io::Error::new(kind, err)))
} else { } else {
BinstallError::Io(err) BinstallError::Io(err)
} }