rust-random / rand

A Rust library for random number generation.
https://crates.io/crates/rand
Other
1.67k stars 431 forks source link

Make `libm` dependency optional if using `std_math` #1300

Closed coreylowman closed 1 year ago

coreylowman commented 1 year ago

Background

What is your motivation? I would like to reduce number of dependency crates I need to compile.

What type of application is this? I'm building a deep learning crate

Feature request

In my library, I have the following dependency for rand_distr:

rand_distr = { version = "0.4.3", default-features = false, features = ["std_math"] }

which uses std::math instead of libm.

However when I use cargo tree, I see the following for rand_distr:

├── num-traits v0.2.15
│   └── libm v0.2.6
│   [build-dependencies]
│   └── autocfg v1.1.0
└── rand_distr v0.4.3
    ├── num-traits v0.2.15 (*)
    └── rand v0.8.5 (*)

I would like to not have to use libm if I elect to use std_math.

Perhaps there can be a libm feature that is mutually exclusive with std_math?

dhardy commented 1 year ago

Cargo doesn't support mutually exclusive features, so I don't think it's possible to say only use feature num-traits/libm when not using feature std_math.

We could make libm opt-in, but: https://github.com/rust-random/rand/pull/1100/commits/382e3d4d4640c64f05372d534ccbef9e717de9b5

So, with the given motivation, this is probably a no.

coreylowman commented 1 year ago

You could use compile_error!() if both features are specified, but I see your point. 👍

dhardy commented 1 year ago

I'd prefer we don't resort to compile_error! for the overlapping case: it makes the diamond inheritance problem unsolvable (a bin depending on two libs each depending on rand_distr but with different feature flags). This is why Cargo doesn't support mutually exclusive features.

So, unfortunately you'll have to live with an unused dependency in your tree and the resultant increase in compile times (but, if libm is unused, this shouldn't affect binary size).