use super::core::from_fn_with_flow;
use crate::{
description,
di::Injectable,
send::{MaybeSend, MaybeSync},
Handler, HandlerDescription, HandlerSignature,
};
use std::{collections::BTreeSet, ops::ControlFlow, sync::Arc};
use futures::FutureExt;
#[must_use]
#[track_caller]
pub fn endpoint<'a, F, Output, FnArgs, Descr>(f: F) -> Endpoint<'a, Output, Descr>
where
F: Injectable<Output, FnArgs> + MaybeSend + MaybeSync + 'a,
Output: 'static,
Descr: HandlerDescription,
{
let f = Arc::new(f);
from_fn_with_flow(
Descr::endpoint(),
move |x, _cont| {
let f = Arc::clone(&f);
async move {
let f = f.inject(&x);
f().map(ControlFlow::Break).await
}
},
HandlerSignature::Other {
obligations: F::obligations(),
guaranteed_outcomes: BTreeSet::default(),
conditional_outcomes: BTreeSet::default(),
continues: false,
},
false,
)
}
pub type Endpoint<'a, Output, Descr = description::Unspecified> = Handler<'a, Output, Descr>;
#[cfg(test)]
mod tests {
use super::*;
use crate::{deps, help_inference};
crate::cross_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);
}
}
}