mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-08 00:00:13 +02:00
Tweak TaskIo wording and such
* Remove TaskIo * task_split -> split * TaskIoRead -> ReadHalf * TaskIoWrite -> WriteHalf Closes #18
This commit is contained in:
+2
-3
@@ -9,7 +9,7 @@ use std::net::SocketAddr;
|
|||||||
|
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use futures::stream::Stream;
|
use futures::stream::Stream;
|
||||||
use tokio_core::io::{copy, TaskIo};
|
use tokio_core::io::{copy, Io};
|
||||||
use tokio_core::net::TcpListener;
|
use tokio_core::net::TcpListener;
|
||||||
use tokio_core::reactor::Core;
|
use tokio_core::reactor::Core;
|
||||||
|
|
||||||
@@ -35,8 +35,7 @@ fn main() {
|
|||||||
// We use the `io::copy` future to copy all data from the
|
// We use the `io::copy` future to copy all data from the
|
||||||
// reading half onto the writing half.
|
// reading half onto the writing half.
|
||||||
socket.incoming().for_each(move |(socket, addr)| {
|
socket.incoming().for_each(move |(socket, addr)| {
|
||||||
let socket = futures::lazy(|| futures::finished(TaskIo::new(socket)));
|
let pair = futures::lazy(|| futures::finished(socket.split()));
|
||||||
let pair = socket.map(|s| s.split());
|
|
||||||
let amt = pair.and_then(|(reader, writer)| copy(reader, writer));
|
let amt = pair.and_then(|(reader, writer)| copy(reader, writer));
|
||||||
|
|
||||||
// Once all that is done we print out how much we wrote, and then
|
// Once all that is done we print out how much we wrote, and then
|
||||||
|
|||||||
+4
-4
@@ -35,14 +35,14 @@ mod copy;
|
|||||||
mod flush;
|
mod flush;
|
||||||
mod read_exact;
|
mod read_exact;
|
||||||
mod read_to_end;
|
mod read_to_end;
|
||||||
mod task;
|
mod split;
|
||||||
mod window;
|
mod window;
|
||||||
mod write_all;
|
mod write_all;
|
||||||
pub use self::copy::{copy, Copy};
|
pub use self::copy::{copy, Copy};
|
||||||
pub use self::flush::{flush, Flush};
|
pub use self::flush::{flush, Flush};
|
||||||
pub use self::read_exact::{read_exact, ReadExact};
|
pub use self::read_exact::{read_exact, ReadExact};
|
||||||
pub use self::read_to_end::{read_to_end, ReadToEnd};
|
pub use self::read_to_end::{read_to_end, ReadToEnd};
|
||||||
pub use self::task::{TaskIo, TaskIoRead, TaskIoWrite};
|
pub use self::split::{ReadHalf, WriteHalf};
|
||||||
pub use self::window::Window;
|
pub use self::window::Window;
|
||||||
pub use self::write_all::{write_all, WriteAll};
|
pub use self::write_all::{write_all, WriteAll};
|
||||||
|
|
||||||
@@ -110,9 +110,9 @@ pub trait Io: Read + Write {
|
|||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// This method will panic if there is not currently an active future task.
|
/// This method will panic if there is not currently an active future task.
|
||||||
fn task_split(self) -> (TaskIoRead<Self>, TaskIoWrite<Self>)
|
fn split(self) -> (ReadHalf<Self>, WriteHalf<Self>)
|
||||||
where Self: Sized
|
where Self: Sized
|
||||||
{
|
{
|
||||||
TaskIo::new(self).split()
|
split::split(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
use std::cell::RefCell;
|
||||||
|
use std::io::{self, Read, Write};
|
||||||
|
|
||||||
|
use futures::task::TaskRc;
|
||||||
|
|
||||||
|
/// The readable half of an object returned from `Io::split`.
|
||||||
|
pub struct ReadHalf<T> {
|
||||||
|
handle: TaskRc<RefCell<T>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The readable half of an object returned from `Io::split`.
|
||||||
|
pub struct WriteHalf<T> {
|
||||||
|
handle: TaskRc<RefCell<T>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn split<T: Read + Write>(t: T) -> (ReadHalf<T>, WriteHalf<T>) {
|
||||||
|
let rc = TaskRc::new(RefCell::new(t));
|
||||||
|
(ReadHalf { handle: rc.clone() }, WriteHalf { handle: rc })
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Read> Read for ReadHalf<T> {
|
||||||
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||||
|
self.handle.with(|t| t.borrow_mut().read(buf))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Write> Write for WriteHalf<T> {
|
||||||
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||||
|
self.handle.with(|t| t.borrow_mut().write(buf))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush(&mut self) -> io::Result<()> {
|
||||||
|
self.handle.with(|t| t.borrow_mut().flush())
|
||||||
|
}
|
||||||
|
}
|
||||||
-102
@@ -1,102 +0,0 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::io::{self, Read, Write};
|
|
||||||
|
|
||||||
use futures::task::TaskRc;
|
|
||||||
|
|
||||||
/// Abstraction that allows inserting an I/O object into task-local storage,
|
|
||||||
/// returning a handle that can be split.
|
|
||||||
///
|
|
||||||
/// A `TaskIo<T>` handle implements the `ReadTask` and `WriteTask` and will only
|
|
||||||
/// work with the same task that the associated object was inserted into. The
|
|
||||||
/// handle may then be optionally `split` into the read/write halves so they can
|
|
||||||
/// be worked with independently.
|
|
||||||
///
|
|
||||||
/// Note that it is important that the future returned from `TaskIo::new`, when
|
|
||||||
/// polled, will pin the yielded `TaskIo<T>` object to that specific task. Any
|
|
||||||
/// attempt to read or write the object on other tasks will result in a panic.
|
|
||||||
pub struct TaskIo<T> {
|
|
||||||
handle: TaskRc<RefCell<T>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The readable half of a `TaskIo<T>` instance returned from `TaskIo::split`.
|
|
||||||
///
|
|
||||||
/// This handle implements the `ReadTask` trait and can be used to split up an
|
|
||||||
/// I/O object into two distinct halves.
|
|
||||||
pub struct TaskIoRead<T> {
|
|
||||||
handle: TaskRc<RefCell<T>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The writable half of a `TaskIo<T>` instance returned from `TaskIo::split`.
|
|
||||||
///
|
|
||||||
/// This handle implements the `WriteTask` trait and can be used to split up an
|
|
||||||
/// I/O object into two distinct halves.
|
|
||||||
pub struct TaskIoWrite<T> {
|
|
||||||
handle: TaskRc<RefCell<T>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> TaskIo<T> {
|
|
||||||
/// Returns a new future which represents the insertion of the I/O object
|
|
||||||
/// `T` into task local storage, returning a `TaskIo<T>` handle to it.
|
|
||||||
///
|
|
||||||
/// The returned future will never resolve to an error.
|
|
||||||
pub fn new(t: T) -> TaskIo<T> {
|
|
||||||
TaskIo {
|
|
||||||
handle: TaskRc::new(RefCell::new(t)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> TaskIo<T>
|
|
||||||
where T: Read + Write,
|
|
||||||
{
|
|
||||||
/// For an I/O object which is both readable and writable, this method can
|
|
||||||
/// be used to split the handle into two independently owned halves.
|
|
||||||
///
|
|
||||||
/// The returned pair implements the `ReadTask` and `WriteTask` traits,
|
|
||||||
/// respectively, and can be used to pass around the object to different
|
|
||||||
/// combinators if necessary.
|
|
||||||
pub fn split(self) -> (TaskIoRead<T>, TaskIoWrite<T>) {
|
|
||||||
(TaskIoRead { handle: self.handle.clone() },
|
|
||||||
TaskIoWrite { handle: self.handle })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Read for TaskIo<T>
|
|
||||||
where T: io::Read,
|
|
||||||
{
|
|
||||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
||||||
self.handle.with(|t| t.borrow_mut().read(buf))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Write for TaskIo<T>
|
|
||||||
where T: io::Write,
|
|
||||||
{
|
|
||||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
||||||
self.handle.with(|t| t.borrow_mut().write(buf))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn flush(&mut self) -> io::Result<()> {
|
|
||||||
self.handle.with(|t| t.borrow_mut().flush())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Read for TaskIoRead<T>
|
|
||||||
where T: io::Read,
|
|
||||||
{
|
|
||||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
||||||
self.handle.with(|t| t.borrow_mut().read(buf))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Write for TaskIoWrite<T>
|
|
||||||
where T: io::Write,
|
|
||||||
{
|
|
||||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
||||||
self.handle.with(|t| t.borrow_mut().write(buf))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn flush(&mut self) -> io::Result<()> {
|
|
||||||
self.handle.with(|t| t.borrow_mut().flush())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+1
-1
@@ -73,7 +73,7 @@
|
|||||||
//! // We use the `io::copy` future to copy all data from the
|
//! // We use the `io::copy` future to copy all data from the
|
||||||
//! // reading half onto the writing half.
|
//! // reading half onto the writing half.
|
||||||
//! socket.incoming().for_each(|(socket, addr)| {
|
//! socket.incoming().for_each(|(socket, addr)| {
|
||||||
//! let pair = futures::lazy(|| Ok(socket.task_split()));
|
//! let pair = futures::lazy(|| Ok(socket.split()));
|
||||||
//! let amt = pair.and_then(|(reader, writer)| copy(reader, writer));
|
//! let amt = pair.and_then(|(reader, writer)| copy(reader, writer));
|
||||||
//!
|
//!
|
||||||
//! // Once all that is done we print out how much we wrote, and then
|
//! // Once all that is done we print out how much we wrote, and then
|
||||||
|
|||||||
+2
-2
@@ -8,7 +8,7 @@ use std::thread;
|
|||||||
|
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use futures::stream::Stream;
|
use futures::stream::Stream;
|
||||||
use tokio_core::io::{copy, TaskIo};
|
use tokio_core::io::{copy, Io};
|
||||||
use tokio_core::net::TcpListener;
|
use tokio_core::net::TcpListener;
|
||||||
use tokio_core::reactor::Core;
|
use tokio_core::reactor::Core;
|
||||||
|
|
||||||
@@ -42,7 +42,7 @@ fn echo_server() {
|
|||||||
|
|
||||||
let clients = srv.incoming();
|
let clients = srv.incoming();
|
||||||
let client = clients.into_future().map(|e| e.0.unwrap()).map_err(|e| e.0);
|
let client = clients.into_future().map(|e| e.0.unwrap()).map_err(|e| e.0);
|
||||||
let halves = client.map(|s| TaskIo::new(s.0).split());
|
let halves = client.map(|s| s.0.split());
|
||||||
let copied = halves.and_then(|(a, b)| copy(a, b));
|
let copied = halves.and_then(|(a, b)| copy(a, b));
|
||||||
|
|
||||||
let amt = t!(l.run(copied));
|
let amt = t!(l.run(copied));
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use std::thread;
|
|||||||
|
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use futures::stream::Stream;
|
use futures::stream::Stream;
|
||||||
use tokio_core::io::{copy, TaskIo};
|
use tokio_core::io::{Io, copy};
|
||||||
use tokio_core::net::TcpListener;
|
use tokio_core::net::TcpListener;
|
||||||
use tokio_core::reactor::Core;
|
use tokio_core::reactor::Core;
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ fn echo_server() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let future = srv.incoming()
|
let future = srv.incoming()
|
||||||
.map(|s| TaskIo::new(s.0).split())
|
.map(|s| s.0.split())
|
||||||
.map(|(a, b)| copy(a, b).map(|_| ()))
|
.map(|(a, b)| copy(a, b).map(|_| ()))
|
||||||
.buffered(10)
|
.buffered(10)
|
||||||
.take(2)
|
.take(2)
|
||||||
|
|||||||
Reference in New Issue
Block a user