cmpute / dashu

A library set of arbitrary precision numbers implemented in Rust.
Apache License 2.0
77 stars 9 forks source link

num_traits for dashu::Rational #47

Closed lskyum closed 11 months ago

lskyum commented 11 months ago

I'm trying to use dashu's Rational for some geometric calculations. So dashu::Rational should be a a scalar in a Vector struct, but here it would be helpfull if it implemented num::Zero, num::One ect.

When trying to add the implementation myself, the Rust compiler complains: 'only traits defined in the current crate can be implemented for types defined outside of the crate'

This is the code i'm trying to make work:

impl num::Zero for dashu::Rational {
    fn zero() -> Self {
        dashu::Rational::ZERO
    }

    fn is_zero(&self) -> bool {
        self.is_zero()
    }
}

Sorry if it's obvious, but I'm still new to Rust...

cmpute commented 11 months ago

num-traits is an optional dependency of dashu-ratio. If you enable num-traits in Cargo.toml, then the traits should be supported.

lskyum commented 11 months ago

@cmpute Oh, that's clever, it works fine!

I'm loving Rust more and more each day :-)

Thanks again

lskyum commented 11 months ago

For anyone that ends up here, just do this in Cargo.toml

[dependencies]
dashu = { version = "x.x.x", features = ["num-traits"] }