Add poll_{read,write} on halves

Closes #32
This commit is contained in:
Alex Crichton
2016-09-14 11:14:18 -07:00
parent 933b34cd25
commit 418a973520
+20 -1
View File
@@ -1,8 +1,11 @@
use std::cell::RefCell;
use std::io::{self, Read, Write};
use futures::Async;
use futures::task::TaskRc;
use io::Io;
/// The readable half of an object returned from `Io::split`.
pub struct ReadHalf<T> {
handle: TaskRc<RefCell<T>>,
@@ -13,11 +16,27 @@ pub struct WriteHalf<T> {
handle: TaskRc<RefCell<T>>,
}
pub fn split<T: Read + Write>(t: T) -> (ReadHalf<T>, WriteHalf<T>) {
pub fn split<T: Io>(t: T) -> (ReadHalf<T>, WriteHalf<T>) {
let rc = TaskRc::new(RefCell::new(t));
(ReadHalf { handle: rc.clone() }, WriteHalf { handle: rc })
}
impl<T: Io> ReadHalf<T> {
/// 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())
}
}
impl<T: Io> WriteHalf<T> {
/// 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())
}
}
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))