dtolnay / dtolnay/async-trait

Possibly optimize simple function body?

Open
#240 0 comments 3 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
2.2k
Forks
101
PR merge metrics
No merged PRs in 30d

Description

I'm working on an autogenerated `EventHandler` trait with many `async fn on_some_event()` methods. There is also one `async fn fallback()` method for which all default implementations of `on_some_event` call, giving:

```rust
#[async_trait::async_trait]
trait EventHandler {
async fn fallback(self, args: SomeType) -> U;

// This 30 times or so for default implementations
async fn on_some_event(self, args: SomeType) -> U {
self.fallback(args).await
}
}
```

This leads to double-boxing the fallback future, which is likely executing more often than not if the library users don't handle all events.

Would it be possible to flag an async method as "`transparent`" perhaps? Strip the `.await` and return the result directly.

```rust
// ...
#[async_trait(transparent)]
async fn on_some_event(self, args: SomeType) -> U {
self.fallback(args).await
}
// would desugar to
fn on_some_event<'async_trait>(self, args: SomeType)
-> std::pin::Pin + Send + 'async_trait>>
where
Self: Send + 'async_trait
{
self.fallback(args)
}
```

However, if the `async_trait` codegen is stable enough, I could also just desugar it manually for default implementations.

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.