Skip to content

Commit 3c86d22

Browse files
author
Markus Westerlind
committed
feat: Implement futures-0.2 support
tokio-rs/tokio-rfcs#2
1 parent f2ad0e0 commit 3c86d22

File tree

4 files changed

+468
-0
lines changed

4 files changed

+468
-0
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ matrix:
1818

1919
script:
2020
- cargo test
21+
- cargo test --features unstable-futures
2122

2223
env:
2324
global:

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ appveyor = { repository = "alexcrichton/tokio-uds" }
1818
[dependencies]
1919
bytes = "0.4"
2020
futures = "0.1"
21+
futures-core = { version = "=0.2.0-beta", optional = true }
22+
futures-io = { version = "=0.2.0-beta", optional = true }
23+
futures-sink = { version = "=0.2.0-beta", optional = true }
2124
iovec = "0.1"
2225
libc = "0.2"
2326
log = "0.4"
@@ -26,3 +29,5 @@ mio-uds = "0.6.4"
2629
tokio-reactor = "0.1.1"
2730
tokio-io = "0.1"
2831

32+
[features]
33+
unstable-futures = ["futures-core", "futures-io", "futures-sink", "tokio-reactor/unstable-futures"]

src/frame.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ use std::path::PathBuf;
44

55
use futures::{Async, AsyncSink, Poll, Sink, StartSend, Stream};
66

7+
#[cfg(feature = "unstable-futures")]
8+
use futures2::{self, task};
9+
#[cfg(feature = "unstable-futures")]
10+
use futures_sink;
11+
712
use UnixDatagram;
813

914
/// Encoding of frames via buffers.
@@ -80,6 +85,20 @@ impl<C: UnixDatagramCodec> Stream for UnixDatagramFramed<C> {
8085
}
8186
}
8287

88+
#[cfg(feature = "unstable-futures")]
89+
impl<C: UnixDatagramCodec> futures2::Stream for UnixDatagramFramed<C> {
90+
type Item = C::In;
91+
type Error = io::Error;
92+
93+
fn poll_next(&mut self, cx: &mut task::Context) -> futures2::Poll<Option<C::In>, io::Error> {
94+
let (n, addr) = try_ready2!(self.socket.recv_from2(cx, &mut self.rd));
95+
trace!("received {} bytes, decoding", n);
96+
let frame = try!(self.codec.decode(&addr, &self.rd[..n]));
97+
trace!("frame decoded from buffer");
98+
Ok(futures2::Async::Ready(Some(frame)))
99+
}
100+
}
101+
83102
impl<C: UnixDatagramCodec> Sink for UnixDatagramFramed<C> {
84103
type SinkItem = C::Out;
85104
type SinkError = io::Error;
@@ -124,6 +143,53 @@ impl<C: UnixDatagramCodec> Sink for UnixDatagramFramed<C> {
124143
}
125144
}
126145

146+
#[cfg(feature = "unstable-futures")]
147+
impl<C: UnixDatagramCodec> futures_sink::Sink for UnixDatagramFramed<C> {
148+
type SinkItem = C::Out;
149+
type SinkError = io::Error;
150+
151+
fn poll_ready(&mut self, cx: &mut task::Context) -> futures2::Poll<(), io::Error> {
152+
if self.wr.len() > 0 {
153+
try!(self.poll_flush(cx));
154+
if self.wr.len() > 0 {
155+
return Ok(futures2::Async::Pending);
156+
}
157+
}
158+
Ok(().into())
159+
}
160+
161+
fn start_send(&mut self, item: C::Out) -> Result<(), io::Error> {
162+
self.out_addr = try!(self.codec.encode(item, &mut self.wr));
163+
Ok(())
164+
}
165+
166+
fn poll_flush(&mut self, cx: &mut task::Context) -> futures2::Poll<(), io::Error> {
167+
trace!("flushing framed transport");
168+
169+
if self.wr.is_empty() {
170+
return Ok(futures2::Async::Ready(()));
171+
}
172+
173+
trace!("writing; remaining={}", self.wr.len());
174+
let n = try_ready2!(self.socket.send_to2(cx, &self.wr, &self.out_addr));
175+
trace!("written {}", n);
176+
let wrote_all = n == self.wr.len();
177+
self.wr.clear();
178+
if wrote_all {
179+
Ok(futures2::Async::Ready(()))
180+
} else {
181+
Err(io::Error::new(
182+
io::ErrorKind::Other,
183+
"failed to write entire datagram to socket",
184+
))
185+
}
186+
}
187+
188+
fn poll_close(&mut self, cx: &mut task::Context) -> futures2::Poll<(), io::Error> {
189+
self.poll_flush(cx)
190+
}
191+
}
192+
127193
pub fn new<C: UnixDatagramCodec>(socket: UnixDatagram, codec: C) -> UnixDatagramFramed<C> {
128194
UnixDatagramFramed {
129195
socket: socket,

0 commit comments

Comments
 (0)