Refactor and improve Delay#3019
Conversation
It's not entirely clear if we need the flexibility of a |
|
The only decoder that still uses Of course, if we relax the constraints a little and allow approximations of the delay fraction, then we don't even really need predefined time scales. A (On that note: |
|
Expanding on the alternatives for time-scale:
Also for all intents and purposes you can get quite good approximations with a microsecond scale. It would not matter to me if there were multiple ways to construct |
|
Then should we just use Or we take the plunge and use |
Not without doing the "as long as our interface for retrieving information from it is reasonably precise about roundtrips and precision" part, no. Also Lindy's law still applies, I don't think we need to rework our internals (rounding in a getter though, fine for me). But embracing different time-scale types as separate, possible constructors for |
What's your "reasonably precise" for roundtrips? Because ns seems like it'd be enough to me. Also, could you please explain your idea for different type-scale types a bit more? I don't have an image of what you're trying to suggest yet. |
|
Not precise roundtrips, but for one reasonable implementation a good enough explanation of what will roundtrip; and how. For With different time-scale types I mean making a public API to enable that promise use-case by use-case. So, bikeshedding names besides, we may have a constructor taking I don't know if it's a good idea. If explaining it here was already hard, it may not be an intuitive API. |
There was a problem hiding this comment.
Since whatever idea was floating in my head aligns with making Duration more private in relation to decoders/ encoders, LGTM.
Derive Ratio: PartialEq + Eq. Each ratio has a unique representation, so we don't need to go via cmp.
Ah yes, we eagerly reduce now, right.
|
One last thing. If we want the internal representation of Same for Also, I think I get the idea of your different time scales now. But as you said, I'm not sure if it's going to be worth the complexity. A u32/u32 fraction can represent all delays exactly (except probably |
Changes:
Ratiois now private to theanimation.rsmodule. Decoders and Co. now only use the public API ofDelay.NonZeroU32for the denominator. This should allow the compiler to remove a few div-by-zero panic paths and creates a niche.Delay::ZEROto represent a delay of 0 ms.Delay::from_millis(u32) -> SelfandDelay::as_millis(self) -> u32. These mirror the functions onDurationfor an easy-to-use API. They are also very efficient implementation-wise.From<Delay> for Duration.Ratio: PartialEq + Eq. Each ratio has a unique representation, so we don't need to go viacmp.Delay::from_numer_denom_ms. Addresses M21 from Claude Code Review Report #2954.I basically implemented what I think
Delayshould look like. Note that I didn't introduce any breaking changes (yet). I still don't likeDelay::from_numer_denom_ms, but I'm also not entirely sure what should replace it.Tell me what you think of these changes. If you like some but not others, I can revert some.