-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Support multiple blanket impls with differing bounds for traits #442
Comments
You can probably achieve what you want with "negative bounds": // All these implementations are valid (i.e. they don't conflict)
impl<T> Trait for T where T: A + B {} // T implements A and B
impl<T> Trait for T where T: A + !B {} // T implements A, but it doesn't implement B
impl<T> Trait for T where T: !A + B {} // the reverse case
impl<T> Trait for T where T: !A + !B {} // T implements neither A nor B There are some parts of the stdlib that could benefit a lot from it. [1, 2] So, we'll likely get this feature at some point in the future. [1] rust-lang/rust#17884 (comment) |
Oh man I want those. The functional completeness that would bring (assuming one is willing to come up with a new trait + blanket impl for every parenthetical) sounds flippin' great. Although, wouldn't this still require deferring checks (in the absence of brute-force checking for non-overlapping coverage of the trait bounds)? |
I'm running into more and more of these problems lately with my error handling experiments. Huge +1 on negative trait bounds. |
I've also wanted this (negative bounds) |
This issue is subsumed by the more general #1053, so I'm going to close it. |
This currently fails to compile:
with error:
Rust Playpen Link
Naturally, this error makes sense. A type can
impl
both traitsA
andB
. I was hoping that such a check could be deferred, though, thus resolving these 'conflicts' later when something actually asking forTrait
pops up. Ex. a type thatimpl
s onlyA
would get the blanketimpl
forA
, and if multipleimpl
s apply (e.g. the typeimpl
s bothA
andB
) then the compiler errors out.Some ideas
It was suggested on IRC that one may allow both blanket
impl
s to apply by using UFCS path syntax to disambiguate trait bounds and narrow down the possibly applicableimpl
s to a single one. Ex. a type thatimpl
s bothA
andB
could get both blanketimpl
s but be disambiguated when calling via<Type as A>::asdf()
.But really I'd be totally cool with only one blanket
impl
applying to any one type at any time.The text was updated successfully, but these errors were encountered: