error-chain icon indicating copy to clipboard operation
error-chain copied to clipboard

impl From<std::convert::Infallible> for Error

Open CraZySacX opened this issue 8 years ago • 5 comments
trafficstars

https://github.com/rust-lang/rust/pull/44174 landed in rust nightly and now I have to add

impl From<Infallible> for Error {
    fn from(infallible: Infallible) -> Self {
        match infallible {}
    }
}

to my error chain Error. Can/Should this impl be included in the library?

CraZySacX avatar Oct 01 '17 14:10 CraZySacX

I'm not sure I understand why this impl is needed?

Yamakaky avatar Oct 01 '17 15:10 Yamakaky

I'm not really sure either, but it was triggered from the following:

let len: isize = isize::try_from(self.conn.num_app_context)?;

where I was converting from a u32 in a FFI binding to isize.

CraZySacX avatar Oct 01 '17 17:10 CraZySacX

Here is the error if I comment out the impl.

error[E0277]: the trait bound `error::Error: std::convert::From<std::convert::Infallible>` is not satisfied
   --> src/context/params.rs:312:26
    |
312 |         let len: isize = isize::try_from(self.conn.num_app_context)?;
    |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<std::convert::Infallible>` is not implemented for `error::Error`
    |
    = help: the following implementations were found:
              <error::Error as std::convert::From<std::io::Error>>
              <error::Error as std::convert::From<std::ffi::NulError>>
              <error::Error as std::convert::From<std::env::VarError>>
              <error::Error as std::convert::From<std::num::TryFromIntError>>
            and 3 others
    = note: required by `std::convert::From::from`

CraZySacX avatar Oct 01 '17 17:10 CraZySacX

Oh, I see. Then yes, it would be good to add it to error-chain, maybe behind a feature until TryFrom comes to stable. I think this would be better:

impl From<Infallible> for Error {
    fn from(_: Infallible) -> Self {
        unreachable!()
    }
}

Yamakaky avatar Oct 01 '17 20:10 Yamakaky

Ah I see what this was all about now. Looks like right now we'd still need to convert from the infallible error type even if it was !, but maybe that will change in the future given the never type is a special snowflake.

KodrAus avatar Oct 04 '17 08:10 KodrAus