miette icon indicating copy to clipboard operation
miette copied to clipboard

Implement `Diagnostic` for `Infallible`

Open caass opened this issue 6 months ago • 1 comments

I've been working with some code that looks like this:

trait DiagnosticTryFrom<Input>: Sized {
  type Error: Diagnostic;

  fn try_from_diagnostic(input: Input) -> Result<Self, Self::Error>;
}

And so logically it makes sense to create a blanket implementation on top of TryInto:

impl <I, O> DiagnosticTryFrom<I> for O where O: TryFrom<I>, O::Error: Diagnostic {
  type Error = O::Error;

  fn try_from_diagnostic(input: I) -> Result<Self, Self::Error> {
    O::try_from(input)
  }
}

Which works very well for my own TryFrom implementations -- but not so well when there's a From implementation available. Every From implementation gets a blanket implementation of TryFrom with an error type of Infallible, so I'm not able to implement Diagnostic for that error type due to the orphan rule.

This PR implements Diagnostic for Infallible, so any API relying on TryFrom<T, Error: Diagnostic> can also use From<T> implementations.

caass avatar Aug 17 '24 18:08 caass