Remove Handle argument from I/O constructors (#61)

This commit removes the `Handle` argument from the following constructors

* `TcpListener::bind`
* `TcpStream::connect`
* `UdpSocket::bind`

The `Handle` argument remains on the various `*_std` constructors as they're
more low-level, but this otherwise is intended to set forth a precedent of by
default not taking `Handle` arguments and instead relying on the global
`Handle::default` return value when necesary.
This commit is contained in:
Alex Crichton
2017-12-12 18:32:50 -06:00
committed by Carl Lerche
parent 849771ecfa
commit 4ef772b2db
24 changed files with 48 additions and 98 deletions
+1 -3
View File
@@ -11,7 +11,6 @@ use futures::Future;
use futures::stream::Stream;
use tokio_io::io::copy;
use tokio::net::TcpListener;
use tokio::reactor::Handle;
macro_rules! t {
($e:expr) => (match $e {
@@ -25,8 +24,7 @@ fn echo_server() {
const N: usize = 1024;
drop(env_logger::init());
let handle = Handle::default();
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &handle));
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse())));
let addr = t!(srv.local_addr());
let msg = "foo bar baz";
+1 -3
View File
@@ -10,7 +10,6 @@ use futures::Future;
use futures::stream::Stream;
use tokio_io::io::read_to_end;
use tokio::net::TcpListener;
use tokio::reactor::Handle;
macro_rules! t {
($e:expr) => (match $e {
@@ -21,8 +20,7 @@ macro_rules! t {
#[test]
fn chain_clients() {
let handle = Handle::default();
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &handle));
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse())));
let addr = t!(srv.local_addr());
let t = thread::spawn(move || {
+7 -2
View File
@@ -2,6 +2,7 @@ extern crate tokio;
extern crate futures;
use std::thread;
use std::net;
use futures::future;
use futures::prelude::*;
@@ -13,7 +14,9 @@ use tokio::reactor::Reactor;
fn tcp_doesnt_block() {
let core = Reactor::new().unwrap();
let handle = core.handle();
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap(), &handle).unwrap();
let listener = net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = listener.local_addr().unwrap();
let listener = TcpListener::from_std(listener, &addr, &handle).unwrap();
drop(core);
assert!(listener.incoming().wait().next().unwrap().is_err());
}
@@ -22,7 +25,9 @@ fn tcp_doesnt_block() {
fn drop_wakes() {
let core = Reactor::new().unwrap();
let handle = core.handle();
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap(), &handle).unwrap();
let listener = net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = listener.local_addr().unwrap();
let listener = TcpListener::from_std(listener, &addr, &handle).unwrap();
let (tx, rx) = oneshot::channel::<()>();
let t = thread::spawn(move || {
let incoming = listener.incoming();
+1 -3
View File
@@ -10,7 +10,6 @@ use std::thread;
use futures::Future;
use futures::stream::Stream;
use tokio::net::TcpListener;
use tokio::reactor::Handle;
use tokio_io::AsyncRead;
use tokio_io::io::copy;
@@ -25,8 +24,7 @@ macro_rules! t {
fn echo_server() {
drop(env_logger::init());
let handle = Handle::default();
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &handle));
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse())));
let addr = t!(srv.local_addr());
let msg = "foo bar baz";
+2 -4
View File
@@ -5,7 +5,6 @@ use std::thread;
use futures::prelude::*;
use tokio::net::{TcpStream, TcpListener};
use tokio::reactor::Handle;
macro_rules! t {
($e:expr) => (match $e {
@@ -18,10 +17,9 @@ macro_rules! t {
fn hammer() {
let threads = (0..10).map(|_| {
thread::spawn(|| {
let handle = Handle::default();
let srv = t!(TcpListener::bind(&"127.0.0.1:0".parse().unwrap(), &handle));
let srv = t!(TcpListener::bind(&"127.0.0.1:0".parse().unwrap()));
let addr = t!(srv.local_addr());
let mine = TcpStream::connect(&addr, &handle);
let mine = TcpStream::connect(&addr);
let theirs = srv.incoming().into_future()
.map(|(s, _)| s.unwrap().0)
.map_err(|(s, _)| s);
+1 -3
View File
@@ -10,7 +10,6 @@ use futures::Future;
use futures::stream::Stream;
use tokio_io::io::read_to_end;
use tokio::net::TcpListener;
use tokio::reactor::Handle;
macro_rules! t {
($e:expr) => (match $e {
@@ -21,8 +20,7 @@ macro_rules! t {
#[test]
fn limit() {
let handle = Handle::default();
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &handle));
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse())));
let addr = t!(srv.local_addr());
let t = thread::spawn(move || {
+2 -4
View File
@@ -13,7 +13,6 @@ use futures::{Future, Stream, Sink};
use futures::future::Executor;
use futures_cpupool::CpuPool;
use tokio::net::{TcpListener, TcpStream};
use tokio::reactor::Handle;
use tokio_io::codec::{Encoder, Decoder};
use tokio_io::io::{write_all, read};
use tokio_io::AsyncRead;
@@ -56,9 +55,8 @@ fn echo() {
drop(env_logger::init());
let pool = CpuPool::new(1);
let handle = Handle::default();
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap(), &handle).unwrap();
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
let pool_inner = pool.clone();
let srv = listener.incoming().for_each(move |(socket, _)| {
@@ -69,7 +67,7 @@ fn echo() {
pool.execute(srv.map_err(|e| panic!("srv error: {}", e))).unwrap();
let client = TcpStream::connect(&addr, &handle);
let client = TcpStream::connect(&addr);
let client = client.wait().unwrap();
let (client, _) = write_all(client, b"a\n").wait().unwrap();
let (client, buf, amt) = read(client, vec![0; 1024]).wait().unwrap();
+1 -3
View File
@@ -12,7 +12,6 @@ use futures::stream::Stream;
use tokio_io::io::copy;
use tokio_io::AsyncRead;
use tokio::net::TcpListener;
use tokio::reactor::Handle;
macro_rules! t {
($e:expr) => (match $e {
@@ -25,8 +24,7 @@ macro_rules! t {
fn echo_server() {
drop(env_logger::init());
let handle = Handle::default();
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &handle));
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse())));
let addr = t!(srv.local_addr());
let t = thread::spawn(move || {
+3 -7
View File
@@ -8,7 +8,6 @@ use std::thread;
use futures::Future;
use futures::stream::Stream;
use tokio::reactor::Handle;
use tokio::net::{TcpListener, TcpStream};
macro_rules! t {
@@ -21,14 +20,13 @@ macro_rules! t {
#[test]
fn connect() {
drop(env_logger::init());
let handle = Handle::default();
let srv = t!(net::TcpListener::bind("127.0.0.1:0"));
let addr = t!(srv.local_addr());
let t = thread::spawn(move || {
t!(srv.accept()).0
});
let stream = TcpStream::connect(&addr, &handle);
let stream = TcpStream::connect(&addr);
let mine = t!(stream.wait());
let theirs = t.join().unwrap();
@@ -39,8 +37,7 @@ fn connect() {
#[test]
fn accept() {
drop(env_logger::init());
let handle = Handle::default();
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &handle));
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse())));
let addr = t!(srv.local_addr());
let (tx, rx) = channel();
@@ -64,8 +61,7 @@ fn accept() {
#[test]
fn accept2() {
drop(env_logger::init());
let handle = Handle::default();
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &handle));
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse())));
let addr = t!(srv.local_addr());
let t = thread::spawn(move || {
+6 -10
View File
@@ -8,7 +8,6 @@ use std::net::SocketAddr;
use futures::{Future, Poll, Stream, Sink};
use tokio::net::{UdpSocket, UdpCodec};
use tokio::reactor::Handle;
macro_rules! t {
($e:expr) => (match $e {
@@ -18,9 +17,8 @@ macro_rules! t {
}
fn send_messages<S: SendFn + Clone, R: RecvFn + Clone>(send: S, recv: R) {
let handle = Handle::default();
let mut a = t!(UdpSocket::bind(&([127, 0, 0, 1], 0).into(), &handle));
let mut b = t!(UdpSocket::bind(&([127, 0, 0, 1], 0).into(), &handle));
let mut a = t!(UdpSocket::bind(&([127, 0, 0, 1], 0).into()));
let mut b = t!(UdpSocket::bind(&([127, 0, 0, 1], 0).into()));
let a_addr = t!(a.local_addr());
let b_addr = t!(b.local_addr());
@@ -166,9 +164,8 @@ impl<R: RecvFn> Future for RecvMessage<R> {
#[test]
fn send_dgrams() {
let handle = Handle::default();
let mut a = t!(UdpSocket::bind(&t!("127.0.0.1:0".parse()), &handle));
let mut b = t!(UdpSocket::bind(&t!("127.0.0.1:0".parse()), &handle));
let mut a = t!(UdpSocket::bind(&t!("127.0.0.1:0".parse())));
let mut b = t!(UdpSocket::bind(&t!("127.0.0.1:0".parse())));
let mut buf = [0u8; 50];
let b_addr = t!(b.local_addr());
@@ -216,9 +213,8 @@ impl UdpCodec for Codec {
#[test]
fn send_framed() {
let handle = Handle::default();
let mut a_soc = t!(UdpSocket::bind(&t!("127.0.0.1:0".parse()), &handle));
let mut b_soc = t!(UdpSocket::bind(&t!("127.0.0.1:0".parse()), &handle));
let mut a_soc = t!(UdpSocket::bind(&t!("127.0.0.1:0".parse())));
let mut b_soc = t!(UdpSocket::bind(&t!("127.0.0.1:0".parse())));
let a_addr = t!(a_soc.local_addr());
let b_addr = t!(b_soc.local_addr());