How use hex::encode in Substrate offchain worker? - rust

I'm calling hex::encode from offchain worker, but compilation fails with:
state.serialize_field("account_id", &hex::encode( self.account_id.encode().as_slice() ) )?;
| ^^^^^^ not found in `hex`
My Cargo.toml contains:
[features]
default = ['std']
std = [
...
'hex/std'
]
[dependencies]
hex = { version='0.4.3', default-features=false }
How to fix this?

Adding the "alloc" feature to Cargo.toml solved the problem:
hex = { version='0.4.3', default-features=false, features = ['alloc'] }

Related

enabling a feature when using libraries

I'm trying to use DefaultAsyncInterface of tunio which depends on tokio feature (here)
I have added tunio with async-tokio feature in Cargo.toml:
[dependencies]
env_logger = "0.10.0"
etherparse = "0.13.0"
futures = "0.3.25"
netconfig = "0.4.0"
tokio = { version = "1.24.1", features = ["full"] }
tunio = { version = "0.3.0", features = ["async-tokio"] }
and imported it:
use tunio::{DefaultDriver,DefaultAsyncInterface};
but when I try to build I get:
error[E0432]: unresolved import `tunio::DefaultAsyncInterface`
--> src/main.rs:7:27
|
7 | use tunio::{DefaultDriver,DefaultAsyncInterface};
| ^^^^^^^^^^^^^^^^^^^^^
| |
| no `DefaultAsyncInterface` in the root
| help: a similar name exists in the module: `DefaultInterface`
It seems the async-tokio feature is not used
What is the problem?

Only one of runetimes can be active error when only one runtime specified

What is going on here:
From Cargo.toml:
name = "sitegen"
version = "0.2.0"
edition = "2021"
[dependencies]
axum_database_sessions = { version = "5.0", features = [ "mysql-native"] }
sqlx = { version = "0.6", features = ["runtime-tokio-rustls", "mysql"] }
tokio = { version = "1.0", features = ["full"] }
Compile Error:
error: only one of ['runtime-actix-native-tls', 'runtime-async-std-native-tls', 'runtime-tokio-native-tls', 'runtime-actix-rustls', 'runtime-async-std-rustls', 'runtime-tokio-rustls'] can be enabled
 --> /home/dsrich/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-rt-0.6.2/src/lib.rs:23:1
  |
23 | / compile_error!(
24 | | "only one of ['runtime-actix-native-tls', 'runtime-async-std-native-tls', \
25 | | 'runtime-tokio-native-tls', 'runtime-actix-rustls', 'runtime-async-std-rustls', \
26 | | 'runtime-tokio-rustls'] can be enabled"
27 | | );
  | |_^
error: could not compile `sqlx-rt` due to previous error
I am using the latest rust x86-64 on linux, MySQL, and having rustls present in Cargo.toml or not makes no difference. Any suggestions? Just makes no sense to me...
Just trying to compile it to start using it with axum-sessions
The default features of axum_database_session is ["postgres-rustls"] therefore if you want to use *-native you have to disable default features:
axum_database_sessions = { version = "5.0", default-features = false, features = ["mysql-native"] }
but since you're explicitly including rustls in your dependency on sqlx you probably just want to use mysql-rustls instead, still without the default features because you're using MySQL instead of PostgreSQL:
axum_database_sessions = { version = "5.0", default-features = false, features = ["mysql-rustls"] }

rust-gpu how to use as a dependency?

I am trying to integrate rust-gpu into a project. The docs explain how to use it as a build dependency but the examples use it as a straight dependency, I have struggled with both but I would prefer to get the dependency version to work as it's more suited for my purposes.
I downloaded the rust-gpu git repo and compiled the ash example, which runs.
This is the toml I used for compilation:
[package]
name = "example-runner-ash"
version = "0.0.0"
authors = ["Embark <opensource#embark-studios.com>"]
edition = "2021"
license = "MIT OR Apache-2.0"
publish = false
# See rustc_codegen_spirv/Cargo.toml for details on these features
[features]
default = ["use-compiled-tools"]
use-installed-tools = ["spirv-builder/use-installed-tools"]
use-compiled-tools = ["spirv-builder/use-compiled-tools"]
[dependencies]
ash = "0.35"
ash-window = "0.9"
winit = "0.26"
structopt = "0.3.20"
cfg-if = "1.0.0"
shared = { path = "../../shaders/shared" }
spirv-builder = { path = "../../../crates/spirv-builder", default-features = false }
# TODO: Remove this once no longer needed, only needed to make cargo-deny happy for some reason.
# https://rustsec.org/advisories/RUSTSEC-2021-0119
nix = "0.20.2"
[target.'cfg(target_os = "macos")'.dependencies]
ash-molten = { version = "0.12.0", features = ["pre-built"] }
So I thought if I did something similar on my own code it would work:
[package]
name = "vulkan_bindings"
version = "0.1.0"
edition = "2021"
[features]
default = ["use-compiled-tools"]
use-installed-tools = ["spirv-builder/use-installed-tools"]
use-compiled-tools = ["spirv-builder/use-compiled-tools"]
[dependencies]
ash = { version = "0.37.0" }
glfw = { version = "0.45.0", features = ["vulkan"] }
gpu-allocator = "0.18.0"
ash-window = "0.9"
winit = "0.26"
structopt = "0.3.20"
cfg-if = "1.0.0"
# shared = { path = "../../shaders/shared" }
spirv-builder = { path = "rust-gpu/crates/spirv-builder", default-features = false }
paste = "1.0.8"
termcolor = "1.1.3"
But this fails with a multiplicity of errors, the first of which is:
error[E0432]: unresolved import `rustc_codegen_ssa::METADATA_FILENAME`
--> /home/makogan/.cargo/git/checkouts/rust-gpu-e0a37a82a46176e6/8052971/crates/rustc_codegen_spirv/src/link.rs:9:52
|
9 | use rustc_codegen_ssa::{CodegenResults, NativeLib, METADATA_FILENAME};
| ^^^^^^^^^^^^^^^^^ no `METADATA_FILENAME` in the root
Not that I am pointing my project to the spirv-builder crate in the rust-gpu repo I downloaded, which worked for the ash example.
I also tried just following the instructions in the docs
So I declared spirv-builder as a build-dependency (using the git link) and I made a build.rs build script and copy pasted the snippet in the docs, as described.
I get the same error about missing METADATA_FILENAME.
I do have a rust-toolchain file setup just like the docs mentioned, and I tried switching the edition field in the toml to 2018, but I consistently get the same error.
I am nto sure what to do now.

