dptree/handler/
endpoint.rs1use crate::{
2 description,
3 di::Injectable,
4 from_fn_with_description,
5 send::{MaybeSend, MaybeSync},
6 Handler, HandlerDescription, HandlerSignature,
7};
8
9use std::{collections::BTreeSet, ops::ControlFlow, sync::Arc};
10
11use futures::FutureExt;
12
13#[must_use]
19#[track_caller]
20pub fn endpoint<'a, F, Output, FnArgs, Descr>(f: F) -> Endpoint<'a, Output, Descr>
21where
22 F: Injectable<Output, FnArgs> + MaybeSend + MaybeSync + 'a,
23 Output: 'static,
24 Descr: HandlerDescription,
25{
26 let f = Arc::new(f);
27
28 from_fn_with_description(
29 Descr::endpoint(),
30 move |x, _cont| {
31 let f = Arc::clone(&f);
32 async move {
33 let f = f.inject(&x);
34 f().map(ControlFlow::Break).await
35 }
36 },
37 HandlerSignature::Other {
38 obligations: F::obligations(),
39 guaranteed_outcomes: BTreeSet::default(),
40 conditional_outcomes: BTreeSet::default(),
41 continues: false,
42 },
43 )
44}
45
46pub type Endpoint<'a, Output, Descr = description::Unspecified> = Handler<'a, Output, Descr>;
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52 use crate::{deps, help_inference};
53
54 crate::cross_test! {
55 async fn test_endpoint() {
56 let input = 123;
57 let output = 7;
58
59 let result = help_inference(endpoint(move |num: i32| async move {
60 assert_eq!(num, input);
61 output
62 }))
63 .dispatch(deps![input])
64 .await;
65
66 let result = match result {
67 ControlFlow::Break(b) => b,
68 _ => panic!("Unexpected: handler return ControlFlow::Break"),
69 };
70 assert_eq!(result, output);
71 }
72 }
73}