Created
October 28, 2015 20:28
-
-
Save seanmonstar/c0782db4a6557e7fe2a3 to your computer and use it in GitHub Desktop.
proposed async io for hyper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn handle(req: Request, res: Response) { | |
// assuming `POST /echo` | |
req.pipe(res).on_error(|err| error!("pipe error: {}", err)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn handle(req: Request, _res: Response) { | |
req.read(move |result| { | |
Ok((data, req)) => { | |
println!("read: {}", from_utf8(data)); | |
// not necessarily eof yet | |
}, | |
Err(e) => { | |
println!("req read error: {}", e); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn handle(req: Request, res: Response) { | |
let mut buf = vec![]; | |
req.stream(move |result| { | |
Ok(Some(bytes)) => buf.extend(bytes), | |
Ok(None) => { | |
res.write_all(buf, |result| if let Err(e) = result { | |
println!("write_all err: {}", e); | |
}); | |
}, | |
Err(e) => println!("stream err: {}", e) | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn handle(req: Request, res: Response) { | |
req.stream(move |result| { | |
match result { | |
Ok(Some(bytes)) => println!("read: {}", from_utf8(bytes).unwrap_or("")), | |
Ok(None) => println!("eof"), | |
Err(e) => println!("read error: {}", e) | |
} | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn handle(req: Request, res: Response) { | |
res.write(move |transport| { | |
use std::io::Write; | |
try!(transport.write("Hello World")); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn handle(req: Request, res: Response) { | |
res.write_all("Hello World!", |result| { | |
match result { | |
Ok(response) => println!("write_all completed"), | |
Err(e) => println!("write_all error: {}", e) | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment