mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-29 00:00:11 +02:00
Migrate to using tokio-io
Deprecate the existing `io` module in this crate entirely. More details coming soon! Closes #61
This commit is contained in:
+4
-3
@@ -1,6 +1,7 @@
|
||||
extern crate env_logger;
|
||||
extern crate futures;
|
||||
extern crate tokio_core;
|
||||
extern crate env_logger;
|
||||
extern crate tokio_io;
|
||||
|
||||
use std::net::TcpStream;
|
||||
use std::thread;
|
||||
@@ -8,7 +9,7 @@ use std::io::{Read, Write, BufReader, BufWriter};
|
||||
|
||||
use futures::Future;
|
||||
use futures::stream::Stream;
|
||||
use tokio_core::io::copy;
|
||||
use tokio_io::io::copy;
|
||||
use tokio_core::net::TcpListener;
|
||||
use tokio_core::reactor::Core;
|
||||
|
||||
@@ -55,7 +56,7 @@ fn echo_server() {
|
||||
copy(a, b)
|
||||
});
|
||||
|
||||
let amt = t!(l.run(copied));
|
||||
let (amt, _, _) = t!(l.run(copied));
|
||||
let (expected, t2) = t.join().unwrap();
|
||||
let actual = t2.join().unwrap();
|
||||
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
extern crate futures;
|
||||
extern crate tokio_core;
|
||||
extern crate tokio_io;
|
||||
|
||||
use std::net::TcpStream;
|
||||
use std::thread;
|
||||
@@ -7,7 +8,7 @@ use std::io::{Write, Read};
|
||||
|
||||
use futures::Future;
|
||||
use futures::stream::Stream;
|
||||
use tokio_core::io::read_to_end;
|
||||
use tokio_io::io::read_to_end;
|
||||
use tokio_core::net::TcpListener;
|
||||
use tokio_core::reactor::Core;
|
||||
|
||||
|
||||
+4
-2
@@ -1,6 +1,7 @@
|
||||
extern crate env_logger;
|
||||
extern crate futures;
|
||||
extern crate tokio_core;
|
||||
extern crate tokio_io;
|
||||
|
||||
use std::io::{Read, Write};
|
||||
use std::net::TcpStream;
|
||||
@@ -8,9 +9,10 @@ use std::thread;
|
||||
|
||||
use futures::Future;
|
||||
use futures::stream::Stream;
|
||||
use tokio_core::io::{copy, Io};
|
||||
use tokio_core::net::TcpListener;
|
||||
use tokio_core::reactor::Core;
|
||||
use tokio_io::AsyncRead;
|
||||
use tokio_io::io::copy;
|
||||
|
||||
macro_rules! t {
|
||||
($e:expr) => (match $e {
|
||||
@@ -44,7 +46,7 @@ fn echo_server() {
|
||||
let halves = client.map(|s| s.0.split());
|
||||
let copied = halves.and_then(|(a, b)| copy(a, b));
|
||||
|
||||
let amt = t!(l.run(copied));
|
||||
let (amt, _, _) = t!(l.run(copied));
|
||||
t.join().unwrap();
|
||||
|
||||
assert_eq!(amt, msg.len() as u64 * 1024);
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
extern crate futures;
|
||||
extern crate tokio_core;
|
||||
extern crate tokio_io;
|
||||
|
||||
use std::net::TcpStream;
|
||||
use std::thread;
|
||||
@@ -7,7 +8,7 @@ use std::io::{Write, Read};
|
||||
|
||||
use futures::Future;
|
||||
use futures::stream::Stream;
|
||||
use tokio_core::io::read_to_end;
|
||||
use tokio_io::io::read_to_end;
|
||||
use tokio_core::net::TcpListener;
|
||||
use tokio_core::reactor::Core;
|
||||
|
||||
|
||||
+27
-13
@@ -1,35 +1,49 @@
|
||||
extern crate tokio_core;
|
||||
extern crate env_logger;
|
||||
extern crate futures;
|
||||
extern crate tokio_core;
|
||||
extern crate tokio_io;
|
||||
extern crate bytes;
|
||||
|
||||
use std::io;
|
||||
use std::net::Shutdown;
|
||||
|
||||
use bytes::{BytesMut, BufMut};
|
||||
use futures::{Future, Stream, Sink};
|
||||
use tokio_core::io::{write_all, read, Codec, EasyBuf, Io};
|
||||
use tokio_core::net::{TcpListener, TcpStream};
|
||||
use tokio_core::reactor::Core;
|
||||
use tokio_io::codec::{Encoder, Decoder};
|
||||
use tokio_io::io::{write_all, read};
|
||||
use tokio_io::AsyncRead;
|
||||
|
||||
pub struct LineCodec;
|
||||
|
||||
impl Codec for LineCodec {
|
||||
type In = EasyBuf;
|
||||
type Out = EasyBuf;
|
||||
impl Decoder for LineCodec {
|
||||
type Item = BytesMut;
|
||||
type Error = io::Error;
|
||||
|
||||
fn decode(&mut self, buf: &mut EasyBuf) -> Result<Option<EasyBuf>, io::Error> {
|
||||
match buf.as_slice().iter().position(|&b| b == b'\n') {
|
||||
Some(i) => Ok(Some(buf.drain_to(i + 1).into())),
|
||||
fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<BytesMut>, io::Error> {
|
||||
match buf.iter().position(|&b| b == b'\n') {
|
||||
Some(i) => Ok(Some(buf.split_to(i + 1).into())),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_eof(&mut self, buf: &mut EasyBuf) -> io::Result<EasyBuf> {
|
||||
let amt = buf.len();
|
||||
Ok(buf.drain_to(amt))
|
||||
fn decode_eof(&mut self, buf: &mut BytesMut) -> io::Result<Option<BytesMut>> {
|
||||
if buf.len() == 0 {
|
||||
Ok(None)
|
||||
} else {
|
||||
let amt = buf.len();
|
||||
Ok(Some(buf.split_to(amt)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn encode(&mut self, item: EasyBuf, into: &mut Vec<u8>) -> io::Result<()> {
|
||||
into.extend_from_slice(item.as_slice());
|
||||
impl Encoder for LineCodec {
|
||||
type Item = BytesMut;
|
||||
type Error = io::Error;
|
||||
|
||||
fn encode(&mut self, item: BytesMut, into: &mut BytesMut) -> io::Result<()> {
|
||||
into.put(&item[..]);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
+9
-6
@@ -5,6 +5,7 @@ extern crate futures;
|
||||
extern crate libc;
|
||||
extern crate mio;
|
||||
extern crate tokio_core;
|
||||
extern crate tokio_io;
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{self, Write};
|
||||
@@ -12,11 +13,11 @@ use std::os::unix::io::{AsRawFd, FromRawFd};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use mio::{Evented, PollOpt, Ready, Token};
|
||||
use mio::unix::EventedFd;
|
||||
|
||||
use tokio_core::io::read_to_end;
|
||||
use mio::unix::{UnixReady, EventedFd};
|
||||
use mio::{PollOpt, Ready, Token};
|
||||
use mio::event::Evented;
|
||||
use tokio_core::reactor::{Core, PollEvented};
|
||||
use tokio_io::io::read_to_end;
|
||||
|
||||
macro_rules! t {
|
||||
($e:expr) => (match $e {
|
||||
@@ -46,11 +47,13 @@ impl io::Read for MyFile {
|
||||
impl Evented for MyFile {
|
||||
fn register(&self, poll: &mio::Poll, token: Token, interest: Ready, opts: PollOpt)
|
||||
-> io::Result<()> {
|
||||
EventedFd(&self.0.as_raw_fd()).register(poll, token, interest | Ready::hup(), opts)
|
||||
let hup: Ready = UnixReady::hup().into();
|
||||
EventedFd(&self.0.as_raw_fd()).register(poll, token, interest | hup, opts)
|
||||
}
|
||||
fn reregister(&self, poll: &mio::Poll, token: Token, interest: Ready, opts: PollOpt)
|
||||
-> io::Result<()> {
|
||||
EventedFd(&self.0.as_raw_fd()).reregister(poll, token, interest | Ready::hup(), opts)
|
||||
let hup: Ready = UnixReady::hup().into();
|
||||
EventedFd(&self.0.as_raw_fd()).reregister(poll, token, interest | hup, opts)
|
||||
}
|
||||
fn deregister(&self, poll: &mio::Poll) -> io::Result<()> {
|
||||
EventedFd(&self.0.as_raw_fd()).deregister(poll)
|
||||
|
||||
+16
-14
@@ -6,6 +6,8 @@ use std::time::Duration;
|
||||
use std::sync::mpsc;
|
||||
|
||||
use futures::Future;
|
||||
use futures::future;
|
||||
use futures::sync::oneshot;
|
||||
use tokio_core::reactor::Core;
|
||||
|
||||
#[test]
|
||||
@@ -13,15 +15,15 @@ fn simple() {
|
||||
drop(env_logger::init());
|
||||
let mut lp = Core::new().unwrap();
|
||||
|
||||
let (tx1, rx1) = futures::oneshot();
|
||||
let (tx2, rx2) = futures::oneshot();
|
||||
lp.handle().spawn(futures::lazy(|| {
|
||||
tx1.complete(1);
|
||||
let (tx1, rx1) = oneshot::channel();
|
||||
let (tx2, rx2) = oneshot::channel();
|
||||
lp.handle().spawn(future::lazy(|| {
|
||||
tx1.send(1).unwrap();
|
||||
Ok(())
|
||||
}));
|
||||
lp.remote().spawn(|_| {
|
||||
futures::lazy(|| {
|
||||
tx2.complete(2);
|
||||
future::lazy(|| {
|
||||
tx2.send(2).unwrap();
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
@@ -38,12 +40,12 @@ fn simple_core_poll() {
|
||||
let (tx1, tx2) = (tx.clone(), tx.clone());
|
||||
|
||||
lp.turn(Some(Duration::new(0, 0)));
|
||||
lp.handle().spawn(futures::lazy(move || {
|
||||
lp.handle().spawn(future::lazy(move || {
|
||||
tx1.send(1).unwrap();
|
||||
Ok(())
|
||||
}));
|
||||
lp.turn(Some(Duration::new(0, 0)));
|
||||
lp.handle().spawn(futures::lazy(move || {
|
||||
lp.handle().spawn(future::lazy(move || {
|
||||
tx2.send(2).unwrap();
|
||||
Ok(())
|
||||
}));
|
||||
@@ -58,14 +60,14 @@ fn spawn_in_poll() {
|
||||
drop(env_logger::init());
|
||||
let mut lp = Core::new().unwrap();
|
||||
|
||||
let (tx1, rx1) = futures::oneshot();
|
||||
let (tx2, rx2) = futures::oneshot();
|
||||
let (tx1, rx1) = oneshot::channel();
|
||||
let (tx2, rx2) = oneshot::channel();
|
||||
let remote = lp.remote();
|
||||
lp.handle().spawn(futures::lazy(move || {
|
||||
tx1.complete(1);
|
||||
lp.handle().spawn(future::lazy(move || {
|
||||
tx1.send(1).unwrap();
|
||||
remote.spawn(|_| {
|
||||
futures::lazy(|| {
|
||||
tx2.complete(2);
|
||||
future::lazy(|| {
|
||||
tx2.send(2).unwrap();
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
extern crate env_logger;
|
||||
extern crate futures;
|
||||
extern crate tokio_core;
|
||||
extern crate env_logger;
|
||||
extern crate tokio_io;
|
||||
|
||||
use std::io::{Read, Write};
|
||||
use std::net::TcpStream;
|
||||
@@ -8,7 +9,8 @@ use std::thread;
|
||||
|
||||
use futures::Future;
|
||||
use futures::stream::Stream;
|
||||
use tokio_core::io::{Io, copy};
|
||||
use tokio_io::io::copy;
|
||||
use tokio_io::AsyncRead;
|
||||
use tokio_core::net::TcpListener;
|
||||
use tokio_core::reactor::Core;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user