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
use crate::{description, di::Injectable, from_fn_with_description, Handler, HandlerDescription};
use futures::FutureExt;
use std::{ops::ControlFlow, sync::Arc};
#[must_use]
#[track_caller]
pub fn endpoint<'a, F, Input, Output, FnArgs, Descr>(f: F) -> Endpoint<'a, Input, Output, Descr>
where
F: Injectable<Input, Output, FnArgs> + Send + Sync + 'a,
Input: Send + 'a,
Output: 'a,
Descr: HandlerDescription,
{
let f = Arc::new(f);
from_fn_with_description(Descr::endpoint(), move |x, _cont| {
let f = Arc::clone(&f);
async move {
let f = f.inject(&x);
f().map(ControlFlow::Break).await
}
})
}
pub type Endpoint<'a, Input, Output, Descr = description::Unspecified> =
Handler<'a, Input, Output, Descr>;
#[cfg(test)]
mod tests {
use super::*;
use crate::{deps, help_inference};
#[tokio::test]
async fn test_endpoint() {
let input = 123;
let output = 7;
let result = help_inference(endpoint(move |num: i32| async move {
assert_eq!(num, input);
output
}))
.dispatch(deps![input])
.await;
let result = match result {
ControlFlow::Break(b) => b,
_ => panic!("Unexpected: handler return ControlFlow::Break"),
};
assert_eq!(result, output);
}
}