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
use crate::context::JobContext;
use crate::job::Job;
use crate::request::JobRequest;
use crate::response::IntoResponse;
use futures::future::BoxFuture;
use std::fmt::{self};
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use tower::{BoxError, Service};
pub fn job_fn<T>(f: T) -> JobFn<T> {
JobFn { f }
}
#[derive(Copy, Clone)]
pub struct JobFn<T> {
f: T,
}
impl<T> fmt::Debug for JobFn<T>
where
T: Job,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("JobFn")
.field("f", &format_args!("{}", T::NAME))
.finish()
}
}
pin_project_lite::pin_project! {
pub struct JobFnHttpFuture<F> {
#[pin]
future: F,
}
}
impl<F, Res> Future for JobFnHttpFuture<F>
where
F: Future<Output = Res> + 'static,
{
type Output = F::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let slf = self.project();
slf.future.poll(cx)
}
}
impl<T, F, Res: 'static, Request, E: 'static, R: 'static> Service<JobRequest<Request>> for JobFn<T>
where
Request: 'static,
T: Fn(Request, JobContext) -> F,
Res: IntoResponse<Result = std::result::Result<R, E>> + 'static,
F: Future<Output = Res> + 'static + Send,
Request: Job,
E: Into<BoxError> + Send + Sync,
{
type Response = R;
type Error = E;
type Future = JobFnHttpFuture<BoxFuture<'static, std::result::Result<R, E>>>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, job: JobRequest<Request>) -> Self::Future {
let fut = (self.f)(job.job, job.context);
JobFnHttpFuture {
future: Box::pin(async { fut.await.into_response() }),
}
}
}