diff options
author | Alex Crichton <alex@alexcrichton.com> | 2017-02-05 17:06:57 -0800 |
---|---|---|
committer | Alex Crichton <alex@alexcrichton.com> | 2017-03-15 09:46:54 -0700 |
commit | 89fcc96dd44bff0ba85432d96a3a8f5b20adc94e (patch) | |
tree | fe257e453157874d6584b84b270ae462b9b88e20 /examples | |
parent | 8fecf98aef1bb2f4f37303c44b2a51126b9c54ff (diff) |
Migrate to using tokio-io
Deprecate the existing `io` module in this crate entirely.
More details coming soon!
Closes #61
Diffstat (limited to 'examples')
-rw-r--r-- | examples/chat.rs | 11 | ||||
-rw-r--r-- | examples/connect.rs | 35 | ||||
-rw-r--r-- | examples/echo.rs | 6 | ||||
-rw-r--r-- | examples/hello.rs | 5 | ||||
-rw-r--r-- | examples/sink.rs | 5 |
5 files changed, 40 insertions, 22 deletions
diff --git a/examples/chat.rs b/examples/chat.rs index e7b472dd..267e0aa6 100644 --- a/examples/chat.rs +++ b/examples/chat.rs @@ -17,8 +17,9 @@ //! connected clients they'll all join the same room and see everyone else's //! messages. -extern crate tokio_core; extern crate futures; +extern crate tokio_core; +extern crate tokio_io; use std::collections::HashMap; use std::rc::Rc; @@ -27,12 +28,12 @@ use std::iter; use std::env; use std::io::{Error, ErrorKind, BufReader}; +use futures::Future; +use futures::stream::{self, Stream}; use tokio_core::net::TcpListener; use tokio_core::reactor::Core; -use tokio_core::io::{self, Io}; - -use futures::stream::{self, Stream}; -use futures::Future; +use tokio_io::io; +use tokio_io::AsyncRead; fn main() { let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string()); diff --git a/examples/connect.rs b/examples/connect.rs index 93b6d5d4..a167e006 100644 --- a/examples/connect.rs +++ b/examples/connect.rs @@ -10,17 +10,21 @@ extern crate futures; extern crate tokio_core; +extern crate tokio_io; +extern crate bytes; use std::env; use std::io::{self, Read, Write}; use std::net::SocketAddr; use std::thread; -use futures::{Sink, Future, Stream}; +use bytes::{BufMut, BytesMut}; use futures::sync::mpsc; -use tokio_core::reactor::Core; -use tokio_core::io::{Io, EasyBuf, Codec}; +use futures::{Sink, Future, Stream}; use tokio_core::net::TcpStream; +use tokio_core::reactor::Core; +use tokio_io::AsyncRead; +use tokio_io::codec::{Encoder, Decoder}; fn main() { // Parse what address we're going to connect to @@ -63,7 +67,7 @@ fn main() { let (sink, stream) = stream.framed(Bytes).split(); let send_stdin = stdin_rx.forward(sink); let write_stdout = stream.for_each(move |buf| { - stdout.write_all(buf.as_slice()) + stdout.write_all(&buf) }); send_stdin.map(|_| ()) @@ -83,21 +87,30 @@ fn main() { /// data into the output location without looking at it. struct Bytes; -impl Codec for Bytes { - type In = EasyBuf; - type Out = Vec<u8>; +impl Decoder for Bytes { + type Item = BytesMut; + type Error = io::Error; - fn decode(&mut self, buf: &mut EasyBuf) -> io::Result<Option<EasyBuf>> { + fn decode(&mut self, buf: &mut BytesMut) -> io::Result<Option<BytesMut>> { if buf.len() > 0 { let len = buf.len(); - Ok(Some(buf.drain_to(len))) + Ok(Some(buf.split_to(len))) } else { Ok(None) } } - fn encode(&mut self, data: Vec<u8>, buf: &mut Vec<u8>) -> io::Result<()> { - buf.extend(data); + fn decode_eof(&mut self, buf: &mut BytesMut) -> io::Result<Option<BytesMut>> { + self.decode(buf) + } +} + +impl Encoder for Bytes { + type Item = Vec<u8>; + type Error = io::Error; + + fn encode(&mut self, data: Vec<u8>, buf: &mut BytesMut) -> io::Result<()> { + buf.put(&data[..]); Ok(()) } } diff --git a/examples/echo.rs b/examples/echo.rs index 45ced33e..80e73ea7 100644 --- a/examples/echo.rs +++ b/examples/echo.rs @@ -19,13 +19,15 @@ extern crate futures; extern crate tokio_core; +extern crate tokio_io; use std::env; use std::net::SocketAddr; use futures::Future; use futures::stream::Stream; -use tokio_core::io::{copy, Io}; +use tokio_io::AsyncRead; +use tokio_io::io::copy; use tokio_core::net::TcpListener; use tokio_core::reactor::Core; @@ -93,7 +95,7 @@ fn main() { // information. let msg = amt.then(move |result| { match result { - Ok(amt) => println!("wrote {} bytes to {}", amt, addr), + Ok((amt, _, _)) => println!("wrote {} bytes to {}", amt, addr), Err(e) => println!("error on {}: {}", addr, e), } diff --git a/examples/hello.rs b/examples/hello.rs index df83115b..a22517a5 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -11,9 +11,10 @@ //! //! You should see `Hello!` printed out and then the `nc` program will exit. +extern crate env_logger; extern crate futures; extern crate tokio_core; -extern crate env_logger; +extern crate tokio_io; use std::env; use std::net::SocketAddr; @@ -35,7 +36,7 @@ fn main() { let clients = listener.incoming(); let welcomes = clients.and_then(|(socket, _peer_addr)| { - tokio_core::io::write_all(socket, b"Hello!\n") + tokio_io::io::write_all(socket, b"Hello!\n") }); let server = welcomes.for_each(|(_socket, _welcome)| { Ok(()) diff --git a/examples/sink.rs b/examples/sink.rs index 2893adc6..2998b269 100644 --- a/examples/sink.rs +++ b/examples/sink.rs @@ -18,6 +18,7 @@ extern crate env_logger; extern crate futures; extern crate tokio_core; +extern crate tokio_io; use std::env; use std::iter; @@ -25,7 +26,7 @@ use std::net::SocketAddr; use futures::Future; use futures::stream::{self, Stream}; -use tokio_core::io::IoFuture; +use tokio_io::IoFuture; use tokio_core::net::{TcpListener, TcpStream}; use tokio_core::reactor::Core; @@ -51,6 +52,6 @@ fn write(socket: TcpStream) -> IoFuture<()> { static BUF: &'static [u8] = &[0; 64 * 1024]; let iter = iter::repeat(()).map(|()| Ok(())); stream::iter(iter).fold(socket, |socket, ()| { - tokio_core::io::write_all(socket, BUF).map(|(socket, _)| socket) + tokio_io::io::write_all(socket, BUF).map(|(socket, _)| socket) }).map(|_| ()).boxed() } |