Files
tokio/src/io/split.rs
T

69 lines
1.9 KiB
Rust
Raw Normal View History

2016-09-07 13:53:18 -07:00
use std::io::{self, Read, Write};
2016-09-14 11:14:18 -07:00
use futures::Async;
2016-11-05 11:14:54 -07:00
use futures::sync::BiLock;
2016-09-07 13:53:18 -07:00
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> {
2016-11-05 11:14:54 -07:00
handle: BiLock<T>,
2016-09-07 13:53:18 -07:00
}
2016-10-05 11:28:46 +02:00
/// The writable half of an object returned from `Io::split`.
2016-09-07 13:53:18 -07:00
pub struct WriteHalf<T> {
2016-11-05 11:14:54 -07:00
handle: BiLock<T>,
2016-09-07 13:53:18 -07:00
}
2016-09-14 11:14:18 -07:00
pub fn split<T: Io>(t: T) -> (ReadHalf<T>, WriteHalf<T>) {
2016-11-05 11:14:54 -07:00
let (a, b) = BiLock::new(t);
(ReadHalf { handle: a }, WriteHalf { handle: b })
2016-09-07 13:53:18 -07:00
}
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<()> {
2016-11-05 11:14:54 -07:00
match self.handle.poll_lock() {
Async::Ready(mut l) => l.poll_read(),
Async::NotReady => Async::NotReady,
}
2016-09-14 11:14:18 -07:00
}
}
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<()> {
2016-11-05 11:14:54 -07:00
match self.handle.poll_lock() {
Async::Ready(mut l) => l.poll_write(),
Async::NotReady => Async::NotReady,
}
2016-09-14 11:14:18 -07:00
}
}
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> {
2016-11-05 11:14:54 -07:00
match self.handle.poll_lock() {
Async::Ready(mut l) => l.read(buf),
2017-09-03 18:39:04 +02:00
Async::NotReady => Err(io::ErrorKind::WouldBlock.into()),
2016-11-05 11:14:54 -07:00
}
2016-09-07 13:53:18 -07:00
}
}
impl<T: Write> Write for WriteHalf<T> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
2016-11-05 11:14:54 -07:00
match self.handle.poll_lock() {
Async::Ready(mut l) => l.write(buf),
2017-09-03 18:39:04 +02:00
Async::NotReady => Err(io::ErrorKind::WouldBlock.into()),
2016-11-05 11:14:54 -07:00
}
2016-09-07 13:53:18 -07:00
}
fn flush(&mut self) -> io::Result<()> {
2016-11-05 11:14:54 -07:00
match self.handle.poll_lock() {
Async::Ready(mut l) => l.flush(),
2017-09-03 18:39:04 +02:00
Async::NotReady => Err(io::ErrorKind::WouldBlock.into()),
2016-11-05 11:14:54 -07:00
}
2016-09-07 13:53:18 -07:00
}
}