Add trait implementations for Rational and SRational#553
Conversation
|
Thanks for the PR. |
…plement `Add` using `impl_ops`
Sure, I just did so, and in the process realized that I only implemented It gets a bit verbose implementing In fact, I think I see some further opportunities for reducing code duplication for the |
|
As I stumbled myself over the lack of some Rational/SRational features, I agree that the num-rational crate is more straightforward. Do you want to take the opportunity to switch to num crate? But this whole shitty code needs some refactoring 😞 |
|
Sure, I'll take another stab at it! I did try to simply substitute but the issue I ran into is all the |
|
OK, so the major issues are:
|
The signature is something like: pub fn new<T>(levels: &[T], width: usize, height: usize, cpp: usize) -> Self
where
T: Copy + Into<Rational>,Afaik there are traits in num-traits to write it like
For now, I would use unwrap_or() and return NaN in such cases. |
Okay, but the (not exactly sure why it's a So unless I'm missing something, it actually needs
That sounds reasonable. |
This PR adds implementations for:
as_f32forSRational(it was already there forRational)AddandSumforRationalandSRationalI also considered simply using
Ratiofrom thenum-rationalcrate instead, as that crate already provides these implementations for these operations (and much more) out-of-the-box. That still might be a better long-term solution (preventing re-inventing the wheel), but that created some other conflicts with the rest of dnglab/rawler code usingRational/SRationalthat turned out less trivial to solve than I had hoped ;)