Skip to content

Commit 583f90c

Browse files
author
bors-servo
authored
Auto merge of #167 - spinda:async, r=jdm
Make IpcReceiver a futures::Stream Extracts the IpcReceiver half of #165. This is placed behind an `async` feature so the dependency on futures isn't forced.
2 parents 36e7f5f + 484f2c4 commit 583f90c

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ repository = "https://github.com/servo/ipc-channel"
1010
force-inprocess = []
1111
memfd = ["syscall"]
1212
unstable = []
13+
async = ["futures"]
1314

1415
[dependencies]
1516
bincode = "0.8"
@@ -24,6 +25,7 @@ fnv = "1.0.3"
2425
mio = "0.6.1"
2526

2627
syscall = { version = "0.2.1", optional = true }
28+
futures = { version = "0.1", optional = true }
2729

2830
[dev-dependencies]
2931
crossbeam = "0.2"

src/ipc.rs

+26
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ use std::marker::PhantomData;
2020
use std::mem;
2121
use std::ops::Deref;
2222

23+
#[cfg(feature = "async")]
24+
use futures::{Async, Poll, Stream};
25+
#[cfg(feature = "async")]
26+
use std::io::ErrorKind;
27+
2328
thread_local! {
2429
static OS_IPC_CHANNELS_FOR_DESERIALIZATION: RefCell<Vec<OsOpaqueIpcChannel>> =
2530
RefCell::new(Vec::new())
@@ -86,6 +91,27 @@ impl<T> IpcReceiver<T> where T: for<'de> Deserialize<'de> + Serialize {
8691
}
8792
}
8893

94+
#[cfg(feature = "async")]
95+
impl<T> Stream for IpcReceiver<T> where T: for<'de> Deserialize<'de> + Serialize {
96+
type Item = T;
97+
type Error = bincode::Error;
98+
99+
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
100+
match self.try_recv() {
101+
Ok(msg) => Ok(Some(msg).into()),
102+
Err(err) => match *err {
103+
bincode::ErrorKind::IoError(ref e) if e.kind() == ErrorKind::ConnectionReset => {
104+
Ok(Async::Ready(None))
105+
}
106+
bincode::ErrorKind::IoError(ref e) if e.kind() == ErrorKind::WouldBlock => {
107+
Ok(Async::NotReady)
108+
}
109+
_ => Err(err),
110+
},
111+
}
112+
}
113+
}
114+
89115
impl<'de, T> Deserialize<'de> for IpcReceiver<T> where T: for<'dde> Deserialize<'dde> + Serialize {
90116
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
91117
let index: usize = try!(Deserialize::deserialize(deserializer));

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ extern crate fnv;
3131
#[macro_use]
3232
extern crate syscall;
3333

34+
#[cfg(feature = "async")]
35+
extern crate futures;
36+
3437

3538
pub mod ipc;
3639
pub mod platform;

0 commit comments

Comments
 (0)