linebender / piet

An abstraction for 2D graphics.
Apache License 2.0
1.24k stars 93 forks source link

impl Hash+Eq for GradientStop #450

Closed raphlinus closed 3 years ago

raphlinus commented 3 years ago

I'm implementing gradients in piet-gpu, and one of the things I'm running into is being able to determine whether a gradient ramp is new or already in the cache. There are a number of ways to deal with this, but the most direct is to impl Hash on GradientStop, so it can be used as a key in a hashmap.

This is messy, because a gradient stop contains a float, and also because it means we have to hash color, but I think it's worth it.

Opening an issue first in case people want to discuss.

cmyr commented 3 years ago

Actually implementing Eq is the tricky part here, we'd have to decide what that means? I find in general with this sort of thing what I tend to be interested in is a very strict form of equality, approximately equal to, "is this thing a clone or bitwise copy of that thing"? In which case defining Eq in terms of bitwise equality of underlying floats seems reasonable to me.

raphlinus commented 3 years ago

Yes, I was thinking bitwise. The actual thing I care about is "will generate an identical GPU-side representation" for which bitwise is appropriate.