DioxusLabs / DioxusLabs/dioxus

Support rust's AsyncFn types for EventHandlers

Open
#3,481 2 comments 0 reactions 0 assignees View on GitHub
core enhancement
Dominant language
Rust
Stars
39.1k
Forks
1.9k
Avg merge
4d 10h
Merged PRs (30d)
4

Description

**Problem**

**Steps To Reproduce**

```rust
use dioxus::prelude::*;

#[server]
async fn save_dog(image: String) -> Result<(), ServerFnError> {
Ok(())
}

#[component]
fn DogView() -> Element {
let src = use_signal(|| "https://images.dog.ceo/breeds/pitbull/dog-3981540_1280.jpg");
let save = async move |_| {
save_dog(src.cloned().to_owned()).await.unwrap();
};

rsx! {
img { src }
div {
button { onclick: save, "save!" }
}
}
}
```

Error:

```rust
error: async closure does not implement `FnMut` because it captures state from its environment
--> src/main.rs:104:16
|
104 | let save = async move |_| {
| ^^^^^^^^^^^^^^
...
113 | button { onclick: save, "save!" }
| ------------- required by a bound introduced by this call
|
= note: required for `Callback>` to implement `SuperFrom<{async closure@src/main.rs:104:16: 104:30}, MarkerWrapper>`
= note: required for `{async closure@src/main.rs:104:16: 104:30}` to implement `SuperInto>, MarkerWrapper>`
```

But it works when you use an inner async block instead:

```diff
- let save = async move |_| {
+ let save = move |_| async move {
save_dog(src.cloned().to_owned()).await.unwrap();
};
```

**Expected behavior**

Async closures has been [stabilized](https://github.com/rust-lang/rust/pull/132706), so it should be expected that it works with Dioxus.

**Screenshots**

**Environment:**
- Dioxus version: v0.6.0
- Rust version: v1.85.0-nightly (45d11e51b 2025-01-01)
- OS info: Linux
- App platform: `web`

**Questionnaire**

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.