Skip to content

Commit fef04d2

Browse files
committed
refactor(server): make with_listener a free function
Allow a Server to operate without requiring the entire Server struct to move into the with_listener function (instead only the handler function needs to move). This, allows other members to not move, or move separately, which will be needed for the next commit. See hyperium#471
1 parent 1a076d1 commit fef04d2

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/server/mod.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl<'a, H: Handler + 'static> Server<'a, H, HttpListener> {
103103
Some((cert, key)) => HttpListener::https(addr, cert, key),
104104
None => HttpListener::http(addr)
105105
});
106-
self.with_listener(listener, threads)
106+
with_listener(self.handler, listener, threads)
107107
}
108108

109109
/// Binds to a socket and starts handling connections.
@@ -117,23 +117,27 @@ H: Handler + 'static,
117117
L: NetworkListener<Stream=S> + Send + 'static,
118118
S: NetworkStream + Clone + Send> Server<'a, H, L> {
119119
/// Creates a new server that will handle `HttpStream`s.
120-
pub fn with_listener(self, mut listener: L, threads: usize) -> HttpResult<Listening> {
121-
let socket = try!(listener.local_addr());
122-
let handler = self.handler;
120+
pub fn with_listener(self, listener: L, threads: usize) -> HttpResult<Listening> {
121+
with_listener(self.handler, listener, threads)
122+
}
123+
}
123124

124-
debug!("threads = {:?}", threads);
125-
let pool = ListenerPool::new(listener.clone());
126-
let work = move |mut stream| handle_connection(&mut stream, &handler);
125+
fn with_listener<H, L>(handler: H, mut listener: L, threads: usize) -> HttpResult<Listening>
126+
where H: Handler + 'static,
127+
L: NetworkListener + Send + 'static {
128+
let socket = try!(listener.local_addr());
127129

128-
let guard = thread::spawn(move || pool.accept(work, threads));
130+
debug!("threads = {:?}", threads);
131+
let pool = ListenerPool::new(listener.clone());
132+
let work = move |mut stream| handle_connection(&mut stream, &handler);
129133

130-
Ok(Listening {
131-
_guard: Some(guard),
132-
socket: socket,
133-
})
134-
}
135-
}
134+
let guard = thread::spawn(move || pool.accept(work, threads));
136135

136+
Ok(Listening {
137+
_guard: Some(guard),
138+
socket: socket,
139+
})
140+
}
137141

138142
fn handle_connection<'h, S, H>(mut stream: &mut S, handler: &'h H)
139143
where S: NetworkStream + Clone, H: Handler {

0 commit comments

Comments
 (0)