-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
impl PartialEq for comparing primitive integer {u,i}N with NonZero{U,I}N #81181
Comments
Do you have an use case? |
Yes, I came here after being surprised by this in my own project. I have an enum where I use enum PlayerId {
Spectator,
Player(NonZeroU8),
} This is useful for correctness (because I don't consider However, it is quite annoying to use in practice, because I cannot compare it with arbitrary I have to suffer the endless papercuts with type conversions (having to type I am sure many people have other similar applications where using a NonZero type makes sense. It seems silly to not support I mean, it's a papercut, not a major blocker, but it's silly that I have to deal with it. It makes using these standard wrapper types a hassle and unergonomic. |
There's also the option of using id.get() |
Implementations of That being said, how about you add these abstractions on your end directly? |
Thanks for the suggestion. You are right that there are things I can do on my end to improve the experience. I guess adding such "conveniences" to the language is more controversial than I imagined. |
I went to look at the docs to find examples, and found that there are already some such implementations:
That considered, I don't see why comparing |
Having more struct Foo;
impl PartialEq<Foo> for u32 {
fn eq(&self, _: &Foo) -> bool {
false
}
}
fn main() {
let x = 42_u32;
let y = 0u8;
assert!(x != y.into());
}
I ran into this scenario myself -- rust-num/num-bigint#150. |
Would it be possible to run crater with this change to get an idea on how much breakage this would cause? |
I added an implementation (#81266) in case you to try things out. Looking at the CI, it seems things are already falling appart:
|
I'm going to close this issue since adding the impl would cause a lot of inferrence issues. |
It should be possible to directly compare primitive integers with their NonZero equivalents:
Currently, such code produces a type mismatch compiler error due to
PartialEq
not being implemented for these types, to allow it.Such a comparison seems trivial. It should be non-controversial to add support for it?
The text was updated successfully, but these errors were encountered: