#749·warp

Publish `Tuple` or `Func` traits

Author: mraerinoCreated Nov 15, 2020Updated Feb 18, 2025
Labelsfeature

Is your feature request related to a problem? Please describe.

I'm trying to realize an opinionated api builder pattern based on warp.

Example:

rust
pub struct ApiBuilder<F> {
    filter: F,
}

impl<F: Filter<Error = Rejection>> ApiBuilder<F> {
    pub fn mount<N, H, Fut, R>(self, filter: N, handler: H) -> ApiBuilder<impl Filter>
    where
        F::Future: TryFuture,
        N: Filter<Error = Rejection, Extract = ()> + Clone + Send,
        N::Future: TryFuture,
        H: Fn() -> Fut + Clone + Send,
        Fut: TryFuture<Ok = R, Error = Rejection> + Send,
        R: Reply,
    {
        let action = filter.and_then(handler);
        let filter = self.filter.or(action);
        ApiBuilder { filter }
    }
}

This example compiles only if I constrain Filter::Extract to the empty tuple as you can see above.

If i want to allow arbitrary filters to be used and values to be exposed, I'd need to introduce constraints using the Func or Tuple traits which can't be accessed from crate users right now.

Describe the solution you'd like

Expose Func or Tuple as public API.

Or any other suggestions to make this work?

Describe alternatives you've considered

I think https://github.com/seanmonstar/warp/pull/471 might also allow to do this, since the handler would then just take a single tuple argument, which I'm fine with.

Additional context

this is the pattern i'm trying to enable, maybe you have other ideas or want to upstream this builder pattern?

rust
let builder = ApiBuilder::new()
    .mount(warp::path!("hello" / String).and(warp::get()), |name| async { /* do something */ });