Comments (7)
FYI for those of you coming to this thread - impl Filter<Extract = impl Reply>
is allowed now!
from warp.
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
, soimpl Filter<Extract = impl Reply>
isn't legal Rust (yet). You can use with a concrete type, likeBoxedFilter<impl Reply>
.
from warp.
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.
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.
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.
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.
This was done, thanks!
from warp.
Related Issues (20)
- Feature request: More customization points in tracing HOT 2
- EC Private key support HOT 1
- Make `Option<F>` a filter when F is a filter
- Feature request: `warp::make_service()` or `warp::service_with_addr()`
- CVE-2023-43669/GHSA-9mcr-873m-xcxp: tungstenite <= 0.20.0 DoS vulnerability HOT 1
- [feature request] [low prio] Non-Metal fallback option HOT 2
- SSE gives up on Streams that return Pending HOT 3
- Server::run should return ! (never type)
- Default OS / self signed certificate without create it HOT 1
- websocket disconnect (code: 1006, reason: "") HOT 2
- Add `rust-version` into `Cargo.toml` HOT 1
- Error `connection closed before message completed` after 60s HOT 2
- Websocket connection closes immediately, but not with firefox HOT 4
- Navigation between panes using vim commands HOT 1
- examples/tls/cert.pem has expired
- Upgrade to hyper v1 HOT 4
- key contains no private key while using ecc key HOT 1
- integrate with monoio
- HELP: filters inside .then never get executed HOT 1
- Chinese input method following error in notebook HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from warp.