mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-24 00:00:11 +02:00
Swap Handle/Pinned
* Handle -> Remote * Pinned -> Handle All APIs now take a `&Handle` by default and in general can return an immediate `io::Result` instead of an `IoFuture`. This reflects how most usage will likely be done through handles rather than remotes, and also all previous functionality can be recovered with a `oneshot` plus `Remote::spawn`. Closes #15
This commit is contained in:
+1
-1
@@ -7,5 +7,5 @@ mod tcp;
|
||||
mod udp;
|
||||
|
||||
pub use self::tcp::{TcpStream, TcpStreamNew};
|
||||
pub use self::tcp::{TcpListener, TcpListenerNew, Incoming};
|
||||
pub use self::tcp::{TcpListener, Incoming};
|
||||
pub use self::udp::{UdpSocket, UdpSocketNew};
|
||||
|
||||
+22
-36
@@ -4,7 +4,7 @@ use std::mem;
|
||||
use std::net::{self, SocketAddr, Shutdown};
|
||||
|
||||
use futures::stream::Stream;
|
||||
use futures::{Future, IntoFuture, failed, Poll, Async};
|
||||
use futures::{self, Future, failed, Poll, Async};
|
||||
use mio;
|
||||
|
||||
use io::{Io, IoFuture, IoStream};
|
||||
@@ -18,11 +18,6 @@ pub struct TcpListener {
|
||||
io: PollEvented<mio::tcp::TcpListener>,
|
||||
}
|
||||
|
||||
/// Future which will resolve to a `TcpListener`
|
||||
pub struct TcpListenerNew {
|
||||
inner: IoFuture<TcpListener>,
|
||||
}
|
||||
|
||||
/// Stream returned by the `TcpListener::incoming` function representing the
|
||||
/// stream of sockets received from a listener.
|
||||
pub struct Incoming {
|
||||
@@ -35,12 +30,9 @@ impl TcpListener {
|
||||
/// The TCP listener will bind to the provided `addr` address, if available,
|
||||
/// and will be returned as a future. The returned future, if resolved
|
||||
/// successfully, can then be used to accept incoming connections.
|
||||
pub fn bind(addr: &SocketAddr, handle: &Handle) -> TcpListenerNew {
|
||||
let future = match mio::tcp::TcpListener::bind(addr) {
|
||||
Ok(l) => TcpListener::new(l, handle),
|
||||
Err(e) => failed(e).boxed(),
|
||||
};
|
||||
TcpListenerNew { inner: future }
|
||||
pub fn bind(addr: &SocketAddr, handle: &Handle) -> io::Result<TcpListener> {
|
||||
let l = try!(mio::tcp::TcpListener::bind(addr));
|
||||
TcpListener::new(l, handle)
|
||||
}
|
||||
|
||||
/// Create a new TCP listener from the standard library's TCP listener.
|
||||
@@ -72,19 +64,15 @@ impl TcpListener {
|
||||
/// well (same for IPv6).
|
||||
pub fn from_listener(listener: net::TcpListener,
|
||||
addr: &SocketAddr,
|
||||
handle: &Handle) -> IoFuture<TcpListener> {
|
||||
let handle = handle.clone();
|
||||
mio::tcp::TcpListener::from_listener(listener, addr)
|
||||
.into_future()
|
||||
.and_then(move |l| TcpListener::new(l, &handle))
|
||||
.boxed()
|
||||
handle: &Handle) -> io::Result<TcpListener> {
|
||||
let l = try!(mio::tcp::TcpListener::from_listener(listener, addr));
|
||||
TcpListener::new(l, handle)
|
||||
}
|
||||
|
||||
fn new(listener: mio::tcp::TcpListener, handle: &Handle)
|
||||
-> IoFuture<TcpListener> {
|
||||
PollEvented::new(listener, handle).map(|io| {
|
||||
TcpListener { io: io }
|
||||
}).boxed()
|
||||
-> io::Result<TcpListener> {
|
||||
let io = try!(PollEvented::new(listener, handle));
|
||||
Ok(TcpListener { io: io })
|
||||
}
|
||||
|
||||
/// Test whether this socket is ready to be read or not.
|
||||
@@ -129,13 +117,19 @@ impl TcpListener {
|
||||
}
|
||||
}
|
||||
|
||||
let handle = self.io.handle().clone();
|
||||
let remote = self.io.remote().clone();
|
||||
let stream = MyIncoming { inner: self };
|
||||
Incoming {
|
||||
inner: stream.and_then(move |(tcp, addr)| {
|
||||
PollEvented::new(tcp, &handle).map(move |io| {
|
||||
(TcpStream { io: io }, addr)
|
||||
})
|
||||
let (tx, rx) = futures::oneshot();
|
||||
remote.spawn(move |handle| {
|
||||
let res = PollEvented::new(tcp, handle).map(move |io| {
|
||||
(TcpStream { io: io }, addr)
|
||||
});
|
||||
tx.complete(res);
|
||||
Ok(())
|
||||
});
|
||||
rx.then(|r| r.expect("shouldn't be canceled"))
|
||||
}).boxed(),
|
||||
}
|
||||
}
|
||||
@@ -185,15 +179,6 @@ impl fmt::Debug for TcpListener {
|
||||
}
|
||||
}
|
||||
|
||||
impl Future for TcpListenerNew {
|
||||
type Item = TcpListener;
|
||||
type Error = io::Error;
|
||||
|
||||
fn poll(&mut self) -> Poll<TcpListener, io::Error> {
|
||||
self.inner.poll()
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream for Incoming {
|
||||
type Item = (TcpStream, SocketAddr);
|
||||
type Error = io::Error;
|
||||
@@ -242,7 +227,8 @@ impl TcpStream {
|
||||
|
||||
fn new(connected_stream: mio::tcp::TcpStream, handle: &Handle)
|
||||
-> IoFuture<TcpStream> {
|
||||
PollEvented::new(connected_stream, handle).and_then(|io| {
|
||||
let tcp = PollEvented::new(connected_stream, handle);
|
||||
futures::done(tcp).and_then(|io| {
|
||||
TcpStreamConnect::Waiting(TcpStream { io: io })
|
||||
}).boxed()
|
||||
}
|
||||
|
||||
+10
-16
@@ -2,7 +2,7 @@ use std::io;
|
||||
use std::net::{self, SocketAddr, Ipv4Addr, Ipv6Addr};
|
||||
use std::fmt;
|
||||
|
||||
use futures::{Future, failed, Poll, Async};
|
||||
use futures::{Future, Poll, Async};
|
||||
use mio;
|
||||
|
||||
use io::IoFuture;
|
||||
@@ -25,18 +25,14 @@ impl UdpSocket {
|
||||
/// `addr` provided. The returned future will be resolved once the socket
|
||||
/// has successfully bound. If an error happens during the binding or during
|
||||
/// the socket creation, that error will be returned to the future instead.
|
||||
pub fn bind(addr: &SocketAddr, handle: &Handle) -> UdpSocketNew {
|
||||
let future = match mio::udp::UdpSocket::bind(addr) {
|
||||
Ok(udp) => UdpSocket::new(udp, handle),
|
||||
Err(e) => failed(e).boxed(),
|
||||
};
|
||||
UdpSocketNew { inner: future }
|
||||
pub fn bind(addr: &SocketAddr, handle: &Handle) -> io::Result<UdpSocket> {
|
||||
let udp = try!(mio::udp::UdpSocket::bind(addr));
|
||||
UdpSocket::new(udp, handle)
|
||||
}
|
||||
|
||||
fn new(socket: mio::udp::UdpSocket, handle: &Handle) -> IoFuture<UdpSocket> {
|
||||
PollEvented::new(socket, handle).map(|io| {
|
||||
UdpSocket { io: io }
|
||||
}).boxed()
|
||||
fn new(socket: mio::udp::UdpSocket, handle: &Handle) -> io::Result<UdpSocket> {
|
||||
let io = try!(PollEvented::new(socket, handle));
|
||||
Ok(UdpSocket { io: io })
|
||||
}
|
||||
|
||||
/// Creates a new `UdpSocket` from the previously bound socket provided.
|
||||
@@ -49,11 +45,9 @@ impl UdpSocket {
|
||||
/// configure a socket before it's handed off, such as setting options like
|
||||
/// `reuse_address` or binding to multiple addresses.
|
||||
pub fn from_socket(socket: net::UdpSocket,
|
||||
handle: &Handle) -> IoFuture<UdpSocket> {
|
||||
match mio::udp::UdpSocket::from_socket(socket) {
|
||||
Ok(udp) => UdpSocket::new(udp, handle),
|
||||
Err(e) => failed(e).boxed(),
|
||||
}
|
||||
handle: &Handle) -> io::Result<UdpSocket> {
|
||||
let udp = try!(mio::udp::UdpSocket::from_socket(socket));
|
||||
UdpSocket::new(udp, handle)
|
||||
}
|
||||
|
||||
/// Returns the local address that this stream is bound to.
|
||||
|
||||
Reference in New Issue
Block a user