Skip to content

Commit a05d1cd

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 9a00da6 commit a05d1cd

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-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 = ["tokio"]
7+
default = ["tokio", "with-std-future"]
8+
with-std-future = []
89

910
[dependencies]
1011
futures = "0.1"

tokio-trace-futures/src/lib.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//!
1515
//! [`Instrument`]: trait.Instrument.html
1616
//! [`WithSubscriber`]: trait.WithSubscriber.html
17+
#![cfg_attr(feature = "with-std-future", feature(futures_api))]
1718
extern crate futures;
1819
#[cfg(feature = "tokio")]
1920
extern crate tokio;
@@ -22,7 +23,13 @@ extern crate tokio_executor;
2223
#[cfg_attr(test, macro_use)]
2324
extern crate tokio_trace;
2425

25-
use futures::{Future, Poll, Sink, StartSend, Stream};
26+
#[cfg(feature = "with-std-future")]
27+
use std::{
28+
pin::Pin,
29+
task::Context,
30+
};
31+
32+
use futures::{Sink, StartSend, Stream};
2633
use tokio_trace::{dispatcher, Dispatch, Span};
2734

2835
pub mod executor;
@@ -32,6 +39,10 @@ pub trait Instrument: Sized {
3239
fn instrument(self, span: Span) -> Instrumented<Self> {
3340
Instrumented { inner: self, span }
3441
}
42+
43+
fn boxed_instrument(self, span: Span) -> Instrumented<Pin<Box<Self>>> {
44+
Instrumented { inner: Box::pin(self), span }
45+
}
3546
}
3647

3748
pub trait WithSubscriber: Sized {
@@ -60,11 +71,23 @@ pub struct WithDispatch<T> {
6071

6172
impl<T: Sized> Instrument for T {}
6273

63-
impl<T: Future> Future for Instrumented<T> {
74+
#[cfg(feature = "with-std-future")]
75+
impl<P: std::future::Future> std::future::Future for Instrumented<Pin<Box<P>>> {
76+
type Output = P::Output;
77+
78+
fn poll(self: Pin<&mut Self>, lw: &mut Context) -> std::task::Poll<Self::Output> {
79+
let this = self.get_mut();
80+
let span = &mut this.span;
81+
let inner = this.inner.as_mut();
82+
span.enter(|| P::poll(inner, lw))
83+
}
84+
}
85+
86+
impl<T: futures::Future> futures::Future for Instrumented<T> {
6487
type Item = T::Item;
6588
type Error = T::Error;
6689

67-
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
90+
fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
6891
let span = &mut self.span;
6992
let inner = &mut self.inner;
7093
span.enter(|| inner.poll())
@@ -75,7 +98,7 @@ impl<T: Stream> Stream for Instrumented<T> {
7598
type Item = T::Item;
7699
type Error = T::Error;
77100

78-
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
101+
fn poll(&mut self) -> futures::Poll<Option<Self::Item>, Self::Error> {
79102
let span = &mut self.span;
80103
let inner = &mut self.inner;
81104
span.enter(|| inner.poll())
@@ -92,7 +115,7 @@ impl<T: Sink> Sink for Instrumented<T> {
92115
span.enter(|| inner.start_send(item))
93116
}
94117

95-
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
118+
fn poll_complete(&mut self) -> futures::Poll<(), Self::SinkError> {
96119
let span = &mut self.span;
97120
let inner = &mut self.inner;
98121
span.enter(|| inner.poll_complete())
@@ -120,11 +143,11 @@ impl<T> Instrumented<T> {
120143

121144
impl<T: Sized> WithSubscriber for T {}
122145

123-
impl<T: Future> Future for WithDispatch<T> {
146+
impl<T: futures::Future> futures::Future for WithDispatch<T> {
124147
type Item = T::Item;
125148
type Error = T::Error;
126149

127-
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
150+
fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
128151
let inner = &mut self.inner;
129152
dispatcher::with_default(&self.dispatch, || inner.poll())
130153
}
@@ -157,7 +180,7 @@ mod tests {
157180
extern crate tokio;
158181

159182
use super::{test_support::*, *};
160-
use futures::{future, stream, task, Async};
183+
use futures::{Async, Future, future, stream, task};
161184
use tokio_trace::{subscriber::with_default, Level};
162185

163186
struct PollN<T, E> {
@@ -166,10 +189,10 @@ mod tests {
166189
polls: usize,
167190
}
168191

169-
impl<T, E> Future for PollN<T, E> {
192+
impl<T, E> futures::Future for PollN<T, E> {
170193
type Item = T;
171194
type Error = E;
172-
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
195+
fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
173196
self.polls += 1;
174197
if self.polls == self.finish_at {
175198
self.and_return

0 commit comments

Comments
 (0)