Specialization - Overlapping trait impls for FnOnce


#1

I have a trait Foo that has a single method run() -> Result<Self::Item, Self::Error> and I’d like to provide blanket implementations for all FnOnce() and FnOnce() -> Result<I, E>. I’d expect to be able to do this with specialization, but I’m getting an error saying the implementations are conflicting. You can see the example here.

Why are do the two impls conflict? Is this genuinely not covered under specialization or is this a part of specialization that has not yet been fully implemented?


#2

That’s because FnOnce() -> () and FnOnce() -> Result<I, E> are disjoint traits (they can’t both be implemented on the same type) because the “return type” is actually an associated type on the FnOnce trait. There is no overlap so one clearly can’t be “more general” than the other. Really, this shouldn’t require specialization. Unfortunately, rust isn’t currently smart enough to figure this out.


#3

Looks like https://github.com/rust-lang/rust/issues/20400.


#4

Ah, thanks! That clarifies it. Shame it doesn’t work today, though :frowning: