Skip to content

Commit 2d09719

Browse files
committed
futures: add support for std::futures::Future and futures 0.3
- To support async functions, the inner future must be boxed. - If someone can figure out how to support !Unpin futures, please do so.
1 parent f135737 commit 2d09719

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

tokio-trace-futures/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ version = "0.0.1"
44
authors = ["Eliza Weisman <[email protected]>"]
55

66
[features]
7-
default = ["with-tokio"]
7+
default = ["with-std-future", "with-tokio"]
8+
with-std-future = []
89
with-tokio = ["tokio"]
910

1011
[dependencies]

tokio-trace-futures/src/lib.rs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1+
#![cfg_attr(feature = "with-std-future", feature(futures_api))]
2+
13
extern crate futures;
24
#[cfg(feature = "with-tokio")]
35
extern crate tokio;
46
#[cfg_attr(test, macro_use)]
57
extern crate tokio_trace;
68

7-
use futures::{Future, Poll, Sink, StartSend, Stream};
9+
#[cfg(feature = "with-std-future")]
10+
use std::{
11+
pin::Pin,
12+
task::Context,
13+
};
14+
15+
use futures::{Sink, StartSend, Stream};
816
use tokio_trace::{dispatcher, Dispatch, Span};
917

1018
pub mod executor;
@@ -14,6 +22,10 @@ pub trait Instrument: Sized {
1422
fn instrument(self, span: Span) -> Instrumented<Self> {
1523
Instrumented { inner: self, span }
1624
}
25+
26+
fn boxed_instrument(self, span: Span) -> Instrumented<Pin<Box<Self>>> {
27+
Instrumented { inner: Box::pin(self), span }
28+
}
1729
}
1830

1931
pub trait WithSubscriber: Sized {
@@ -42,11 +54,23 @@ pub struct WithDispatch<T> {
4254

4355
impl<T: Sized> Instrument for T {}
4456

45-
impl<T: Future> Future for Instrumented<T> {
57+
#[cfg(feature = "with-std-future")]
58+
impl<P: std::future::Future> std::future::Future for Instrumented<Pin<Box<P>>> {
59+
type Output = P::Output;
60+
61+
fn poll(self: Pin<&mut Self>, lw: &mut Context) -> std::task::Poll<Self::Output> {
62+
let this = self.get_mut();
63+
let span = &mut this.span;
64+
let inner = this.inner.as_mut();
65+
span.enter(|| P::poll(inner, lw))
66+
}
67+
}
68+
69+
impl<T: futures::Future> futures::Future for Instrumented<T> {
4670
type Item = T::Item;
4771
type Error = T::Error;
4872

49-
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
73+
fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
5074
let span = &mut self.span;
5175
let inner = &mut self.inner;
5276
span.enter(|| inner.poll())
@@ -57,7 +81,7 @@ impl<T: Stream> Stream for Instrumented<T> {
5781
type Item = T::Item;
5882
type Error = T::Error;
5983

60-
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
84+
fn poll(&mut self) -> futures::Poll<Option<Self::Item>, Self::Error> {
6185
let span = &mut self.span;
6286
let inner = &mut self.inner;
6387
span.enter(|| inner.poll())
@@ -74,7 +98,7 @@ impl<T: Sink> Sink for Instrumented<T> {
7498
span.enter(|| inner.start_send(item))
7599
}
76100

77-
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
101+
fn poll_complete(&mut self) -> futures::Poll<(), Self::SinkError> {
78102
let span = &mut self.span;
79103
let inner = &mut self.inner;
80104
span.enter(|| inner.poll_complete())
@@ -102,11 +126,11 @@ impl<T> Instrumented<T> {
102126

103127
impl<T: Sized> WithSubscriber for T {}
104128

105-
impl<T: Future> Future for WithDispatch<T> {
129+
impl<T: futures::Future> futures::Future for WithDispatch<T> {
106130
type Item = T::Item;
107131
type Error = T::Error;
108132

109-
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
133+
fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
110134
let inner = &mut self.inner;
111135
dispatcher::with_default(&self.dispatch, || inner.poll())
112136
}
@@ -139,7 +163,7 @@ mod tests {
139163
extern crate tokio;
140164

141165
use super::{test_support::*, *};
142-
use futures::{future, stream, task, Async};
166+
use futures::{Async, Future, future, stream, task};
143167
use tokio_trace::{subscriber::with_default, Level};
144168

145169
struct PollN<T, E> {
@@ -148,10 +172,10 @@ mod tests {
148172
polls: usize,
149173
}
150174

151-
impl<T, E> Future for PollN<T, E> {
175+
impl<T, E> futures::Future for PollN<T, E> {
152176
type Item = T;
153177
type Error = E;
154-
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
178+
fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
155179
self.polls += 1;
156180
if self.polls == self.finish_at {
157181
self.and_return

0 commit comments

Comments
 (0)