1use either::Either;
13use futures::{Async, Future, IntoFuture};
14
15use error::Error;
16use input::{Input, RequestBody};
17use never::Never;
18use poll::{Poll, PollResult};
19
20#[derive(Debug)]
22pub struct Context<'a> {
23 input: &'a Input,
24 body: &'a mut Option<RequestBody>,
25 }
27
28impl<'a> Context<'a> {
29 #[inline]
31 pub fn new(input: &'a Input, body: &'a mut Option<RequestBody>) -> Context<'a> {
32 Context { input, body }
33 }
34
35 #[inline]
37 pub fn input(&self) -> &Input {
38 self.input
39 }
40
41 #[inline]
43 pub fn body(&mut self) -> Option<RequestBody> {
44 self.body.take()
45 }
46}
47
48pub trait Task: Send {
52 type Output;
54
55 fn poll_task(&mut self, cx: &mut Context) -> PollResult<Self::Output, Error>;
57}
58
59impl<L, R> Task for Either<L, R>
60where
61 L: Task,
62 R: Task<Output = L::Output>,
63{
64 type Output = L::Output;
65
66 #[inline(always)]
67 fn poll_task(&mut self, cx: &mut Context) -> PollResult<Self::Output, Error> {
68 match *self {
69 Either::Left(ref mut t) => t.poll_task(cx),
70 Either::Right(ref mut t) => t.poll_task(cx),
71 }
72 }
73}
74
75pub trait IntoTask {
77 type Output;
79
80 type Task: Task<Output = Self::Output>;
82
83 fn into_task(self) -> Self::Task;
85}
86
87impl<F> IntoTask for F
89where
90 F: IntoFuture,
91 F::Future: Send,
92{
93 type Output = Result<F::Item, F::Error>;
94 type Task = TaskFuture<F::Future>;
95
96 #[inline(always)]
97 fn into_task(self) -> Self::Task {
98 future(self)
99 }
100}
101
102#[derive(Debug)]
104pub struct TaskFuture<F>(F);
105
106impl<F> From<F> for TaskFuture<F>
107where
108 F: Future + Send,
109{
110 fn from(fut: F) -> Self {
111 TaskFuture(fut)
112 }
113}
114
115impl<F> Task for TaskFuture<F>
116where
117 F: Future + Send,
118{
119 type Output = Result<F::Item, F::Error>;
120
121 #[inline(always)]
122 fn poll_task(&mut self, _: &mut Context) -> PollResult<Self::Output, Error> {
123 match Future::poll(&mut self.0) {
124 Ok(Async::Ready(ready)) => Poll::Ready(Ok(Ok(ready))),
125 Ok(Async::NotReady) => Poll::Pending,
126 Err(err) => Poll::Ready(Ok(Err(err))),
127 }
128 }
129}
130
131pub fn future<F>(future: F) -> TaskFuture<F::Future>
133where
134 F: IntoFuture,
135 F::Future: Send,
136{
137 TaskFuture::from(IntoFuture::into_future(future))
138}
139
140#[derive(Debug)]
142pub struct Ready<T>(Option<T>);
143
144impl<T: Send> From<T> for Ready<T> {
145 fn from(val: T) -> Self {
146 Ready(Some(val))
147 }
148}
149
150impl<T: Send> Task for Ready<T> {
151 type Output = T;
152
153 #[inline(always)]
154 fn poll_task(&mut self, _: &mut Context) -> PollResult<Self::Output, Error> {
155 let val = self.0.take().expect("The task cannot resolve twice");
156 Poll::Ready(Ok(val))
157 }
158}
159
160pub fn ready<T: Send>(val: T) -> Ready<T> {
162 Ready::from(val)
163}
164
165#[derive(Debug)]
167pub struct Abort<E> {
168 cause: Option<E>,
169}
170
171impl<E> From<E> for Abort<E>
172where
173 E: Into<Error> + Send,
174{
175 fn from(cause: E) -> Self {
176 Abort { cause: Some(cause) }
177 }
178}
179
180impl<E> Task for Abort<E>
181where
182 E: Into<Error> + Send,
183{
184 type Output = Never;
185
186 #[inline(always)]
187 fn poll_task(&mut self, _: &mut Context) -> PollResult<Self::Output, Error> {
188 let cause = self.cause.take().expect("The task cannot reject twice");
189 Poll::Ready(Err(Into::into(cause)))
190 }
191}
192
193pub fn abort<E>(cause: E) -> Abort<E>
195where
196 E: Into<Error> + Send,
197{
198 Abort::from(cause)
199}