-
Notifications
You must be signed in to change notification settings - Fork 61
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
Implement PartialEq/Eq for all colorspaces, Alpha, PreAlpha, and LabHue/RgbHue #211
Conversation
I'm not sure if I missed any wrappers. I don't think we can easily implement |
Thanks for picking this up as well! I think it's hitting some legacy parts of the library, so there are some things I would have done different today with all the hindsight I have accumulated. Particularly different trait requirements and to not derive traits that apply themselves to all type parameters. 🤔 My intention with implementing Also, in hindsight once again, I'm not sure if there's a good reason to require hues to implement I think the only other wrapper is |
Implement Eq for: - Alpha - PreAlpha - Luma - Rgb - Hsl - Hsv - LabHue/RgbHue - Hwb - Lab - Lch - Xyz - Yxy
I added I didn't even try to derive Eq at first but I believe that's what you meant. Is this what you meant by We don't really need to require this meaning the handwritten impls? And yes, the traits are a bit broad it seems. I agree that hues probably shouldn't need to be Float. |
Sorry, I wasn't clear regarding the deriving. I meant the opposite, that deriving adds unnecessary requirements in some cases, like requiring white points, standards and spaces to also implement the traits. I would prefer a hand written |
This seems like it might be a bigger change than I anticipated. If I understand correctly, the struct definitions would all need to have their trait bounds removed? Currently, it seems that because the struct definitions contain trait bounds, any impl of Eq/PartialEq would need the bounds on the white points/spaces/standards even if handwritten. Lab would change as such: - pub struct Lab<Wp = D65, T = f32>
- where
- T: FloatComponent,
- Wp: WhitePoint,
- {
+ pub struct Lab<Wp = D65, T = f32> { Then handwrite the impls like this: impl<Wp, T: PartialEq> PartialEq for Lab<Wp, T> {
fn eq(&self, other: &Self) -> bool {
self.l == other.l && self.a == other.a && self.b == other.b
}
}
impl<Wp, T: Eq> Eq for Lab<Wp, T> {} Does this seem like the correct approach? |
No, I'm just talking about the traits in the impl<Wp, T> PartialEq for Lab<Wp, T>
where
T: FloatComponent + PartialEq,
Wp: WhitePoint,
{
fn eq(&self, other: &Self) -> bool {
self.l == other.l && self.a == other.a && self.b == other.b
}
}
impl<Wp, T> Eq for Lab<Wp, T>
where
T: FloatComponent + Eq,
Wp: WhitePoint,
{} So, yeah, not a tiny change, but a good quality of life improvement. |
Add Component bound to T for AbsDiffEq, RelativeEq, and UlpsEq in Alpha
Ah, that works. Thanks for the guidance. I had to add a a For adding trait bounds to struct types, it seems to be discouraged except in special cases. Since the bounds exist on the impl blocks, they don't have to be on the generic parameters in the struct definition in most cases. That's not related to this topic though. |
palette/src/alpha/alpha.rs
Outdated
@@ -44,6 +44,23 @@ impl<C, T: Component> Alpha<C, T> { | |||
} | |||
} | |||
|
|||
impl<C, T> PartialEq for Alpha<C, T> | |||
where | |||
T: Component + PartialEq, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I bet the Component
here is what makes it require them for the other comparison traits. It shouldn't be necessary here and for Eq
. 🙂
Very nice! You are right about the trait bounds on structs. They can probably be removed in most cases. That may even open up some use of |
Alright, but let's merge. Thanks! bors r+ |
Build succeeded: |
Implement PartialEq/Eq for:
closes #206