Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the trait std::convert::From<std::convert::Infallible> is not implemented for MyOwnError #62

Closed
bestouff opened this issue Feb 12, 2020 · 3 comments

Comments

@bestouff
Copy link

bestouff commented Feb 12, 2020

I'm generating code which can result in this error:

the trait `std::convert::From<std::convert::Infallible>` is not implemented for `MyOwnError`

This is because (of course) I have a function which can't fail (from_str() actually) but try to convert that to a Result<_, MyOwnError> via a question mark.
For now I have implemented this workaround manually:

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

but I'd prefer if it could be done automagically.

@dtolnay
Copy link
Owner

dtolnay commented Feb 24, 2020

I would prefer not to build anything for this into thiserror. That error will be solved by adding impl<T> From<!> to T in the standard library (rust-lang/rust#64715). Until then, the handwritten impl is fine or you could look for a From derive macro which could generate it.

@dtolnay dtolnay closed this as completed Feb 24, 2020
@leighmcculloch
Copy link

It appears impl<T> From<!> to T has been marked as permanent unstable just recently: rust-lang/rust#64715 (comment). Could this issue be reconsidered given that?

@evbo
Copy link

evbo commented Dec 1, 2022

I too am hitting this when I implement From and try to use the blanket implementation of TryFrom

I'm not so sure my function "can't fail" as I am using try_into() to anticipate when it may fail. But in certain contexts it shouldn't fail, and so I also need into()

What is the proposed workaround doing exactly? I am so confused and hate just copying and pasting this. Can anyone clarify?

ccouzens added a commit to ccouzens/leptonica-plumbing that referenced this issue Feb 26, 2023
As documented here
dtolnay/thiserror#62

Sometimes I'd get a build failure
For example here
https://github.com/antimatter15/tesseract-rs/actions/runs/4275328902/jobs/7442596787

```
error[E0277]: `?` couldn't convert the error to `PixReadMemError`
  --> /home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/leptonica-plumbing-1.0.0/src/pix.rs:68:73
   |
68 |         let ptr = unsafe { pixReadMem(img.as_ptr(), img.len().try_into()?) };
   |                                                                         ^ the trait `From<Infallible>` is not implemented for `PixReadMemError`
   |
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = help: the trait `From<TryFromIntError>` is implemented for `PixReadMemError`
   = note: required for `Result<RefCountedExclusive<pix::Pix>, PixReadMemError>` to implement `FromResidual<Result<Infallible, Infallible>>`
```

It seems hit and miss to if it's needed or not, which is why I didn't
immedaitely pick up on it.

https://tpgit.github.io/Leptonica/leptprotos_8h.html#a027a927dc3438192e3bdae8c219d7f6a

`pixReadMem` has the parameter defined as `size_t`, so it should be
automatically compatible with `usize`.
Repository owner locked and limited conversation to collaborators May 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants