What to do if rust module function is private - rust

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

Related

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.

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

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.

UseDeclaration cannot find struct in the crate root

I start with the cargo new tst. Then in the src/lib.rs I have:
pub struct Config {}
And src/main.rs looks like the following:
use crate::Config;
fn main() {}
This however does not compile:
> cargo run
Compiling tst v0.1.0 (/home/*/rust/book/tst)
error[E0432]: unresolved import `crate::Config`
--> src/main.rs:1:5
|
1 | use crate::Config;
| ^^^^^^^^^^^^^ no `Config` in the root
For more information about this error, try `rustc --explain E0432`.
error: could not compile `tst` due to previous error
But if I replace crate:: with the name of the crate like so:
use tst::Config;
fn main() {}
Then it just work:
> cargo run
Compiling tst v0.1.0 (/home/*/rust/book/tst)
warning: unused import: `tst::Config`
--> src/main.rs:1:5
|
1 | use tst::Config;
| ^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
warning: `tst` (bin "tst") generated 1 warning
Finished dev [unoptimized + debuginfo] target(s) in 0.25s
Running `target/debug/tst`
The output for rustc --explain E0432 has the following quote, which if I understand it correctly means, that I can either use the name of a crate or simply crate:::
In Rust 2018, paths in use statements are relative to the current module
unless they begin with the name of a crate or a literal crate::, in which
case they start from the crate root. As in Rust 2015 code, the self:: and
super:: prefixes refer to the current and parent modules respectively.
Am I doing something wrong in here? Is there a way to use code from lib.rs without hardcoding the name of the crate?
> rustc --version
rustc 1.56.1 (Arch Linux rust 1:1.56.1-3)
> cat Cargo.toml
[package]
name = "tst"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
There is no way to refer to “the library crate in my package”; you must refer to it by its name. The Rust compiler doesn't (currently) know anything about other crates that happen to be in the same Cargo package; it just compiles one crate at a time.
In order for this to change, you would need to write a Rust RFC, which would need to include a description of why it would be valuable to have this feature — probably a stronger argument than “it would be less hardcoded”, since main depending on the library is almost certainly going to use library-specific names anyway, so the name of the library itself is a minor issue (unless, I suppose, you're creating many packages from a common template).

Compile error when running cargo bench (criterion/serde)

I added following lines to the cargo.toml of my project in order to benchmark my code:
[dev-dependencies]
criterion = "0.3"
[[bench]]
name = "samples"
harness = false
After running cargo bench, I get a lot of errors similar to the following:
Compiling criterion v0.3.4
error[E0603]: module `export` is private
--> C:\Development\Rust\cargo\registry\src\github.com-1ecc6299db9ec823\criterion-0.3.4\src\connection.rs:201:17
|
201 | #[derive(Debug, Deserialize)]
| ^^^^^^^^^^^ private module
|
note: the module `export` is defined here
--> C:\Development\Rust\cargo\registry\src\github.com-1ecc6299db9ec823\serde-1.0.123\src\lib.rs:275:5
|
275 | use self::__private as export;
The error message looks to me that there is a problem between serde and criterion. But I did not find this error message in either project issues, so there may be a hidden reason in my workspace.
Some additional information:
the project is compiled using the nightly toolchain
there is just one explicit dependency (proc macros) in the cargo.toml which transitively references syn, quote and proc-macro2
The serde version and serde_derive version in your dependency graph are mismatched. You need to use cargo update to bring them in sync. The two must have the identical version number always.

Unable to find symbols from extern crates included with `use`

I'm trying to use some Rust libraries from crates on Github. This is the first time I've tried to do this. The code, lifted from an "html" library example, begins like this:
mod interactive_test {
extern crate http;
extern crate url;
use std::os;
use std::str;
use url::Url;
use http::client::RequestWriter;
use http::method::Get;
use http::headers::HeaderEnum;
// ...
}
fn main() {}
Errors look like this:
error[E0432]: unresolved import `url::Url`
--> src/main.rs:7:9
|
7 | use url::Url;
| ^^^^^^^^ Did you mean `self::url`?
error[E0432]: unresolved import `http::client::RequestWriter`
--> src/main.rs:9:9
|
9 | use http::client::RequestWriter;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Did you mean `interactive_test::http`?
error[E0432]: unresolved import `http::method::Get`
--> src/main.rs:10:9
|
10 | use http::method::Get;
| ^^^^^^^^^^^^^^^^^ Did you mean `self::http::method`?
error[E0432]: unresolved import `http::headers::HeaderEnum`
--> src/main.rs:11:9
|
11 | use http::headers::HeaderEnum;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ Did you mean `interactive_test::http`?
The Cargo.toml file contains
[dependencies.http]
http = "https://github.com/chris-morgan/rust-http"
[dependencies.url]
url = "0.2.7"
and the HTTP and URL packages were found and fetched by cargo build earlier.
The extern crate http and extern crate url lines do not generate errors; the crates are being found by the compiler, but those crates don't seem to contain the expected symbols. If I add `extern crate foo", I get an error, so that is checked.
This is probably some problem with how Rust or Cargo search for libraries. Rust is installed in ~/local, not as root, done by setting the --prefix parameter during installation. That may have broken something, although Cargo should handle that. Basic stuff like "hello_world" works fine; bringing in external libraries does not.
I notice that cargo update doesn't cause a re-fetch of the http and url crates from Github. The documentation indicates that it should.
Versions:
Ubuntu 14.04 LTS.
rustc 0.13.0-nightly (96a3c7c6a 2014-12-23 22:21:10 +0000)
cargo 0.0.1-pre-nightly (e11c317 2014-12-21 20:43:45 +0000)
The compiler gave you the answer you need.
Your extern crate statements are inside a module, and use statements require absolute paths. That is, when you say use url::Url; inside the interactive_test module, what you are actually saying is "use url::Url which is defined in the root module", which it isn't.
What you need to do is prefix the path with self:: to tell it to look in the current module. You can also use super:: to access the parent module (if that ever comes up).
Personally, I get around this by putting all my extern crate statements in the root module, which also serves as a kind of program-wide list of external crates being used.

Resources