rust-num / num-rational

Generic Rational numbers for Rust
Apache License 2.0
144 stars 51 forks source link

Symmetry for `Ratio::new_raw` #115

Closed zbrachinara closed 6 months ago

zbrachinara commented 1 year ago

A request to create a function (for now I'll call it Ratio::into_raw) which acts like this:

fn into_raw(self) -> (T, T) {
    (self.numer, self.denom)
}

This would be useful as a way to circumvent unnecessary cloning while maintaining the "weak invariants" of Ratio (mentioned in this issue). It would also be useful for "manually" implementing more efficient primitive operations on BigRational from outside of num-rational like so:

fn div_by_u32(rational: BigRational, primitive: u32) -> Option<BigRational> {
    (primitive != 0).then(|| {
        let (numer, mut denom) = rational.into_raw();
        denom *= primitive;
        BigRational::new_raw(numer, denom)
    })
}

This could solve issues that #38 tries to solve without making as much of a sweeping change, which hopefully will help it to be accepted more quickly.

zbrachinara commented 1 year ago

I misclicked the "create issue" button, so apologies for any errors in the code or being too terse

cuviper commented 8 months ago

Sorry I missed this -- I think into_raw sounds like a fine addition.