Optional features

I am trying to make optional features for my binary crate, which include features from dependencies, this is what I have:

default = ["required"]
required = ["dep:rustdb", "dep:tokio"]
mt-tokio = ["tokio?/rt-multi-thread"]
unsafe-optim = ["rustdb?/unsafe-optim"]

tokio = { version = "1.13.0", features = ["macros","signal"], optional=true }
rustdb = { version = "5.2.0", optional=true }

This works ok, but the "required" feature is always needed, I have put it in default, but if someone compiles (or installs) with --no-default-features there will be errors.

It seems like maybe I am missing something. Is there a way to unconditionally include the "required" feature? ( I almost want to omit the two "optional=true" bits, but that isn't allowed ).

( Actual toml file is here: RustWeb2/Cargo.toml at a5c2b7523ab7ff66220a12cf9a4e49f74005f59f · georgebarwood/RustWeb2 · GitHub ]

Why not?

Well, normally you wouldn't put stuff that needs to be always included behind a feature gate.

I personally wouldn't be two concerned about users breaking your library with default-features = false though. That's something that can happen. In async-land you often have to enable either tokio or async-std features to change the runtime, one being enabled by default. If you disable the default features without adding a certain non-default feature (i.e. async-std if the hypothetical crate enables the tokio feature per default), building that library would fail. Happened to me before.

I get this error:
C:\Users\ano31\Rust\RustWeb2>cargo build
error: failed to parse manifest at C:\Users\ano31\Rust\RustWeb2\Cargo.toml

Caused by:
feature mt-tokio includes tokio?/rt-multi-thread with a ?, but tokio is not an optional dependency
A non-optional dependency of the same name is defined; consider removing the ? or changing the dependency to be optional

That error can be fixed by changing this line to not use the ? operator, which only works for optional dependencies:

- mt-tokio = ["tokio?/rt-multi-thread"]
+ mt-tokio = ["tokio/rt-multi-thread"]

Oh, I solved it. In the error message suggestions I needed to remove the question marks, not add optional = true!

What I need is simply this:

mt-tokio = ["tokio/rt-multi-thread"]
unsafe-optim = ["rustdb/unsafe-optim"]

tokio = { version = "1.13.0", features = ["macros","signal"]}
rustdb = { version = "5.2.0" }

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.