Getting error while using #[js_export] from crate stdweb

I need to read client file in a rust-wasm program and I am trying two solution given on stackoverflow https://stackoverflow.com/a/51231915 and https://stackoverflow.com/a/69305956/4466255. Both these solutions are accepted and given bounties so I am assuming they work.
both of these have used following code
use stdweb::js_export
#[js_export]
But when i try to compile these codes I got following error
error[E0425]: cannot find function `__web_free` in module `stdweb::private`
--> src/lib.rs:14:1
|
14 | #[js_export]
| ^^^^^^^^^^^^ not found in `stdweb::private`
|
= note: this error originates in the attribute macro `js_export` (in Nightly builds, run with -Z macro-backtrace for more info)
What I am missing. How can we make it work?
My current code, which I have taken from stackoverflow https://stackoverflow.com/a/51231915, in lib.rs is
#[macro_use]
extern crate stdweb;
use stdweb::js_export;
use stdweb::web::FileReader;
use stdweb::web::FileReaderResult;
#[js_export]
fn print_result(file_reader: FileReader) -> String {
match file_reader.result() {
Some(value) => match value {
FileReaderResult::String(value) => value,
_ => String::from("not a text"),
}
None => String::from("empty")
}
}
pub fn test_reader() {
stdweb::initialize();
stdweb::event_loop();
}
and the Cargo.toml file is
[package]
name = "read-file2"
version = "0.1.0"
authors = ["kushdesh"]
edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2.63"
stdweb = "0.4.20"
console_error_panic_hook = { version = "0.1.6", optional = true }
wee_alloc = { version = "0.4.5", optional = true }
[dev-dependencies]
wasm-bindgen-test = "0.3.13"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

Resolving imports in Rust

I'm having trouble with importing rand crate from crates.io. After adding the line rand="0.8.3" and then running command cargo build for the project, it keeps displaying the same errors:
error[E0432]: unresolved import `rand`
--> main.rs:1:5
|
1 | use rand::Rng;
| ^^^^ maybe a missing crate `rand`?
error[E0433]: failed to resolve: use of undeclared crate or module `rand`
--> main.rs:4:25
|
4 | let secret_number = rand::thread_rng().gen_range(1..=11);
| ^^^^ use of undeclared crate or module `rand`
error: aborting due to 2 previous errors
the cargo.toml file
[package]
name = "roller"
version = "0.1.0"
authors = ["User"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.8.3"
Basically the simplest reproducible example is this single line of code:
use rand::Rng;
fn main(){
let secret_number = rand::thread_rng().gen_range(1..=11);
print!("{}",secret_number);
}
What's wrong with it?
Just in case:
The **cargo.lock**file:
# This file is automatically #generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "getrandom"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c"
[[package]]
name = "ppv-lite86"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]]
name = "rand"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73"
dependencies = [
"rand_core",
]
[[package]]
name = "roller"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
In file Cargo.toml add lines:
[dependencies]
rand="0.3.14"
And rebuild project!
It worked with these versions:
rustc 1.53.0
cargo 1.53.0
Took answer from here https://github.com/rust-lang/rust/issues/34616
got the same error when I run it with vs code(rust-analyzer).
When you press the run button
rustic main.rs this command is called in terminal, it will cause error.
Type this in terminal,
cargo build
cargo run
It works well
If you think that you did everything right and get a strange unable to import error like this then you could try cargo clean.
That will allow you to fully re-build your binary/library after that.

Resources