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,
Expand description
Constructs a handler that has no further handlers in a chain.
An endpoint is a handler that always breaks handler execution after its completion. So, you can use it when your chain of responsibility must end up, and handle an incoming event.
Examples found in repository?
More examples
examples/storage.rs (lines 13-16)
≺ ≻
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
async fn main() {
fn assert_num_string_handler(
expected_num: u32,
expected_string: &'static str,
) -> Endpoint<'static, Store, ()> {
// The handler requires `u32` and `String` types from the input storage.
dptree::endpoint(move |num: u32, string: String| async move {
assert_eq!(num, expected_num);
assert_eq!(string, expected_string);
})
}
// Init storage with string and num
let store = Arc::new(dptree::deps![10u32, "Hello".to_owned()]);
let str_num_handler = assert_num_string_handler(10u32, "Hello");
str_num_handler.dispatch(store.clone()).await;
// This will cause a panic because we do not store `Ipv4Addr` in out store.
let handle = tokio::spawn(async move {
let ip_handler: Endpoint<_, _> = dptree::endpoint(|ip: Ipv4Addr| async move {
assert_eq!(ip, Ipv4Addr::new(0, 0, 0, 0));
});
ip_handler.dispatch(store.clone()).await;
});
let result = handle.await;
assert!(result.is_err())
}