How to use the raft-rs crate and build with cargo? - rust

I try to build the single_mem_node example, but as a standalone example and use the raft-rs crate as a library.
But unfortunately, I get this build error, when building using cargo build
error[E0599]: no method named `is_empty` found for reference `&raft_proto::protos::eraftpb::Snapshot` in the current scope
--> src/main.rs:123:26
|
123 | if !ready.snapshot().is_empty() {
| ^^^^^^^^ method not found in `&raft_proto::protos::eraftpb::Snapshot`
I have declared these dependencies in my Cargo.toml
[dependencies]
protobuf = { version = "2", features = ["with-bytes"] }
raft = "0.6.0-alpha"
slog = "2.5.2"
slog-term = "2.6.0"
slog-async = "2.5.0"
The instructions on how to use the raft-rs crate is:
You can use raft with either rust-protobuf or Prost to encode/decode gRPC messages. We use rust-protobuf by default. To use Prost, build (or depend on) Raft using the prost-codec feature and without default features.
It certainly looks like I miss something around the protobuf... but what? and how do I find out about it?

It looks like the method was added in this commit, after version 0.6.0-alpha was released. GitHub shows the master branch by default, so in the future, try browsing at the commit that corresponds to the version you are using. In this case it is not tagged properly but I think this commit is the published 0.6.0-alpha version. In the example at that commit, the corresponding line is:
if !raft::is_empty_snap(ready.snapshot()) {
Alternatively, since it seems that 0.6.0-alpha was released in July of 2019, if you want the latest changes on master, you could change the dependency in Cargo.toml to read:
raft = { git = "https://github.com/tikv/raft-rs" }
In that case, cargo will fetch the latest commit and store its hash in Cargo.lock, and you can update to a newer commit with cargo update.

Related

How can I enable different features of bindgen dependening on the host env? [duplicate]

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?

Enabling determinism flag on wasmer

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.

Not being able to call build_vk_surface() on a WindowBuild object

I am following the vulkano tutorial to open a window and create a surface with vulkano-win.
Most of the tutorial is dated which I've been able to work around so far, however I have not been able to find a solution.
Currently, I get the following error when I call let window = WindowBuilder::new().build_vk_surface(&events_loop, instance.clone())
error[E0599]: no method named build_vk_surface found for struct winit::window::WindowBuilder in the current scope
I have checked the library and the version of vulkano_win and it seems to properly extend WindowBuilder I will post the dependencies in my Cargo.toml below.
[dependencies]
vulkano = "0.19"
vulkano-shaders = "0.18"
winit = "0.23"
vulkano-win = "0.19"
image = "0.23"
P.S. - There is a legacy instance of this happening on a much older (2 yr ago) version of vulkano_win here. I suspect this has been fixed since I have checked the dependencies for the vulkano_win library and they use the newer and restructured versions of winit there:
https://github.com/vulkano-rs/vulkano/issues/943
I have checked the library and the version of vulkano_win and it seems to properly extend WindowBuilder I will post the dependencies in my Cargo.toml below.
In short, you are correct. The issue is that the VkSurfaceBuild trait, is implemented for winit 0.22's WindowBuilder and not winit 0.23's WindowBuilder.
So to fix your issue, you need to update your Cargo.toml to use winit 0.22 instead of 0.23.
Example:
[dependencies]
vulkano = "0.19"
vulkano-shaders = "0.19"
vulkano-win = "0.19"
winit = "0.22"
Additionally, your confusion might come from browsing the repository.
In the repository both vulkano-win and the examples uses winit 0.23.
However, remember that the current state of the repository is not necessarily the same as what was released in 0.19.
On GitHub you can select tags, and view the commit that was the 0.19 release.
If you then look at vulkano-win and the examples, you'll see that they all use winit 0.22.
If you really want to use winit 0.23. Then you could depend directly on the repository. Like this:
[dependencies]
vulkano = { git = "https://github.com/vulkano-rs/vulkano" }
vulkano-win = { git = "https://github.com/vulkano-rs/vulkano" }
vulkano-shaders = { git = "https://github.com/vulkano-rs/vulkano" }
winit = "0.23"
However, one breaking change to the repository, could potentially ruin your build. So use with caution.
How were able to determine how that trait was implemented over the 0.22 WindowBuilder but not the 0.23 WindowBuilder?
Unsure if there's a fancy way of doing it, but an easy way is to just go to VkSurfaceBuild on docs.rs, then at "Implementations on Foreign Types" if you click on WindowBuilder, then it redirects to winit 0.22.2 docs.
Alternatively, you can also check your Cargo.lock. If you attempt to use winit 0.23, then your Cargo.lock would contain two winit versions:
[[package]]
name = "winit"
version = "0.22.2"
...
[[package]]
name = "winit"
version = "0.23.0"
...
If you then peek at vulkano-win then you can see it uses winit 0.22.2:
[[package]]
name = "vulkano-win"
version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b9a02addddf5532396d11dbb822f77d87ca17a00c918e4c8a0a125d6c207e2b"
dependencies = [
"cocoa 0.20.2",
"metal",
"objc",
"vulkano",
"winit 0.22.2",
]
If there's no duplicate versions, it would just say "winit" like the others.

how do i say that a feature is only available on a given platform

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 {
//...
}

How do you enable a Rust "crate feature"?

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.

Resources