5

I am looking to add the possibility to add the possibility of using my crate without the standard library. Some of the core functionality does depend on floating-point functions, which in no-std mode would need to be provided by libm.

The usual way I've seen no-std setup is to have a feature called "std" that enables the standard library, but in this case I would want the feature to remove the libm dependency. I could call the "additional" feature "no-std", but that then leads to the issue that I have certain features that would be difficult to implement in no-std mode, so I'd want them to depend on std being enabled.

Is it possible for cargo to specify an optional dependency that is present only when a feature is not enabled?

  • 1
    You could use `std` as a default-feature and let people opt out from it by using `default-features = false`. Read more here https://doc.rust-lang.org/cargo/reference/features.html – hellow May 13 '20 at 07:53
  • @hellow - that doesn't help. It would still need to pull in libm only when std is disabled. –  May 13 '20 at 07:54
  • @antispinwards if you declare `libm` as an optional dependency and specify it as a dependency of the `std` feature it should work – Masklinn May 13 '20 at 08:05
  • @Masklinn - that's the opposite of what I need to do - I need `libm` only when `std` is NOT enabled. –  May 13 '20 at 08:08
  • 2
    Aaah sorry I misread the problem. Then I don't think it's possible and the only thing you can do is create two features (one for libm and one for std) and use conditional to trigger a compilation error if neither or both is enabled. [possibly related issue](https://github.com/rust-lang/cargo/issues/2980) – Masklinn May 13 '20 at 08:10
  • @antispinwards could you rephrase your question, so it actually reflects these comments? So essentialy copy this sentence into your question *"I need libm only when std is NOT enabled"* so the question itself is the real question. So noone needs to read the comments to get an idead of what you really want. – hellow May 13 '20 at 10:03
  • 2
    @hellow - I've already stated what I want in the question title, and the body of the question. From the question: "The usual way I've seen no-std setup is to have a feature called "std" that enables the standard library, but in this case I would want the feature to remove the libm dependency". If people read that part of the question rather than skimming over it then I wouldn't need to restate it in the comments. But here we are. –  May 13 '20 at 10:06

0 Answers0