finchers/endpoint/
boxed.rs

1use futures::Future;
2use std::fmt;
3
4use common::Tuple;
5use endpoint::{ApplyContext, ApplyResult, Endpoint, SendEndpoint};
6use error::Error;
7
8trait FutureObjEndpoint<'a>: 'a {
9    type Output: Tuple;
10
11    fn apply_obj(
12        &'a self,
13        ecx: &mut ApplyContext<'_>,
14    ) -> ApplyResult<Box<dyn Future<Item = Self::Output, Error = Error> + Send + 'a>>;
15}
16
17impl<'e, E: SendEndpoint<'e>> FutureObjEndpoint<'e> for E {
18    type Output = E::Output;
19
20    #[inline(always)]
21    fn apply_obj(
22        &'e self,
23        ecx: &mut ApplyContext<'_>,
24    ) -> ApplyResult<Box<dyn Future<Item = Self::Output, Error = Error> + Send + 'e>> {
25        let future = self.apply_send(ecx)?;
26        Ok(Box::new(future))
27    }
28}
29
30#[allow(missing_docs)]
31pub struct EndpointObj<T: Tuple + 'static> {
32    inner: Box<dyn for<'a> FutureObjEndpoint<'a, Output = T> + Send + Sync + 'static>,
33}
34
35impl<T: Tuple + 'static> EndpointObj<T> {
36    #[allow(missing_docs)]
37    pub fn new<E>(endpoint: E) -> EndpointObj<T>
38    where
39        for<'a> E: SendEndpoint<'a, Output = T> + Send + Sync + 'static,
40    {
41        EndpointObj {
42            inner: Box::new(endpoint),
43        }
44    }
45}
46
47impl<T: Tuple + 'static> fmt::Debug for EndpointObj<T> {
48    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
49        formatter.debug_struct("EndpointObj").finish()
50    }
51}
52
53impl<'e, T: Tuple + 'static> Endpoint<'e> for EndpointObj<T> {
54    type Output = T;
55    type Future = Box<dyn Future<Item = Self::Output, Error = Error> + Send + 'e>;
56
57    #[inline(always)]
58    fn apply(&'e self, ecx: &mut ApplyContext<'_>) -> ApplyResult<Self::Future> {
59        self.inner.apply_obj(ecx)
60    }
61}
62
63// ==== BoxedLocal ====
64
65trait LocalFutureObjEndpoint<'a>: 'a {
66    type Output: Tuple;
67
68    fn apply_local_obj(
69        &'a self,
70        ecx: &mut ApplyContext<'_>,
71    ) -> ApplyResult<Box<dyn Future<Item = Self::Output, Error = Error> + 'a>>;
72}
73
74impl<'e, E: Endpoint<'e>> LocalFutureObjEndpoint<'e> for E {
75    type Output = E::Output;
76
77    #[inline(always)]
78    fn apply_local_obj(
79        &'e self,
80        ecx: &mut ApplyContext<'_>,
81    ) -> ApplyResult<Box<dyn Future<Item = Self::Output, Error = Error> + 'e>> {
82        let future = self.apply(ecx)?;
83        Ok(Box::new(future))
84    }
85}
86
87#[allow(missing_docs)]
88pub struct LocalEndpointObj<T: Tuple + 'static> {
89    inner: Box<dyn for<'a> LocalFutureObjEndpoint<'a, Output = T> + 'static>,
90}
91
92impl<T: Tuple + 'static> LocalEndpointObj<T> {
93    #[allow(missing_docs)]
94    pub fn new<E>(endpoint: E) -> LocalEndpointObj<T>
95    where
96        for<'a> E: Endpoint<'a, Output = T> + Send + Sync + 'static,
97    {
98        LocalEndpointObj {
99            inner: Box::new(endpoint),
100        }
101    }
102}
103
104impl<T: Tuple + 'static> fmt::Debug for LocalEndpointObj<T> {
105    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
106        formatter.debug_struct("LocalEndpointObj").finish()
107    }
108}
109
110impl<'e, T: Tuple + 'static> Endpoint<'e> for LocalEndpointObj<T> {
111    type Output = T;
112    type Future = Box<dyn Future<Item = Self::Output, Error = Error> + 'e>;
113
114    #[inline(always)]
115    fn apply(&'e self, ecx: &mut ApplyContext<'_>) -> ApplyResult<Self::Future> {
116        self.inner.apply_local_obj(ecx)
117    }
118}