Building Embassy and embedded-storage-async

Welcome to the embedded category of the Rust users forum!

If you haven't done so already, please read the pinned topic which explains the scope of this category.

Also, before you start a new thread make sure to:

  • Check the embedded Rust bookshelf. If you are looking for resources to learn about embedded development with Rust the bookshelf has you covered.
  • Search through the existing threads. People may already by discussing the topic you have in mind!

Finally, I'd like to remind you about Rust's Code of Conduct which applies to this category and forum. Be excellent to each other!

Hello all!

I am trying to follow a embassy tutorial and build the blinky app. My problem is that I am getting an error out of the .cargo registry cache that I am unsure how to fix.

After
cargo run --bin blinky

I get

 C:\users\fdemi\DevEnv/.cargo\registry\src\index.crates.io-6f17d22bba15001f\embedded-storage-async-0.4.1\src\nor_flash.rs:72:2
   |
72 |     async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
   |     -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |     |
   |     `async` because of this
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait
   = note: see issue #91611 <https://github.com/rust-lang/rust/issues/91611> for more information   
   = help: add `#![feature(async_fn_in_trait)]` to the crate attributes to enable  ```

The help note is pretty clear but the problem is where I should put that. How do I influence crates being built in the cache? I am new to the async eco-system at this time.

I created a project and installed embassy inside it with the following tool-chain.toml:

C:\users\fdemi\DevEnv/.cargo\registry\src\index.crates.io-6f17d22bba15001f\embedded-storage-async-0.4.1\src\nor_flash.rs:72:2
|
72 | async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| async because of this
|
= note: async trait functions are not currently supported
= note: consider using the async-trait crate: crates.io: Rust Package Registry
= note: see issue #91611 https://github.com/rust-lang/rust/issues/91611 for more information
= help: add #![feature(async_fn_in_trait)] to the crate attributes to enable


Sorry about the botched first post. I was tired last night.

Thanks,
Frank

Try to start by reading the error message. I understand that you might be new to programming and the stress caused by seeing an error might get you anxious, but you have to calm down and read what you have in the screen. Error messages from the Rust compiler are particularly famous for being helpful.

Actually I am not new to programming having started almost 50 years ago. Not new to Rust either.
Sigh

I have looked at the line

= help: add #![feature(async_fn_in_trait)] to the crate attributes to enable

and tried adding it in various locations such as in the ```
embedded-storage-async-0.4.1

crate. No luck. 

Put in the context of the toolchain.toml for the project containing the Embassy build, no luck.

So yes the help looks helpful but using it has not been straight forward. I have searched for a match at that and examined my environment.

Your answer was a bit condescending

Frank

Sorry if my first post sounded rude. I had not seen that you were struggling with the async trait annotations.

For the record, code blocks need to be enclosed within two sets of triple backticks (```). If you do it only once, the formatting of your posts will be all over the place, making them really hard to read.

Now, to the point of your post. Here's an example of usage of the async trait crate:

use async_trait::async_trait;

#[async_trait]
trait MyAsyncTrait {
  async fn do_something() -> ();
}

struct Implementor;

#[async_trait]
impl MyAsyncTrait for Implementor {
  async fn do_something() -> () {
    println!("Look ma, I did something async!");
  }
}

Note that you have to annotate both the async trait and the impl block with #[async_trait].

1 Like

Hi Thanks for that.

Part of the problem is that the crate embedded-storage-async-0.4.1 is brought in to the cargo registry to satisfy build dependencies. Somehow I don't think hacking the crate in the cargo registry is the solution.

So I do have a tool-chain.toml with the following contents:

# rust-toolchain.toml
[toolchain]
channel = "nightly-2023-10-02"
targets = ["thumbv6m-none-eabi"]
features = ["async_fn_in_trait", "const_fn_trait_bound", "const_trait_impl", "custom_test_frameworks", "default_alloc_error_handler", "dispatch_from_dyn", "doc_cfg", "doc_spotlight", "doc_spotlight"]
#![feature(async_fn_in_trait)]

That toml is at the top of root with Embassy's folder at that level. From my understanding that features listed in the toolchain would carry over to the crate builds in the .cargo/registry. Looking at the source for the embedded-storage-async crate I don't see your use async_trait::async_trait; HMM. Shouldn't the source for that be generated at build time? I'm sure there is something missing in my environment to affect this but now I am out of my experience.

I will note that while I have been programming for awhile there was a lapse for 12 years as I got dragged into more marketing/sales efforts. In the mean time alot of good stuff like rust came along and I am very impressed.

Thanks,
Frank

I am using nightly as my default. Forgot to say that.

What version of Rust are you using? Since version 1.75, async_fn_in_trait was stabilized, and the version 0.4.1 of embedded-storage-async was updated to reflect that, that's why you don't see the annotations from the async-trait crate that I mentioned earlier.

So far, it seems that the easiest fix for your problem will be to update your version of Rust.

I am currently on 1.76 but....

I am found that just entering rustc --version can give at first surprising results even if you had changed to a stable tool-chain! rustc reads the hierarchical rust directories etc. And that gave me again rustc being nightly
Well I am hacking tool-chain.html files to fix up the problem that Embassy has not been ported to the recent versions of rustc. It has nightly embedded in the tool-chain.tomls .

Hmmm Well I have learned a few things which isn't a bad thing. I can see why amateurs like me should stay away from nightly.

Thanks for your help

I found my problem centered that instructions given out in website had me switch to a branch that is not stable

1 Like