DioxusLabs / DioxusLabs/dioxus
Trying to extract FromContext on a trait object throws a compiler error
- Dominant language
- Rust
- Stars
- 39.1k
- Forks
- 1.9k
- Avg merge
- 4d 10h
- Merged PRs (30d)
- 4
Description
**Problem**
The following code results in a compiler error:
```rust
use std::sync::Arc;
use dioxus::prelude::*;
trait Foo: Send + Sync {}
#[server]
pub async fn bar() -> Result<(), ServerFnError> {
let FromContext(my_context) = extract::>, _>().await?;
Ok(())
}
```
```
error: higher-ranked lifetime error
--> src/lib.rs:7:1
|
16 | #[server]
| ^^^^^^^^^
|
= note: this error originates in the attribute macro `server` (in Nightly builds, run with -Z macro-backtrace for more info)
```
This is because it is triggering rust-lang/rust#102211.
**Steps To Reproduce**
Steps to reproduce the behavior:
1. Compile the above for server
This was a complete nightmare to track down, minimize, and fix.
**Expected behavior**
No compiler error, because it is valid rust code.
There are a number of ways to work around it, but the two main ones are as follows:
1. Hide the `Arc` inside another type - ie. extract it into a struct. For some reason this fixes it!
2. Make the modification below to `extract` (based on my limited understanding of the bug, I believe it's the explicit `Send` that sidesteps the issue:
```rust
pub fn extract, I>(
) -> impl std::future::Future> + Send {
async { E::from_request(&server_context()).await }
}
```
**Environment:**
- Dioxus version: 0.6.3
- Rust version: tested on multiple rust versions
- OS info: NixOS 24.11
- App platform: N/a
**Questionnaire**
I would like to fix and I have a solution.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.