1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use error;
use response::Serializer;
use routing::{RouteMatch, RouteSet};
use util::BufStream;
use bytes::Buf;
use futures::{Future, Poll};
use futures::future::FutureResult;
use http;
use std::marker::PhantomData;
pub trait Resource: Clone {
type Destination: Clone + Send + Sync + 'static;
type RequestBody: BufStream;
type Buf: Buf;
type Body: BufStream<Item = Self::Buf, Error = ::Error>;
type Future: ResourceFuture<Body = Self::Body>;
fn dispatch(
&mut self,
destination: Self::Destination,
route_match: &RouteMatch,
body: Self::RequestBody,
) -> Self::Future;
}
pub trait ResourceFuture {
type Body;
fn poll_response(&mut self, request: &http::Request<()>)
-> Poll<http::Response<Self::Body>, ::Error>;
}
pub trait IntoResource<S, RequestBody>
where S: Serializer,
RequestBody: BufStream,
{
type Destination: Clone + Send + Sync + 'static;
type Resource: Resource<Destination = Self::Destination,
RequestBody = RequestBody>;
fn routes(&self) -> RouteSet<Self::Destination>;
fn into_resource(self, serializer: S) -> Self::Resource;
}
impl<T, B> ResourceFuture for T
where
T: Future<Item = http::Response<B>, Error = ::Error>
{
type Body = B;
fn poll_response(&mut self, _: &http::Request<()>) -> Poll<T::Item, ::Error> {
self.poll()
}
}
#[derive(Debug)]
pub struct Unit<B> {
_p: PhantomData<B>,
}
impl<B> Unit<B> {
pub fn new() -> Self {
Unit { _p: PhantomData }
}
}
impl<B> Resource for Unit<B>
where B: BufStream,
{
type Destination = ();
type RequestBody = B;
type Buf = <Self::Body as BufStream>::Item;
type Body = error::Map<String>;
type Future = FutureResult<http::Response<Self::Body>, ::Error>;
fn dispatch(&mut self, _: (), _: &RouteMatch, _: Self::RequestBody) -> Self::Future {
unreachable!();
}
}
impl<B> Clone for Unit<B> {
fn clone(&self) -> Self {
Unit::new()
}
}