Files
tokio/src/io/split.rs
T

55 lines
1.5 KiB
Rust
Raw Normal View History

2016-09-07 13:53:18 -07:00
use std::cell::RefCell;
use std::io::{self, Read, Write};
2016-09-14 11:14:18 -07:00
use futures::Async;
2016-09-07 13:53:18 -07:00
use futures::task::TaskRc;
2016-09-14 11:14:18 -07:00
use io::Io;
2016-09-07 13:53:18 -07:00
/// 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>>,
}
2016-09-14 11:14:18 -07:00
pub fn split<T: Io>(t: T) -> (ReadHalf<T>, WriteHalf<T>) {
2016-09-07 13:53:18 -07:00
let rc = TaskRc::new(RefCell::new(t));
(ReadHalf { handle: rc.clone() }, WriteHalf { handle: rc })
}
2016-09-14 11:14:18 -07:00
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())
}
}
2016-09-07 13:53:18 -07:00
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())
}
}