Coder Social home page Coder Social logo

Comments (7)

danbruder avatar danbruder commented on May 4, 2024 1

FYI for those of you coming to this thread - impl Filter<Extract = impl Reply> is allowed now!

from warp.

seanmonstar avatar seanmonstar commented on May 4, 2024

There's two ways to return a Filter from a function:

With impl Filter<Extract = T, Error = Rejection>

pub fn index() -> impl warp::Filter<Extract = (), Error = Rejection> {
    warp::path::index()
}

pub fn index_ok() -> impl warp::Filter<Extract = (StatusCode,), Error = Rejection> {
    index()
        .map(|| StatusCode::OK)
}

With Filter::boxed()

pub fn get_routes() -> BoxedFilter<(impl Reply,)> {
    some_filter()
        .or(some_other())
        .or(a_third)
        .with(my_log)
        // etc
        .boxed()
}

There's reasons you might pick one over the other.

  • impl Filter
    • allows monomorphization, and so possibly better performance or optimizations.
  • BoxedFilter
    • Uses dynamic dispatch, which can dramatic improve compile times.
    • You can't use nested impl Trait, so impl Filter<Extract = impl Reply> isn't legal Rust (yet). You can use with a concrete type, like BoxedFilter<impl Reply>.

from warp.

blm768 avatar blm768 commented on May 4, 2024

That makes more sense. I'd looked at BoxedFilter, but I hadn't managed to figure out what type arguments to give it.
Do you think that this is a significant enough topic to have an example for it, or would it belong somewhere else in the documentation? I'd be happy to submit a PR with an example or doc updates.

from warp.

seanmonstar avatar seanmonstar commented on May 4, 2024

Yes, examples or docs are sorely needed. I've been meaning to flesh out the warp::test section of docs showing this off, but maybe there's better places.

And unfortunately, since trait specialization isn't stable, it isn't immediately obvious that a filter extracting just 1 value needs to set Extract = (Foo,), not Extract = Foo.

from warp.

blm768 avatar blm768 commented on May 4, 2024

I can think of a couple of places in the docs that could be tweaked to give people with intermediate Rust experience enough to figure things out; an inline example of the correct type signature in BoxedFilter or Filter::boxed() might do it. I should be able to draft something up tomorrow.

from warp.

blm768 avatar blm768 commented on May 4, 2024

Oh, wow. Just realized that Filter::boxed() already had examples. Apparently I need to learn how to read. But I've thrown together an example and put a basic example in the BoxedFilter docs, so I'll submit a PR.

from warp.

seanmonstar avatar seanmonstar commented on May 4, 2024

This was done, thanks!

from warp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.