Skip to content

async fn in trait: trait method with default impl doesn't work #102308

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Dirbaio opened this issue Sep 26, 2022 · 2 comments
Closed

async fn in trait: trait method with default impl doesn't work #102308

Dirbaio opened this issue Sep 26, 2022 · 2 comments
Labels
F-async_fn_in_trait Static async fn in traits

Comments

@Dirbaio
Copy link
Contributor

Dirbaio commented Sep 26, 2022

playground

trait Foo {
    async fn bar(&mut self) {}
}

fails with

error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
 --> src/main.rs:5:29
  |
5 |     async fn bar(&mut self) {}
  |                             ^^ expected associated type, found opaque type
  |
  = note: expected associated type `impl Future<Output = ()>` (trait associated opaque type at <src/main.rs:5:29>)
                 found opaque type `impl Future<Output = ()>` (opaque type at </rustc/f5193a9fcc73dc09e41a90c5a2c97fc9acc16032/library/core/src/future/mod.rs:72:43>)

Nightly version: 1.66.0-nightly (2022-09-25 f5193a9)

@rustbot label +F-async_fn_in_traits

@rustbot rustbot added the F-async_fn_in_trait Static async fn in traits label Sep 26, 2022
@compiler-errors
Copy link
Member

compiler-errors commented Sep 26, 2022

Duplicate of #101665, fixed by #101679.

@compiler-errors
Copy link
Member

This works now.

bors bot added a commit to rust-embedded/embedded-hal that referenced this issue Nov 23, 2022
407: async: switch to async-fn-in-traits, release v0.2.0-alpha.0 r=eldruin a=Dirbaio

Latest Rust nightlies have somewhat usable async-fn-in-trait support already! 🎉 

embassy-nrf updated here embassy-rs/embassy#974

Paprecuts encountered:

- there's this annoying error [playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=f04fca1f2a3d643c323fb49c05bd3ed3), workaround is to use the concrete type instead of `Self::Error`. This is a limitation of all `async fn`s, not just in traits, but it hits especially hard within traits, so I dunno if there's plans to improve it.

> `async fn` return type cannot contain a projection or `Self` that references lifetimes from a parent scope

- The SpiDevice trait ICEs, issue filed rust-lang/rust#102310
- default methods don't work, but there's a PR already rust-lang/rust#102308

Due to the last 2 I've left `SpiDevice` alone for now.

Co-authored-by: Dario Nieuwenhuis <dirbaio@dirbaio.net>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
F-async_fn_in_trait Static async fn in traits
Projects
None yet
Development

No branches or pull requests

3 participants