summaryrefslogtreecommitdiffstats
path: root/examples/echo.rs
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2016-09-07 16:11:19 -0700
committerAlex Crichton <alex@alexcrichton.com>2016-09-07 22:12:41 -0700
commit66cff8e84b0bdbdc716fe1cd715363aa7e79aefa (patch)
tree6b428ef251d49ba9764e27f3482b0f7d68cc3159 /examples/echo.rs
parente60002b653594c0a024733d1db0ce8d680e4f808 (diff)
Swap Handle/Pinned
* Handle -> Remote * Pinned -> Handle All APIs now take a `&Handle` by default and in general can return an immediate `io::Result` instead of an `IoFuture`. This reflects how most usage will likely be done through handles rather than remotes, and also all previous functionality can be recovered with a `oneshot` plus `Remote::spawn`. Closes #15
Diffstat (limited to 'examples/echo.rs')
-rw-r--r--examples/echo.rs54
1 files changed, 26 insertions, 28 deletions
diff --git a/examples/echo.rs b/examples/echo.rs
index 4f46e5fc..cf441e5d 100644
--- a/examples/echo.rs
+++ b/examples/echo.rs
@@ -20,36 +20,34 @@ fn main() {
// Create the event loop that will drive this server
let mut l = Core::new().unwrap();
- let pin = l.pin();
+ let handle = l.handle();
// Create a TCP listener which will listen for incoming connections
- let server = TcpListener::bind(&addr, &l.handle());
-
- let done = server.and_then(move |socket| {
- // Once we've got the TCP listener, inform that we have it
- println!("Listening on: {}", addr);
-
- // Pull out the stream of incoming connections and then for each new
- // one spin up a new task copying data.
- //
- // We use the `io::copy` future to copy all data from the
- // reading half onto the writing half.
- socket.incoming().for_each(move |(socket, addr)| {
- let pair = futures::lazy(|| futures::finished(socket.split()));
- let amt = pair.and_then(|(reader, writer)| copy(reader, writer));
-
- // Once all that is done we print out how much we wrote, and then
- // critically we *spawn* this future which allows it to run
- // concurrently with other connections.
- let msg = amt.map(move |amt| {
- println!("wrote {} bytes to {}", amt, addr)
- }).map_err(|e| {
- panic!("error: {}", e);
- });
- pin.spawn(msg);
-
- Ok(())
- })
+ let socket = TcpListener::bind(&addr, &l.handle()).unwrap();
+
+ // Once we've got the TCP listener, inform that we have it
+ println!("Listening on: {}", addr);
+
+ // Pull out the stream of incoming connections and then for each new
+ // one spin up a new task copying data.
+ //
+ // We use the `io::copy` future to copy all data from the
+ // reading half onto the writing half.
+ let done = socket.incoming().for_each(move |(socket, addr)| {
+ let pair = futures::lazy(|| futures::finished(socket.split()));
+ let amt = pair.and_then(|(reader, writer)| copy(reader, writer));
+
+ // Once all that is done we print out how much we wrote, and then
+ // critically we *spawn* this future which allows it to run
+ // concurrently with other connections.
+ let msg = amt.map(move |amt| {
+ println!("wrote {} bytes to {}", amt, addr)
+ }).map_err(|e| {
+ panic!("error: {}", e);
+ });
+ handle.spawn(msg);
+
+ Ok(())
});
l.run(done).unwrap();
}