I’ve got a trait with one method and for convenience I want to implement it for any closure. The trait looks somewhat like this:
pub trait Foo {
fn run(&mut self) -> Result<(), Error>;
}
With the generic FnMut
impl looking like this:
impl<F> Foo for F
where F: FnMut() -> Result<(), Error>
{
fn run(&mut self) -> Result<(), Error> {
self()
}
}
However there are some times when you statically know your F
will never fail, so I thought I’d also create implement the trait for functions which don’t return anything.
impl<F> Foo for F
where F: FnMut() // <- notice the different return type
{
fn run(&mut self) -> Result<(), Error> {
self()
}
}
But then when you actually try to compile this you get a “conflicting implementation” error. Does anyone know why we get conflicting implementation errors when the two F
types have different signatures?
Seeing as this use case is quite intuitive and should work,would this be a bug in the Fn*
traits (which are still experimental), how the compiler desugars the parenthesis notation for a Fn*
trait, or maybe even coherence?