finchers_ext/
lazy.rs

1#![allow(missing_docs)]
2
3use finchers_core::Endpoint;
4use finchers_core::endpoint::Context;
5use finchers_core::task;
6
7/// Create an endpoint which applies the given function to the incoming request and returns
8/// an immediate value of `T`.
9///
10/// NOTE: The trait bound of returned value from `F` should be replaced with `IntoTask`.
11pub fn lazy<F, T>(f: F) -> Lazy<F>
12where
13    F: Fn(&mut Context) -> Option<T> + Send + Sync,
14    T: Send,
15{
16    Lazy { f }
17}
18
19#[derive(Debug, Clone, Copy)]
20pub struct Lazy<F> {
21    f: F,
22}
23
24impl<F, T> Endpoint for Lazy<F>
25where
26    F: Fn(&mut Context) -> Option<T> + Send + Sync,
27    T: Send,
28{
29    type Output = T;
30    type Task = task::Ready<T>;
31
32    fn apply(&self, cx: &mut Context) -> Option<Self::Task> {
33        (self.f)(cx).map(task::ready)
34    }
35}