finchers_core/endpoint/
apply.rs

1use super::{Context, Endpoint};
2use error::Error;
3use input::{Input, RequestBody};
4use poll::Poll;
5use task::{self, Task};
6
7/// Create an asynchronous computation for handling an HTTP request.
8pub fn apply_request<E>(endpoint: &E, input: &Input, body: RequestBody) -> ApplyRequest<E::Task>
9where
10    E: Endpoint + ?Sized,
11{
12    let in_flight = endpoint.apply(&mut Context::new(input));
13    ApplyRequest {
14        in_flight,
15        body: Some(body),
16    }
17}
18
19/// An asynchronous computation created by the endpoint.
20///
21/// Typically, this value is wrapped by a type which contains an instance of `Input`.
22#[derive(Debug)]
23pub struct ApplyRequest<T> {
24    in_flight: Option<T>,
25    body: Option<RequestBody>,
26}
27
28impl<T: Task> ApplyRequest<T> {
29    /// Poll the inner `Task` and return its output if available.
30    pub fn poll_ready(&mut self, input: &Input) -> Poll<Result<T::Output, Error>> {
31        match self.in_flight {
32            Some(ref mut f) => {
33                let mut cx = task::Context::new(input, &mut self.body);
34                f.poll_task(&mut cx)
35            }
36            None => Poll::Ready(Err(Error::skipped())),
37        }
38    }
39}