Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate adopting impl Trait instead of our current Box<HandlerFuture> #32

Open
smangelsdorf opened this issue Aug 20, 2017 · 1 comment

Comments

@smangelsdorf
Copy link
Contributor

Originally discussed in the announcement thread, it would be nice if we could support concrete future types using the impl Trait feature which is currently only available on nightly Rust.

What we actually need, though, is for impl Trait to be supported in the associated type position, and for the inferred type to be able to vary with the generic type parameters, which is being discussed at:

We'll need a Middleware trait that looks something like:

trait Middleware<R>
where
    R: Future<Item = (State, Response), Error = (State, Error)> + Send,
{
    type Output: Future<Item = (State, Response), Error = (State, Error)> + Send;

    fn call<Chain>(self, state: State, request: Request, chain: Chain) -> Self::Output
    where
        Chain: FnOnce(State, Request) -> R,
        Self: Sized;
}

Here, R is the concrete type of the future returned by the next Middleware. Though it's only used for the call function, it's likely to be important that this exists at the trait level instead, as SomeUsefulMiddleware::<R>::Output will need to vary with the R type.

Implementing Middleware would then look something like:

struct MyMiddleware;
impl<R> Middleware<R> for MyMiddleware {
    type Output = impl Future<Item = (State, Response), Error = (State, Error)> + Send;

    fn call<Chain>(self, state: State, request: Request, chain: Chain) -> Self::Output
    where
        Chain: FnOnce(State, Request) -> R,
        Self: Sized,
    {
        unimplemented!()
    }
}

Constructing the middleware pipeline will be considerably more difficult with the R parameter here, so this will take some experimentation to prove that the idea works. If this is deemed a suitable way forward, we could investigate making a similar change to Handler.

@msrd0
Copy link
Member

msrd0 commented Sep 11, 2020

The required RFC has landed but not been standardized, tracking issue is rust-lang/rust#63063

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants