Simplify Service Trait with Async Trait in Next Major Version
- Vorherrschende Sprache
- Rust
- Sterne
- 775
- Forks
- 363
- Ø Merge
- 21 Std. 2 Min.
- Gemergte PRs (30 T.)
- 31
Beschreibung
The current Service trait is defined as:
```rust
pub trait Service {
type Response;
type Error;
type Future: Future>;
fn poll_ready(&self, ctx: &mut task::Context<'_>) -> Poll>;
fn call(&self, req: Req) -> Self::Future;
}
```
In the next major version, we could leverage async traits to simplify the trait definition and improve ergonomics. Proposed change:
```rust
pub trait Service {
type Response;
type Error;
fn poll_ready(&self, ctx: &mut task::Context<'_>) -> Poll>;
fn call(&self, req: Req) -> impl Future>;
}
```
This allows implementations like:
```rust
struct MyMiddleware;
impl Service for MyMiddleware {
type Response = ();
type Error = ();
fn poll_ready(&self, ctx: &mut task::Context<'_>) -> Poll> {
todo!()
}
async fn call(&self, req: Req) -> Result {
todo!()
}
}
```
**Benefits:**
- Eliminates the need to define a `Future` associated type.
- Simplifies implementation by using `async fn` directly.
- Improves readability and maintainability.
We can also use async trait for `ServiceFactory` and `Transform`
Beitragsleitfaden
Bewertung
Dieses Issue wurde noch nicht bewertet.