From 89fcc96dd44bff0ba85432d96a3a8f5b20adc94e Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 5 Feb 2017 17:06:57 -0800 Subject: Migrate to using tokio-io Deprecate the existing `io` module in this crate entirely. More details coming soon! Closes #61 --- examples/connect.rs | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'examples/connect.rs') 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; +impl Decoder for Bytes { + type Item = BytesMut; + type Error = io::Error; - fn decode(&mut self, buf: &mut EasyBuf) -> io::Result> { + fn decode(&mut self, buf: &mut BytesMut) -> io::Result> { 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, buf: &mut Vec) -> io::Result<()> { - buf.extend(data); + fn decode_eof(&mut self, buf: &mut BytesMut) -> io::Result> { + self.decode(buf) + } +} + +impl Encoder for Bytes { + type Item = Vec; + type Error = io::Error; + + fn encode(&mut self, data: Vec, buf: &mut BytesMut) -> io::Result<()> { + buf.put(&data[..]); Ok(()) } } -- cgit v1.2.3