Compile Error len is not yet stable as a const fn - rust

I get an error from one of my dependencies bytes 0.5.2. Here a code example of the error:
pub const fn foo(foo: &'static [u8]) -> usize {
foo.len()
}
error: `core::slice::<impl [T]>::len` is not yet stable as a const fn
--> <source>:2:5
|
2 | foo.len()
| ^^^^^^^^^
error: aborting due to previous error
active toolchain
----------------
stable-x86_64-pc-windows-msvc (default) rustc 1.38.0 (625451e37 2019-09-23)

The 0.5.x would appear to require Rust 1.39 so the easiest option would likely be upgrading to the newest version.
The error states
core::slice::<impl [T]>::len is not yet stable as a const fn
and if you look at the release notes for 1.39 you can see that one of the entries is
str::len, [T]::len and str::as_bytes are now const functions
so this crate specifically requires >=1.39

Related

Why is Rocket's `Json` type not found in the `content` module?

Today I found that a project that previously worked no longer compiles. Here is a minimal example that reproduces my sudden error:
use rocket::{get, launch, routes};
use rocket::response::content;
#[get("/health")]
pub fn health() -> content::Json<String> {
content::Json("ok".parse().unwrap())
}
#[launch]
async fn rocket() -> _ {
rocket::build().mount("/actuator", routes![health])
}
error[E0412]: cannot find type `Json` in module `content`
--> src\main.rs:5:29
|
5 | pub fn health() -> content::Json<String> {
| ^^^^ not found in `content`
|
help: consider importing this struct
|
1 | use rocket::serde::json::Json;
|
error[E0425]: cannot find function, tuple struct or tuple variant `Json` in module `content`
--> src\main.rs:6:14
|
6 | content::Json("ok".parse().unwrap())
| ^^^^ not found in `content`
|
help: consider importing this tuple struct
|
1 | use rocket::serde::json::Json;
|
I am using cargo 1.59.0 (49d8809dc 2022-02-10) and rustc 1.59.0 (9d1b2106e 2022-02-23). And here are my dependencies:
[dependencies]
rocket = { version = "0.5.0-rc.1", features = ["json"] }
The content::Json is supposed to come from Rocket and should be enabled by the cargo feature above. It definitely worked the past. What should I do to fix this problem? How did this happen?
The content module was changed between Rocket release candidates. Compare the documentation for 0.5.0-rc.1 and 0.5.0-rc.2. The Json type moved to rocket::serde::json::Json. So your code should look like this:
use rocket::serde::json::Json;
pub fn health() -> Json<String> {
Json("ok".parse().unwrap())
}
But I did not upgrade the rocket version, I am still using 0.5.0-rc.1
It seems you did, albiet unknowningly. This can happen if cargo update was ran or you simply didn't preserve your Cargo.lock file (or something recreated it). It is somewhat dubious if rc.1 should be seen as compatible with rc.2, but regardless, you can lock your dependency to a specific version by prefixing it with = if you want to stay on rc.1:
[dependencies]
rocket = { version = "=0.5.0-rc.1", features = ["json"] }

Rust-Rocket cannot be compiled

I have set up a rust base project according to the Getting Started page of the rocket-framework:
I added this line to my Cargo.toml: rocket = "0.4.10"
My main.rs:
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
fn main() {
rocket::ignite().mount("/", routes![index]).launch();
}
When I try to run (or cargo build it) I get the following error:
Compiling rocket v0.4.10 error[E0277]: the trait bound `(dyn handler::Handler + 'static): handler::Handler` is not satisfied --> /Users/.../.cargo/registry/src/github.com-1ecc6299db9ec823/rocket-0.4.10/src/rocket.rs:299:41
| 299 | let outcome = route.handler.handle(request, data);
| ^^^^^^ the trait `handler::Handler` is not implemented for `(dyn handler::Handler + 'static)`
For more information about this error, try `rustc --explain E0277`. error: could not compile `rocket` due to previous error
I also tried it with previous versions of the rocket framework, all of them threw the same error.
Of course I'm using the latest nightly version of rust, right before cargo build I entered following commands:
rustup override set nightly
info: using existing install for 'nightly-x86_64-apple-darwin'
info: override toolchain for '/Users/...' set to 'nightly-x86_64-apple-darwin'
nightly-x86_64-apple-darwin unchanged - rustc 1.58.0-nightly (bd41e09da 2021-10-18)
Is there a known issue with the latest rust compiler on MacOS? Is there any other solution I could try?

Why is the trait not implemented?

I wanted to try the amethyst_physics library to make a game. (Duh)
I followed to Example, but somhow I does not work:
use amethyst::GameDataBuilder;
use amethyst_physics::{PhysicsBundle};
use amethyst_nphysics::NPhysicsBackend;
fn main() -> amethyst::Result<()> {
amethyst::start_logger(Default::default());
let game_data = GameDataBuilder::default()
.with_bundle(
PhysicsBundle::<f32, NPhysicsBackend>::new()
)
;
Ok(())
}
Error:
the trait bound `amethyst_physics::PhysicsBundle<'_, '_, f32, amethyst_nphysics::NPhysicsBackend>: amethyst::amethyst_core::SystemBundle<'_, '_>` is not satisfied
the trait `amethyst::amethyst_core::SystemBundle<'_, '_>` is not implemented for `amethyst_physics::PhysicsBundle<'_, '_, f32, amethyst_nphysics::NPhysicsBackend>`
Here is the example.
What am I doing wrong?
This appears to be a bug. It compiles successfully using amethyst version 0.15.1 but not 0.15.3. A regression like this is not expected during a patch change.
amethyst uses amethyst_core version 0.15.3 (where SystemBundle is defined) but amethyst_physics uses amethyst_core version 0.10.1.
I've filed an issue on the amethyst repository.
Use this as a workaround:
amethyst = { version = ">=0.15.0, <0.15.3", features = ["empty"] }

near-bindgen macro: unsupported argument type

I am writing a smart contract promise interface for NEAR blockchain.
I have the following interface:
#[ext_contract(token_receiver)]
pub trait ExtTokenReceiver {
fn process_token_received(&self, sender_id: AccountId, amount: Balance, message: [u8]) -> Option<String>;
}
However this fails with the following error:
error: Unsupported argument type.
--> src/token.rs:32:1
|
32 | #[ext_contract(token_receiver)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
How can I debug near-bindgen macro
What is the Unsupported argument type in this case
How to fix my interface
I believe the unsupported arguments is [u8]. I have changed the code to use Vec and it works:
use near_sdk::ext_contract;
#[ext_contract(token_receiver)]
pub trait ExtTokenReceiver {
fn process_token_received(
&self,
sender_id: AccountId,
amount: Balance,
message: Vec<u8>,
) -> Option<String>;
}
Finished release [optimized] target(s) in 0.05s
What I believe is the issue that the compiler doesn't know the size of the static array at compile time and complains, and Vec as it is a dynamic container that is fine.
For the first question it says: run nightly with -Z macro-backtrace, which you can do by editing the project's build.sh. Another tool to debug macros in rust is to use cargo-expand, which will decompile from the altered AST.
For the second and third questions: my guess is that it is the [u8], which is an array that must be known at compile time. You should use Vec<u8>, which can be coerced into a &[u8] where needed.

Unable to coerce &String to &str

I'm trying to compile the following code, from the Rust book at the Rust official website.
fn takes_slice(slice: &str) {
println!("Got: {}", slice);
}
fn main() {
let s = "Hello".to_string();
takes_slice(&s);
}
At compilation, it throws the following error
/devl/rust/bc_09/src/main.rs:7:17: 7:19 error: mismatched types:
expected &str, found &collections::string::String (expected str,
found struct collections::string::String)
/devl/rust/bc_09/src/main.rs:7 takes_slice(&s);
^~ error: aborting due to previous error Could not compile hello_world.
Here is the Rust version I'm running: rustc 1.0.0-nightly (44a287e6e 2015-01-08 17:03:40 -0800)
That's a really old version of the nightly in Rust terms! Old enough that the &String -> &str coercion isn't available. You just need to upgrade to a newer version.

Resources