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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use futures::{Future, IntoFuture, Poll};
use futures::stream::Stream;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::{fmt, io, mem};
use std::error::Error;
use std::net::{SocketAddr, ToSocketAddrs};
#[allow(unreachable_code)]
pub struct Never(!);
impl fmt::Debug for Never {
fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
self.0
}
}
impl Error for Never {
fn description(&self) -> &str {
self.0
}
}
impl fmt::Display for Never {
fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
self.0
}
}
impl Future for Never {
type Item = Never;
type Error = Never;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.0
}
}
impl Stream for Never {
type Item = Never;
type Error = Never;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
self.0
}
}
impl Serialize for Never {
fn serialize<S>(&self, _: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
self.0
}
}
impl Deserialize for Never {
fn deserialize<D>(_: D) -> Result<Self, D::Error>
where D: Deserializer
{
panic!("Never cannot be instantiated!");
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Message(pub String);
impl Error for Message {
fn description(&self) -> &str {
&self.0
}
}
impl fmt::Display for Message {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl<S: Into<String>> From<S> for Message {
fn from(s: S) -> Self {
Message(s.into())
}
}
pub trait FirstSocketAddr: ToSocketAddrs {
fn try_first_socket_addr(&self) -> io::Result<SocketAddr> {
if let Some(a) = self.to_socket_addrs()?.next() {
Ok(a)
} else {
Err(io::Error::new(io::ErrorKind::AddrNotAvailable,
"`ToSocketAddrs::to_socket_addrs` returned an empty iterator."))
}
}
fn first_socket_addr(&self) -> SocketAddr {
self.try_first_socket_addr().unwrap()
}
}
impl<A: ToSocketAddrs> FirstSocketAddr for A {}
pub fn lazy<F, A, R>(f: F, args: A) -> Lazy<F, A, R>
where F: FnOnce(A) -> R,
R: IntoFuture
{
Lazy {
inner: _Lazy::First(f, args),
}
}
#[derive(Debug)]
#[must_use = "futures do nothing unless polled"]
pub struct Lazy<F, A, R: IntoFuture> {
inner: _Lazy<F, A, R::Future>,
}
#[derive(Debug)]
enum _Lazy<F, A, R> {
First(F, A),
Second(R),
Moved,
}
impl<F, A, R> Lazy<F, A, R>
where F: FnOnce(A) -> R,
R: IntoFuture,
{
fn get(&mut self) -> &mut R::Future {
match self.inner {
_Lazy::First(..) => {}
_Lazy::Second(ref mut f) => return f,
_Lazy::Moved => panic!(),
}
match mem::replace(&mut self.inner, _Lazy::Moved) {
_Lazy::First(f, args) => self.inner = _Lazy::Second(f(args).into_future()),
_ => panic!(),
}
match self.inner {
_Lazy::Second(ref mut f) => f,
_ => panic!(),
}
}
}
impl<F, A, R> Future for Lazy<F, A, R>
where F: FnOnce(A) -> R,
R: IntoFuture,
{
type Item = R::Item;
type Error = R::Error;
fn poll(&mut self) -> Poll<R::Item, R::Error> {
self.get().poll()
}
}