Skip to content

Commit 63d8516

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 f4752ff commit 63d8516

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

tracing-futures/src/lib.rs

Lines changed: 29 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,21 @@ 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 + Unpin> std::future::Future for Instrumented<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+
Pin::new(&mut this.inner).poll(lw)
79+
}
80+
}
81+
82+
impl<T: futures::Future> futures::Future for Instrumented<T> {
6483
type Item = T::Item;
6584
type Error = T::Error;
6685

67-
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
86+
fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
6887
let _enter = self.span.enter();
6988
self.inner.poll()
7089
}
@@ -74,7 +93,7 @@ impl<T: Stream> Stream for Instrumented<T> {
7493
type Item = T::Item;
7594
type Error = T::Error;
7695

77-
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
96+
fn poll(&mut self) -> futures::Poll<Option<Self::Item>, Self::Error> {
7897
let _enter = self.span.enter();
7998
self.inner.poll()
8099
}
@@ -89,7 +108,7 @@ impl<T: Sink> Sink for Instrumented<T> {
89108
self.inner.start_send(item)
90109
}
91110

92-
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
111+
fn poll_complete(&mut self) -> futures::Poll<(), Self::SinkError> {
93112
let _enter = self.span.enter();
94113
self.inner.poll_complete()
95114
}
@@ -116,11 +135,11 @@ impl<T> Instrumented<T> {
116135

117136
impl<T: Sized> WithSubscriber for T {}
118137

119-
impl<T: Future> Future for WithDispatch<T> {
138+
impl<T: futures::Future> futures::Future for WithDispatch<T> {
120139
type Item = T::Item;
121140
type Error = T::Error;
122141

123-
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
142+
fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
124143
let inner = &mut self.inner;
125144
dispatcher::with_default(&self.dispatch, || inner.poll())
126145
}
@@ -157,7 +176,7 @@ mod tests {
157176
extern crate tokio;
158177

159178
use super::{test_support::*, *};
160-
use futures::{future, stream, task, Async};
179+
use futures::{future, stream, task, Async, Future};
161180
use tracing::{subscriber::with_default, Level};
162181

163182
struct PollN<T, E> {
@@ -166,10 +185,10 @@ mod tests {
166185
polls: usize,
167186
}
168187

169-
impl<T, E> Future for PollN<T, E> {
188+
impl<T, E> futures::Future for PollN<T, E> {
170189
type Item = T;
171190
type Error = E;
172-
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
191+
fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
173192
self.polls += 1;
174193
if self.polls == self.finish_at {
175194
self.and_return

0 commit comments

Comments
 (0)