finchers_core/endpoint/
endpoint.rs

1use std::sync::Arc;
2
3use super::apply::ApplyRequest;
4use super::context::Context;
5use input::{Input, RequestBody};
6use task::Task;
7
8/// Trait representing an endpoint.
9pub trait Endpoint: Send + Sync {
10    /// The inner type associated with this endpoint.
11    type Output;
12
13    /// The type of value which will be returned from `apply`.
14    type Task: Task<Output = Self::Output>;
15
16    /// Perform checking the incoming HTTP request and returns
17    /// an instance of the associated task if matched.
18    fn apply(&self, cx: &mut Context) -> Option<Self::Task>;
19
20    /// Create an asyncrhonous computation from a request.
21    fn apply_request(&self, input: &Input, body: RequestBody) -> ApplyRequest<Self::Task> {
22        super::apply::apply_request(self, input, body)
23    }
24}
25
26impl<'a, E: Endpoint> Endpoint for &'a E {
27    type Output = E::Output;
28    type Task = E::Task;
29
30    fn apply(&self, cx: &mut Context) -> Option<Self::Task> {
31        (*self).apply(cx)
32    }
33}
34
35impl<E: Endpoint> Endpoint for Box<E> {
36    type Output = E::Output;
37    type Task = E::Task;
38
39    fn apply(&self, cx: &mut Context) -> Option<Self::Task> {
40        (**self).apply(cx)
41    }
42}
43
44impl<E: Endpoint> Endpoint for Arc<E> {
45    type Output = E::Output;
46    type Task = E::Task;
47
48    fn apply(&self, cx: &mut Context) -> Option<Self::Task> {
49        (**self).apply(cx)
50    }
51}
52
53/// Trait representing the transformation into an `Endpoint`.
54pub trait IntoEndpoint {
55    /// The inner type of associated `Endpoint`.
56    type Output;
57
58    /// The type of transformed `Endpoint`.
59    type Endpoint: Endpoint<Output = Self::Output>;
60
61    /// Consume itself and transform into an `Endpoint`.
62    fn into_endpoint(self) -> Self::Endpoint;
63}
64
65impl<E: Endpoint> IntoEndpoint for E {
66    type Output = E::Output;
67    type Endpoint = E;
68
69    #[inline]
70    fn into_endpoint(self) -> Self::Endpoint {
71        self
72    }
73}