/
service.rs
91 lines (77 loc) · 2 KB
/
service.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::net::SocketAddr;
use std::task::{Context, Poll};
use std::pin::Pin;
use std::future::Future;
use pin_project::pin_project;
use futures::future::TryFuture;
use crate::reject::IsReject;
use crate::reply::Reply;
use crate::route::{self, Route};
use crate::server::{IntoWarpService, WarpService};
use crate::{Filter, Request};
#[derive(Copy, Clone, Debug)]
pub struct FilteredService<F> {
filter: F,
}
impl<F> WarpService for FilteredService<F>
where
F: Filter,
<F::Future as TryFuture>::Ok: Reply,
<F::Future as TryFuture>::Error: IsReject,
{
type Reply = FilteredFuture<F::Future>;
#[inline]
fn call(&self, req: Request, remote_addr: Option<SocketAddr>) -> Self::Reply {
debug_assert!(!route::is_set(), "nested route::set calls");
let route = Route::new(req, remote_addr);
let fut = route::set(&route, || self.filter.filter());
FilteredFuture {
future: fut,
route,
}
}
}
#[pin_project]
#[derive(Debug)]
pub struct FilteredFuture<F> {
#[pin]
future: F,
route: ::std::cell::RefCell<Route>,
}
impl<F> Future for FilteredFuture<F>
where
F: TryFuture,
{
type Output = Result<F::Ok, F::Error>;
#[inline]
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
debug_assert!(!route::is_set(), "nested route::set calls");
let pin = self.project();
let fut = pin.future;
route::set(&pin.route, || fut.try_poll(cx))
}
}
impl<F> IntoWarpService for FilteredService<F>
where
F: Filter + Send + Sync + 'static,
F::Extract: Reply,
F::Error: IsReject,
{
type Service = FilteredService<F>;
#[inline]
fn into_warp_service(self) -> Self::Service {
self
}
}
impl<F> IntoWarpService for F
where
F: Filter + Send + Sync + 'static,
F::Extract: Reply,
F::Error: IsReject,
{
type Service = FilteredService<F>;
#[inline]
fn into_warp_service(self) -> Self::Service {
FilteredService { filter: self }
}
}