From 614887b8c15943b4afae494af83a815058f128a3 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 5 Nov 2016 11:14:54 -0700 Subject: [PATCH] Implement split() with BiLock --- Cargo.toml | 3 +++ src/io/mod.rs | 6 +----- src/io/split.rs | 37 ++++++++++++++++++++++++++----------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 60d304b35..b16e52b2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,3 +20,6 @@ slab = "0.3" [dev-dependencies] env_logger = { version = "0.3", default-features = false } + +[replace] +'futures:0.1.3' = { git = "https://github.com/alexcrichton/futures-rs" } diff --git a/src/io/mod.rs b/src/io/mod.rs index 2263692fa..4648cf70f 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -109,11 +109,7 @@ pub trait Io: io::Read + io::Write { /// Helper method for splitting this read/write object into two halves. /// /// The two halves returned implement the `Read` and `Write` traits, - /// respectively, but are only usable on the current task. - /// - /// # Panics - /// - /// This method will panic if there is not currently an active future task. + /// respectively. fn split(self) -> (ReadHalf, WriteHalf) where Self: Sized { diff --git a/src/io/split.rs b/src/io/split.rs index 46b89308a..a012ed69a 100644 --- a/src/io/split.rs +++ b/src/io/split.rs @@ -1,31 +1,34 @@ -use std::cell::RefCell; use std::io::{self, Read, Write}; use futures::Async; -use futures::task::TaskRc; +use futures::sync::BiLock; +use mio; use io::Io; /// The readable half of an object returned from `Io::split`. pub struct ReadHalf { - handle: TaskRc>, + handle: BiLock, } /// The writable half of an object returned from `Io::split`. pub struct WriteHalf { - handle: TaskRc>, + handle: BiLock, } pub fn split(t: T) -> (ReadHalf, WriteHalf) { - let rc = TaskRc::new(RefCell::new(t)); - (ReadHalf { handle: rc.clone() }, WriteHalf { handle: rc }) + let (a, b) = BiLock::new(t); + (ReadHalf { handle: a }, WriteHalf { handle: b }) } impl ReadHalf { /// Calls the underlying `poll_read` function on this handling, testing to /// see if it's ready to be read from. pub fn poll_read(&mut self) -> Async<()> { - self.handle.with(|t| t.borrow_mut().poll_read()) + match self.handle.poll_lock() { + Async::Ready(mut l) => l.poll_read(), + Async::NotReady => Async::NotReady, + } } } @@ -33,22 +36,34 @@ impl WriteHalf { /// Calls the underlying `poll_write` function on this handling, testing to /// see if it's ready to be written to. pub fn poll_write(&mut self) -> Async<()> { - self.handle.with(|t| t.borrow_mut().poll_write()) + match self.handle.poll_lock() { + Async::Ready(mut l) => l.poll_write(), + Async::NotReady => Async::NotReady, + } } } impl Read for ReadHalf { fn read(&mut self, buf: &mut [u8]) -> io::Result { - self.handle.with(|t| t.borrow_mut().read(buf)) + match self.handle.poll_lock() { + Async::Ready(mut l) => l.read(buf), + Async::NotReady => Err(mio::would_block()), + } } } impl Write for WriteHalf { fn write(&mut self, buf: &[u8]) -> io::Result { - self.handle.with(|t| t.borrow_mut().write(buf)) + match self.handle.poll_lock() { + Async::Ready(mut l) => l.write(buf), + Async::NotReady => Err(mio::would_block()), + } } fn flush(&mut self) -> io::Result<()> { - self.handle.with(|t| t.borrow_mut().flush()) + match self.handle.poll_lock() { + Async::Ready(mut l) => l.flush(), + Async::NotReady => Err(mio::would_block()), + } } }