How do I compile the rust GTK example to the gtk crate? "#[doc(alias)] is experimental" - rust

I have been trying to learn the Rust language. I have managed to create some simple command line applications but now I am moving on to see if I can create an application that uses a graphical interface. I have looked into several GUI libraries including iced and the Qt API, but they have not worked out. I have most recently landed on GTK. I grabbed the example from the gtk crate documentation at
https://gtk-rs.org/gtk3-rs/stable/latest/docs/gtk/
There is no information on that page on what to put into the Cargo.toml file so I have been trying to figure it out. The answers I have found on the web so far have been no help at all. So I went to crates.io and grabbed the the latest version of the gtk crate, 0.14.3, and put that into the toml.
[package]
name = "gtkhello"
version = "0.1.0"
authors = ["brian"]
edition = "2018"
[dependencies]
gtk = "0.14.3"
Here is the example program, copied unaltered from the documentation.
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow};
fn main() {
let app = Application::builder()
.application_id("org.example.HelloWorld")
.build();
app.connect_activate(|app| {
// We create the main window.
let win = ApplicationWindow::builder()
.application(app)
.default_width(320)
.default_height(200)
.title("Hello, World!")
.build();
// Don't forget to make all widgets visible.
win.show_all();
});
app.run();
}
When I attempt to build this with cargo the compilation of the library fails. I do not know how to continue if the library itself will not compile. Here are the errors:
[brian#localhost gtkhello]$ cargo build
Compiling glib-macros v0.14.1
Compiling gdk-pixbuf-sys v0.14.0
error[E0658]: `#[doc(alias)]` is experimental
--> /home/brian/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-macros-0.14.1/src/clone.rs:202:1
|
202 | #[doc(alias = "get_full_ident")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #50146 <https://github.com/rust-lang/rust/issues/50146> for more information
error[E0658]: `#[doc(alias)]` is experimental
--> /home/brian/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-macros-0.14.1/src/clone.rs:248:1
|
248 | #[doc(alias = "get_keyword")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #50146 <https://github.com/rust-lang/rust/issues/50146> for more information
error[E0658]: `#[doc(alias)]` is experimental
--> /home/brian/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-macros-0.14.1/src/clone.rs:366:1
|
366 | #[doc(alias = "get_expr")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #50146 <https://github.com/rust-lang/rust/issues/50146> for more information
error[E0658]: `#[doc(alias)]` is experimental
--> /home/brian/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-macros-0.14.1/src/clone.rs:413:1
|
413 | #[doc(alias = "get_return_kind")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #50146 <https://github.com/rust-lang/rust/issues/50146> for more information
error[E0658]: `#[doc(alias)]` is experimental
--> /home/brian/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-macros-0.14.1/src/clone.rs:474:5
|
474 | #[doc(alias = "get_closure")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #50146 <https://github.com/rust-lang/rust/issues/50146> for more information
error[E0658]: `#[doc(alias)]` is experimental
--> /home/brian/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-macros-0.14.1/src/clone.rs:579:1
|
579 | #[doc(alias = "get_closure")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #50146 <https://github.com/rust-lang/rust/issues/50146> for more information
Compiling gdk-sys v0.14.0
error: aborting due to 6 previous errors
For more information about this error, try `rustc --explain E0658`.
error: could not compile `glib-macros`.
To learn more, run the command again with --verbose.
warning: build failed, waiting for other jobs to finish...
error: build failed
I went to the link that was suggested in the error messages for the issue and it ends with:
bors closed this in 57c5f40 on Sep 14, 2020
So I guess my question has several facets. What do I put into the Cargo.toml file to make this example work? How do I find that information in the future should I branch out from this simple example? How do I get around a required crate that will not compile? How do I tell cargo that experimental code is OK by me?
The biggest question is "What am I missing?"

The referenced feature was previously unstable, but stabilized in version 1.48.0. So, minimal supported Rust version for gtk-rs is at least 1.48.0.
In general, if you see some crate failing to compile with reference to "unstable features", but the corresponding tracking issue is already closed, this usually means that your compiler version is older then the minimal supported by the crate in question. If the tracking issue is open, i.e. the feature is not stabilized yet, the crate might be nightly-only - in this case you have to add the necessary toolchain with rustup install nightly and then either set is as global default via rustup default nightly, or set an override (the easiest way is with rustup override nightly) for current project only.

Related

What to do if rust module function is private

I wanted to use qoi in my rust project and tried this module, https://github.com/ChevyRay/qoi_rs
The problem is that, when I write this:
use qoi::Pixel;
The error :
error[E0432]: unresolved import `qoi::Pixel`
--> src/create_images.rs:4:5
|
4 | use qoi::Pixel;
| ^^^^^-----
| | |
| | help: a similar name exists in the module: `pixel`
| no `Pixel` in the root
For more information about this error, try `rustc --explain E0432`.
error: could not compile `Thing_in_rust` due to previous error
so I tried pixel :
use qoi::pixel;
And this error message pops up
error[E0603]: module `pixel` is private
--> src/create_images.rs:4:10
|
4 | use qoi::pixel;
| ^^^^^ private module
|
I am new to rust, but looking at the github code https://github.com/ChevyRay/qoi_rs/blob/main/src/pixel.rs gave me the impression that it is public.
cargo.toml :
[dependencies]
qoi = "0.4.0"
The crate you've linked to doesn't seem to be published on crates.io, although there's a multitude of qoi crates over there.
So your options are either:
use one of the crates which is published on crates.io
use a direct git dependency rather than a cargo one
a variant of the second one is to vendor the crate using a path dependency in case you need to update it

Problems installing Inari library

I am new to Rust, so this could be a stupid mistake, but I can't figure out how to fix it.
The problem
I tried to install Inari (a Rust implementation of interval arithmetic, https://crates.io/crates/inari). But when I try to compile the project, I get some errors:
Compiling inari v1.0.0
error: RUSTFLAGS='-Ctarget-cpu=haswell' or later is required. See https://doc.rust-lang.org/rustc/codegen-options/#target-cpu
--> /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/inari-1.0.0/src/simd/x86_64.rs:174:9
|
174 | ... compile_error!("RUSTFLAGS='-Ctarget-cpu=haswell' or later is required. See https://doc.rust-lang.org/rustc/codegen-options/#target-cpu...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0425]: cannot find function `add_ru` in this scope
--> /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/inari-1.0.0/src/arith.rs:25:21
|
25 | Self { rep: add_ru(x, y) }
| ^^^^^^ help: a function with a similar name exists: `add_rn`
|
...
The error says something about using Haswell, so I tried to add the following lines to Cargo.toml:
[build]
rustflags = ["-Ctarget-cpu=haswell"]
rustdocflags = ["-Ctarget-cpu=haswell"]
but it didn't change anything.
Steps to reproduce
Create a new project with cargo new project-name. Add inari as a dependancy cargo add inari. Run the project with cargo run.
Environment
cargo 1.62.1 (a748cf5a3 2022-06-08)
rustc 1.62.1 (e092d0b6b 2022-07-16)
Ubuntu 20.04.3 LTS (in WSL)
You need to put that in .cargo/config.toml, not in Cargo.toml. Alternatively, set the environment variable RUSTFLAGS before running Cargo.
See Configuration - build.rustflags - The Cargo Book.

Could not compile `lazy_static`

I'm having a hard time cross-compiling an embedded Rust project that worked before for a raspberry pi. I have all the needed deps in Cargo.toml but on doing:
$ cargo build --target thumbv7m-none-eabi
I get the following error.
error[E0463]: can't find crate for `std`
--> /home/.cargo/registry/src/github.com-1ecc6299db9ec823/lazy_static-1.4.0/src/inline_lazy.rs:9:1
|
9 | extern crate std;
| ^^^^^^^^^^^^^^^^^ can't find crate
|
= note: the `thumbv7m-none-eabi` target may not support the standard library
= help: consider building the standard library from source with `cargo build -Zbuild-std`
For more information about this error, try `rustc --explain E0463`.
error: could not compile `lazy_static` due to previous error
$ rustup show
installed targets for active toolchain
--------------------------------------
thumbv7m-none-eabi
x86_64-unknown-linux-gnu
active toolchain
----------------
nightly-x86_64-unknown-linux-gnu (default)
rustc 1.64.0-nightly (38b72154d 2022-07-11)
Compilation used to work previously without lazy_static as a dependency in cargo.toml,now I don't understand why this is happening.
By default lazy_static depends on the rust standard library, which as the compiler told you
may not be supported on the thumbv7m-none-eabi target
If you do not need the standard library in your project you can enable the no-std feature of lazy_static like this:
lazy_static = { version = "1.5.0", features = ["spin_no_std"] }
as described here.

Error adding Custom RPCs for custom pallet Substrate

I've been working with parity's contracts node (latest version) and the substrate template node (tag polkadot-v0.9.18), both present the same issue when compiling.
I have a very simple pallet that stores certain items. The main structure is the following:
#[pallet::storage]
#[pallet::getter(fn items)]
/// 'Mapping' Item ID -> Item Data
pub(crate) type Items<T: Config> = StorageMap<_, Twox64Concat, T::Hash, Item<T>>;
I was trying to add a simple RPC method following this guides https://core.tetcoin.org/recipes/custom-rpc.html#rpc-to-call-a-runtime-api and https://core.tetcoin.org/recipes/runtime-api.html
I also checked some projects that already have custom RPC calls implementations, like de subsocial node and I have pretty much the same structure and dependencies.
My rpc method does nothing but return a number 2 just to make sure it works, but it doesn't. This is what the pallets directory looks like:
pallets directory
When I try to compile, the following error shows
error: the wasm32-unknown-unknown target is not supported by default, you may need to
enable the "js" feature. For more information see:
https://docs.rs/getrandom/#webassembly-support
I don't even use that module, but I've read that it is used somewhere as an indirect dependency.
I'm compiling my project with the following command
cargo build --release
Checking the documentation regarding the 'getrandom' crate issue, I added the following dependency in the Cargo.toml (I tried adding it in every Cargo.toml within the project, individually, by pairs, ...)
getrandom = { version = "0.2", features = ["js"] }
Then another error shows up:
error: failed to run custom build command for secp256k1-sys v0.4.1
Which again, doesn't make any sense to me.
The project itself has nothing but the node template base and a new pallet that implements a create and transfer function. Without the RPC implementation, it works perfectly using the Polkadot App, but as soon as I include the custom rpc, it just doesn't compile.
This is my rust configuration (rustup show)
installed toolchains
--------------------
stable-x86_64-apple-darwin (default)
nightly-2021-11-04-x86_64-apple-darwin
nightly-x86_64-apple-darwin
active toolchain
----------------
stable-x86_64-apple-darwin (default)
rustc 1.59.0 (9d1b2106e 2022-02-23)
I haven't found anyone who is dealing with this kind of issue, and I don't know where the problem might be.
This is the first issue logs:
error: the wasm32-unknown-unknown target is not supported by default, you may need to enable the "js" feature. For more information see: https://docs.rs/getrandom/#webassembly-support
--> /home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.5/src/lib.rs:229:9
|
229 | / compile_error!("the wasm32-unknown-unknown target is not supported by \
230 | | default, you may need to enable the \"js\" feature. \
231 | | For more information see: \
232 | | https://docs.rs/getrandom/#webassembly-support");
| |________________________________________________________________________^
error[E0433]: failed to resolve: use of undeclared crate or module `imp`
--> /home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.5/src/lib.rs:256:5
|
256 | imp::getrandom_inner(dest)
| ^^^ use of undeclared crate or module `imp`
For more information about this error, try `rustc --explain E0433`.
error: could not compile `getrandom` due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: build failed
Current status (to reproduce error): https://github.com/andresvsm/substrate-pallet-rpc/tree/items-branch
Sometimes, you can get this error from a deep dependency of another dependency, e.g. when you really build for a wasm32-unknown-unknown target, and getrandom is linked but even not used. It can be fixed (worked around) with the following trick:
In Cargo.toml, add this line:
[dependencies]
getrandom = {version = "0.2", default-features = false, features = ["custom"]}
It tells the compiler to use a dummy implementation inside of getrandom.
Fixed for me when I added "default features = 'false'" into my Cargo.toml under the dependency in question.

Change nightly Rust version?

I tried to build rls by the command cargo +nightly build --release -Z unstable-options, but got the following errors:
error[E0599]: no method named `expect_none` found for enum `Option<Fingerprint>` in the current scope
--> /Users/cjw/.cargo/registry/src/github.com-1ecc6299db9ec823/rustc-ap-rustc_span-705.0.0/src/lib.rs:2003:48
|
2003 | cache[index].replace(sub_hash).expect_none("Cache slot was filled");
| ^^^^^^^^^^^ method not found in `Option<Fingerprint>`
After searching it, I found that expect_none is a nightly feature and seemingly has been removed.
So I think maybe I should change the rust compiler version to fix the compilation problem. If this is the correct solution, how could I do it? Can anyone provide some detail suggestions?
Using rustup you can manage different nightly versions. See The Edition Guide for more.
As Option::expect_none was removed on March 25th, we can get the nightly for March 24th in the following way:
rustup toolchain install nightly-2021-03-24 --force
Note: the --force option was used as the components rustfmt and clippy might be missing.
Switch to the newly downloaded toolchain:
rustup default nightly-2021-03-24
The following main.rs should now panic, as expected:
#![feature(option_expect_none)]
fn main() {
let value = Some(42);
value.expect_none("The answer");
}
If you're curious, you could try this with nightly-2021-03-26 and you'll find that it will give you the expected error, indicating it was indeed removed:
error[E0599]: no method named `expect_none` found for enum `Option<{integer}>` in the current scope
--> src/main.rs:5:11
|
5 | value.expect_none("Expected none!");
| ^^^^^^^^^^^ method not found in `Option<{integer}>`

Resources