dnsl48 / fraction

[Rust] Lossless fractions and decimals; drop-in float replacement
Apache License 2.0
83 stars 25 forks source link

Made some Fraction functions const #67

Closed stijnfrishert closed 2 years ago

stijnfrishert commented 2 years ago

Hey,

In my personal work I'm using Fraction to represent musical rhythmic values, and I would very much like to create some const variables for commonly used values. I can't, because Fraction::new*() isn't const.

I've created a fork where I made new_raw*() const (and numer(), denom() and sign() while I was at it). Can't do so with new() because num::Ratio::new() isn't const.

If you're ok with it, I would love to merge at least the const raw variants in.

dnsl48 commented 2 years ago

hey @stijnfrishert thank you for the contribution! Yes, that definitely makes sense! IIRC most of that API surface was written before const fn became a stable language feture, so there are no actual reasons for them to be in runtime. Before releasing the patch, we'll go over the codebase and try to find other places that should be const.

stijnfrishert commented 2 years ago

Thanks! Much appreciated. 😄 Was hoping I could get the regular new()'s const too, but they're not const in num::Ratio as well (due to gcd and whatnot).

Looking forward to the patch 👍

dnsl48 commented 2 years ago

@stijnfrishert thanks again for the contribution! This patch is a part of the 0.12.0 release. We also made some other functions const throughout the codebase.

stijnfrishert commented 2 years ago

@dnsl48 Much appreciated :)