Use trait objects and dynamic dispatch to abstract over NetworkStream

Server and client benchmarks show that this makes very little difference
in performance and using dynamic dispatch here is significantly more ergonomic.

This also bounds NetworkStream with Send to prevent incorrect implementations.

Allows the implementation of mock streams for testing and flexibility.

Fixes #5
This commit is contained in:
Jonathan Reem
2014-09-09 14:28:06 -07:00
parent 0285fc2acc
commit 76a58940d8
5 changed files with 43 additions and 31 deletions

View File

@@ -13,7 +13,7 @@ use hyper::header::common::ContentLength;
use hyper::net::{HttpStream, HttpAcceptor};
trait ConcurrentHandler: Send + Sync {
fn handle(&self, req: Request, res: Response<Fresh, HttpStream>);
fn handle(&self, req: Request, res: Response<Fresh>);
}
struct Concurrent<H: ConcurrentHandler> { handler: Arc<H> }
@@ -39,7 +39,7 @@ macro_rules! try_abort(
struct Echo;
impl ConcurrentHandler for Echo {
fn handle(&self, mut req: Request, mut res: Response<Fresh, HttpStream>) {
fn handle(&self, mut req: Request, mut res: Response<Fresh>) {
match req.uri {
hyper::uri::AbsolutePath(ref path) => match (&req.method, path.as_slice()) {
(&Get, "/") | (&Get, "/echo") => {