summaryrefslogtreecommitdiffstats
path: root/src/bin/echo.rs
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2016-08-17 09:29:05 -0700
committerAlex Crichton <alex@alexcrichton.com>2016-08-17 18:41:34 -0700
commitd0b911189c9a879fd3861b7fc84d4f03723208de (patch)
tree1b5f5cfde234bfa7fa1fd6607adeef4fceed7e64 /src/bin/echo.rs
parent293d1041770384c9eeb34ac7d97214feaf3b88c3 (diff)
Re-work I/O
* Auto-register interest whenever we see WouldBlock * Remove implementations of `Stream<Item=Ready>`, no longer needed * Add explicit `poll_{read,write}` methods, if needed * Remove all I/O streams, libstd ones suffice * Update all I/O futures
Diffstat (limited to 'src/bin/echo.rs')
-rw-r--r--src/bin/echo.rs34
1 files changed, 26 insertions, 8 deletions
diff --git a/src/bin/echo.rs b/src/bin/echo.rs
index a6fb1c32..6bc76b7c 100644
--- a/src/bin/echo.rs
+++ b/src/bin/echo.rs
@@ -5,11 +5,14 @@ extern crate futures_io;
extern crate futures_mio;
use std::env;
+use std::io::{self, Read, Write};
use std::net::SocketAddr;
+use std::sync::Arc;
use futures::Future;
-use futures_io::{copy, TaskIo};
use futures::stream::Stream;
+use futures_io::copy;
+use futures_mio::TcpStream;
fn main() {
let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string());
@@ -27,17 +30,14 @@ fn main() {
// Pull out the stream of incoming connections and then for each new
// one spin up a new task copying data. We put the `socket` into a
- // `TaskIo` structure which then allows us to `split` it into the read
- // and write halves of the socket.
+ // `Arc` structure which then allows us to share it across the
+ // read/write halves with a small shim.
//
// Finally we use the `io::copy` future to copy all data from the
// reading half onto the writing half.
socket.incoming().for_each(|(socket, addr)| {
- let io = TaskIo::new(socket);
- let pair = io.map(|io| io.split());
- let amt = pair.and_then(|(reader, writer)| {
- copy(reader, writer)
- });
+ let socket = Arc::new(socket);
+ let amt = copy(SocketIo(socket.clone()), SocketIo(socket));
// Once all that is done we print out how much we wrote, and then
// critically we *forget* this future which allows it to run
@@ -51,3 +51,21 @@ fn main() {
});
l.run(done).unwrap();
}
+
+struct SocketIo(Arc<TcpStream>);
+
+impl Read for SocketIo {
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ (&*self.0).read(buf)
+ }
+}
+
+impl Write for SocketIo {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ (&*self.0).write(buf)
+ }
+
+ fn flush(&mut self) -> io::Result<()> {
+ (&*self.0).flush()
+ }
+}