actix / actix/actix-net

Simplify Service Trait with Async Trait in Next Major Version

Aperta
#670 1 commento 3 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Rust
Stelle
775
Fork
363
Merge medio
21h 2m
PR unite (30g)
31

Descrizione

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`

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.