Skip to content

Instantly share code, notes, and snippets.

@overhacked
Last active October 13, 2020 15:14
Show Gist options
  • Save overhacked/59aea8c0f5e59486abe3dc00081cb9c2 to your computer and use it in GitHub Desktop.
Save overhacked/59aea8c0f5e59486abe3dc00081cb9c2 to your computer and use it in GitHub Desktop.
warp_question about synthetic delay
[package]
name = "warp_question"
version = "0.1.0"
authors = ["Ross Williams <ross@ross-williams.net>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tokio = { version = "0.2.22", features = ["full"] }
warp = "0.2.5"
Compiling warp_question v0.1.0 (/Users/overhack/Downloads/warp_question)
error[E0308]: mismatched types
--> src/main.rs:28:9
|
21 | fn add_latency<F, T, O>(delay: Duration) -> impl Fn(F) -> O
| - this type parameter
...
28 | / warp::any()
29 | | .and(filter)
30 | | .and_then(|extract: T| async move {
31 | | // Operation with param, producing no extract:
... |
34 | | Ok(extract)
35 | | })
| |______________^ expected type parameter `O`, found struct `warp::filter::and_then::AndThen`
|
::: /Users/overhack/.cargo/registry/src/github.com-1ecc6299db9ec823/warp-0.2.5/src/filters/any.rs:47:17
|
47 | pub fn any() -> impl Filter<Extract = (), Error = Infallible> + Copy {
| ---------------------------------------------------- the found opaque type
|
= note: expected type parameter `O`
found struct `warp::filter::and_then::AndThen<warp::filter::and::And<impl warp::filter::Filter+std::marker::Copy, F>, [closure@src/main.rs:30:23: 35:14 delay:_]>`
error[E0698]: type inside `async` block must be known in this context
--> src/main.rs:13:9
|
13 | let routes = warp::any()
| ^^^^^^ cannot infer type
|
note: the type is part of the `async` block because of this `await`
--> src/main.rs:18:5
|
18 | warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0698]: type inside `async` block must be known in this context
--> src/main.rs:18:17
|
18 | warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
| ^^^^^^ cannot infer type
|
note: the type is part of the `async` block because of this `await`
--> src/main.rs:18:5
|
18 | warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0698]: type inside `async` block must be known in this context
--> src/main.rs:18:5
|
18 | warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
| ^^^^^^^^^^^ cannot infer type for type parameter `F` declared on the function `serve`
|
note: the type is part of the `async` block because of this `await`
--> src/main.rs:18:5
|
18 | warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0698]: type inside `async` block must be known in this context
--> src/main.rs:18:25
|
18 | warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
| ^^^ cannot infer type for type parameter `F`
|
note: the type is part of the `async` block because of this `await`
--> src/main.rs:18:5
|
18 | warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 5 previous errors
Some errors have detailed explanations: E0308, E0698.
For more information about an error, try `rustc --explain E0308`.
error: could not compile `warp_question`.
To learn more, run the command again with --verbose.
use std::time::Duration;
use warp::{
self,
Filter,
Rejection,
Reply,
};
#[tokio::main]
async fn main() {
let delay = Duration::from_millis(500);
let routes = warp::any()
.and(warp::get())
.map(|| "Hello")
.with(warp::wrap_fn(add_latency(delay)));
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}
fn add_latency<F, T, O>(delay: Duration) -> impl Fn(F) -> O
where
F: Filter<Extract = (T,), Error = Rejection> + Clone + Send + Sync + 'static,
F::Extract: Reply,
O: Filter<Extract = (T,), Error = Rejection> + Clone + Send + Sync + 'static,
{
move |filter: F| {
warp::any()
.and(filter)
.and_then(|extract: T| async move {
// Operation with param, producing no extract:
println!("Adding {:?} latency", delay);
tokio::time::delay_for(delay).await;
Ok(extract)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment