See this section of the re-rebalanced orphan rules RFC for an explanation of what coherence is, and why the orphan rules -- which limit which traits you can implement for which types -- exist. There's some more motivation on why the orphan rules are balanced how they are in this pre-1.0 RFC.
Now, for your particular situation: You have a blanket implementation -- an implementation for all T
, modulo some where
clauses -- and you have an implementation for a foreign type (bool
) that does not, today, meet those where
clauses.
For coherence to work, it has to be able to rely on the where
clauses not holding. This is often called "negative reasoning". But this is an issue in the context of balancing what changes are supposed to be non-breaking: like the error notes,
= note: upstream crates may add a new impl of trait `num_traits::Num` for type `bool` in future versions
= note: upstream crates may add a new impl of trait `num_traits::ConstZero` for type `bool` in future versions
= note: upstream crates may add a new impl of trait `num_traits::ConstOne` for type `bool` in future versions
The error preventing your bool
implementation ensures it's not a breaking change for num_traits
to add these implementations for its local trait in the future.
(If you could have your bool
implementation and num_traits
added those implementations, coherence would have to break your crate due to overlapping implementations.)
The limits on negative reasoning follows the orphan rules (as updated by RFC 2451). You're not allowed to impl Num for bool
for example, so you can't rely on the lack of such an implementation today.
If you use the macro and not a blanket implementation, num_traits
adding those implementations can't break your crate.
Specialization may some day allow overlapping implementations, so long as the intersection of all overlaps is exactly covered by a specializing implementation. At least, I believe that's how it works under the current unstable feature. However, that is a long way off (if ever), and the feature as it is today isn't a solution to your OP.