Skip to content

Commit 55bc69f

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 3dc80c9 commit 55bc69f

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

tracing-futures/src/lib.rs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ extern crate tokio_executor;
2222
#[cfg_attr(test, macro_use)]
2323
extern crate tracing;
2424

25-
use futures::{Future, Poll, Sink, StartSend, Stream};
25+
use std::{
26+
pin::Pin,
27+
task::Context,
28+
};
29+
30+
use futures::{Sink, StartSend, Stream};
2631
use tracing::{dispatcher, Dispatch, Span};
2732

2833
pub mod executor;
@@ -32,6 +37,10 @@ pub trait Instrument: Sized {
3237
fn instrument(self, span: Span) -> Instrumented<Self> {
3338
Instrumented { inner: self, span }
3439
}
40+
41+
fn boxed_instrument(self, span: Span) -> Instrumented<Pin<Box<Self>>> {
42+
Instrumented { inner: Box::pin(self), span }
43+
}
3544
}
3645

3746
pub trait WithSubscriber: Sized {
@@ -60,11 +69,22 @@ pub struct WithDispatch<T> {
6069

6170
impl<T: Sized> Instrument for T {}
6271

63-
impl<T: Future> Future for Instrumented<T> {
72+
impl<P: std::future::Future> std::future::Future for Instrumented<Pin<Box<P>>> {
73+
type Output = P::Output;
74+
75+
fn poll(self: Pin<&mut Self>, lw: &mut Context) -> std::task::Poll<Self::Output> {
76+
let this = self.get_mut();
77+
let _enter = this.span.enter();
78+
let inner = this.inner.as_mut();
79+
P::poll(inner, lw)
80+
}
81+
}
82+
83+
impl<T: futures::Future> futures::Future for Instrumented<T> {
6484
type Item = T::Item;
6585
type Error = T::Error;
6686

67-
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
87+
fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
6888
let _enter = self.span.enter();
6989
self.inner.poll()
7090
}
@@ -74,7 +94,7 @@ impl<T: Stream> Stream for Instrumented<T> {
7494
type Item = T::Item;
7595
type Error = T::Error;
7696

77-
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
97+
fn poll(&mut self) -> futures::Poll<Option<Self::Item>, Self::Error> {
7898
let _enter = self.span.enter();
7999
self.inner.poll()
80100
}
@@ -89,7 +109,7 @@ impl<T: Sink> Sink for Instrumented<T> {
89109
self.inner.start_send(item)
90110
}
91111

92-
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
112+
fn poll_complete(&mut self) -> futures::Poll<(), Self::SinkError> {
93113
let _enter = self.span.enter();
94114
self.inner.poll_complete()
95115
}
@@ -116,11 +136,11 @@ impl<T> Instrumented<T> {
116136

117137
impl<T: Sized> WithSubscriber for T {}
118138

119-
impl<T: Future> Future for WithDispatch<T> {
139+
impl<T: futures::Future> futures::Future for WithDispatch<T> {
120140
type Item = T::Item;
121141
type Error = T::Error;
122142

123-
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
143+
fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
124144
let inner = &mut self.inner;
125145
dispatcher::with_default(&self.dispatch, || inner.poll())
126146
}
@@ -157,7 +177,7 @@ mod tests {
157177
extern crate tokio;
158178

159179
use super::{test_support::*, *};
160-
use futures::{future, stream, task, Async};
180+
use futures::{future, stream, task, Async, Future};
161181
use tracing::{subscriber::with_default, Level};
162182

163183
struct PollN<T, E> {
@@ -166,10 +186,10 @@ mod tests {
166186
polls: usize,
167187
}
168188

169-
impl<T, E> Future for PollN<T, E> {
189+
impl<T, E> futures::Future for PollN<T, E> {
170190
type Item = T;
171191
type Error = E;
172-
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
192+
fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
173193
self.polls += 1;
174194
if self.polls == self.finish_at {
175195
self.and_return

0 commit comments

Comments
 (0)