I am new to Rust and am looking for a way to enable the deterministic execution flag property on the cranelift complier for my Rust project described here through Cargo.toml.
https://github.com/wasmerio/wasmer/pull/709/files/6ca581279811aee9d26d77ed2b7372a6153fe3bf#
I would like the feature to stay enabled by default.
I tried enabling the feature on Cargo.toml like this way
wasmer-compiler-cranelift = { version = "2.0.0", default-features=false, features=["deterministic-execution"]}
But this doesn't seem to work as on doing cargo check I get an error which states that the compiler does not have this feature.
Related
I am currently making an embedded system.
For that I have 2 different panic handlers based on whether it is run normally or as a test.
#[cfg(test)]
for the test panic handler and
#[cfg(not(test))]
for the normal panic handler.
rust-analyzer says: code is inactive due to #[cfg] directives: test is enabled
and grays out the function.
Test is never explicitly set so I cant just change it and I don't want to disable the graying out inactive code across the workspace.
Is there a way to either disable rust-analyzer checking the test cfg, or to disable the gray out just for this function
I tried finding infos about the test flag but I couldn't find any, i'm using VS Code if it is important
In VSCode's workspaces settings, set the following:
"rust-analyzer.cargo.extraEnv": {
"RUSTFLAGS": "--cfg rust_analyzer"
}
This will enable the #[cfg(rust_analyzer)] for rust-analyzer metadata inspection.
Then, replace #[cfg(not(test))] with #[cfg(any(not(test), rust_analyzer))], and #[cfg(test)] with #[cfg(all(test, not(rust_analyzer)))]. This will disable and enable the functions for testing as usual, but when running in rust-analyzer, only the no-test function will be enabled.
Important: this will only work if you don't need to set special RUSTFLAGS for your workspace.
How do I enable crate features per-platform in a Cargo.toml configuration? I've tried two methods, neither of which work.
Method 1:
[target.'cfg(windows)'.dependencies.rusqlite]
version = "0.19.0"
features = ["bundled"]
[target.'cfg(unix)'.dependencies.rusqlite] # same behavior with cfg(not(windows))
version = "0.19.0"
Method 2:
[target.'cfg(windows)'.dependencies]
rusqlite = { version = "0.19.0", features = ["bundled"] }
[target.'cfg(unix)'.dependencies]
rusqlite = { version = "0.19.0" }
I'm trying to use the 'bundled' feature only on Windows platforms, but regardless of which way I try to configure cargo it always adds the 'bundled' feature when building on an Ubuntu system.
Is it possible to enable features only on one platform?
Is it possible to enable features only on one platform?
No, it is not possible, due to Cargo issue #1197.
See also:
How can I optionally pass rustc flags depending on a Cargo feature?
Let's assume I have the following feature defined in Cargo.toml:
[features]
my_feature = []
And the following code lives in src/lib.rs:
#[cfg(feature = "my_feature")]
fn f() { /* ... */ }
#[cfg(not(feature = "my_faeture"))] // <-- Mind the typo!
fn f() { /* ... */ }
How can I enforce, that the feature-strings are matched against the list of both explicitly defined and implicitly available features in Cargo.toml so that for instance typos could be avoided?
When RFC 3013, "Checking conditional compilation at compile time", is implemented, there will be warnings for a #[cfg] referring to a feature name that is not declared by Cargo, just as you're asking for. However, the implementation only just got started (Sep 28, 2021).
The means of operation described in the RFC is just as you suggested, ‘cargo would pass down the flags to rustc’.
It may be worth noting that this will not check all conditions appearing in the source text; as described in the RFC:
This lint will not be able to detect invalid #[cfg] tests that are within modules that are not compiled, presumably because an ancestor mod is disabled.
So, it will not confirm that all #[cfg(feature)] are valid on a single cargo check — you will need to test with your various features or combinations of features. But those are the same combinations that you would need anyway to check for compile errors in all of the regular source code that could be enabled; once you do that, the lint will assure you that you don't have any #[cfg(feature)] that are never enabled due to a typo.
I know how to say that a dependancy is only needed on windows but how do I say (as a crate writer) that a feature is only available on windows.
I tried (based on the depends way)
[target.'cfg(windows)'.features]
windbg = []
but this doesn't work.
cargo build says
warning: unused manifest key: target.cfg(windows).features
and a client app using the crate fails saying that the feature doesn't exist
Currently Cargo is not able to specify feature's target platform, but you can add target_os to your code as an extra attribute to let compiler know that your feature will only be available on the target you set.
Let's say you have defined your feature like below.
#[cfg(feature = "windbg")]
mod windbg {
//...
}
You'll need to replace it with:
#[cfg(all(target_os = "windows", feature = "windbg"))]
mod windbg {
//...
}
I'm trying to use rand::SmallRng. The documentation says
This PRNG is feature-gated: to use, you must enable the crate feature small_rng.
I've been searching and can't figure out how to enable "crate features". The phrase isn't even used anywhere in the Rust docs. This is the best I could come up with:
[features]
default = ["small_rng"]
But I get:
Feature default includes small_rng which is neither a dependency nor another feature
Are the docs wrong, or is there something I'm missing?
Specify the dependencies in Cargo.toml like so:
[dependencies]
rand = { version = "0.7.2", features = ["small_rng"] }
Alternatively:
[dependencies.rand]
version = "0.7.2"
features = ["small_rng"]
Both work.