Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

futures: replace pin_utils with pin_project #374

Merged
merged 1 commit into from
Oct 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tracing-futures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ license = "MIT"
[features]
default = ["futures-01", "tokio"]
futures-01 = ["futures"]
std-future = ["pin-utils"]
std-future = ["pin-project"]
futures-preview = ["std-future", "futures-core-preview"]
tokio-alpha = ["std-future", "tokio_02"]

[dependencies]
futures = { version = "0.1", optional = true }
futures-core-preview = { version = "0.3.0-alpha.18", optional = true }
pin-utils = { version = "0.1.0-alpha.4", optional = true }
pin-project = { version = "0.4", optional = true}
tracing = "0.1"
tokio-executor = { version = "0.1", optional = true }
tokio = { version = "0.1", optional = true }
tokio_02 = { package = "tokio", version = "0.2.0-alpha.4", optional = true }
tokio_02 = { package = "tokio", version = "0.2.0-alpha.6", optional = true }

[dev-dependencies]
tokio = "0.1.22"
Expand Down
44 changes: 22 additions & 22 deletions tracing-futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
//! [`futures`]: https://crates.io/crates/futures
#[cfg(feature = "futures-01")]
extern crate futures;
#[cfg(feature = "std-future")]
extern crate pin_utils;
#[cfg(feature = "tokio-executor")]
extern crate tokio_executor;
#[cfg_attr(test, macro_use)]
extern crate tracing;

#[cfg(feature = "std-future")]
use pin_project::pin_project;
#[cfg(feature = "std-future")]
use std::{pin::Pin, task::Context};

Expand Down Expand Up @@ -87,41 +87,44 @@ pub trait WithSubscriber: Sized {
}

/// A future, stream, or sink that has been instrumented with a `tracing` span.
#[cfg_attr(feature = "std-future", pin_project)]
#[derive(Debug, Clone)]
pub struct Instrumented<T> {
#[cfg(feature = "std-future")]
#[pin]
inner: T,
#[cfg(not(feature = "std-future"))]
inner: T,
span: Span,
}

/// A future, stream, sink, or executor that has been instrumented with a
/// `tracing` subscriber.
#[cfg_attr(feature = "std-future", pin_project)]
#[derive(Clone, Debug)]
pub struct WithDispatch<T> {
// cfg_attr doesn't work inside structs, apparently...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this is related to the order of application of proc-macro (taiki-e/pin-project#68 (comment)). I will fix this.

#[cfg(feature = "std-future")]
#[pin]
inner: T,
#[cfg(not(feature = "std-future"))]
inner: T,
dispatch: Dispatch,
}

impl<T: Sized> Instrument for T {}

#[cfg(feature = "std-future")]
impl<T: std::future::Future> Instrumented<T> {
pin_utils::unsafe_pinned!(inner: T);
}

#[cfg(feature = "std-future")]
impl<T: std::future::Future> std::future::Future for Instrumented<T> {
type Output = T::Output;

fn poll(mut self: Pin<&mut Self>, lw: &mut Context) -> std::task::Poll<Self::Output> {
let span = self.as_ref().span.clone();
let _enter = span.enter();
self.as_mut().inner().poll(lw)
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> std::task::Poll<Self::Output> {
let this = self.project();
let _enter = this.span.enter();
this.inner.poll(cx)
}
}

#[cfg(feature = "std-future")]
impl<T: Unpin> Unpin for Instrumented<T> {}

#[cfg(feature = "futures-01")]
impl<T: futures::Future> futures::Future for Instrumented<T> {
type Item = T::Item;
Expand Down Expand Up @@ -192,18 +195,15 @@ impl<T: futures::Future> futures::Future for WithDispatch<T> {
}
}

#[cfg(feature = "std-future")]
impl<T: std::future::Future> WithDispatch<T> {
pin_utils::unsafe_pinned!(inner: T);
}

#[cfg(feature = "std-future")]
impl<T: std::future::Future> std::future::Future for WithDispatch<T> {
type Output = T::Output;

fn poll(mut self: Pin<&mut Self>, lw: &mut Context) -> std::task::Poll<Self::Output> {
let dispatch = self.as_ref().dispatch.clone();
dispatcher::with_default(&dispatch, || self.as_mut().inner().poll(lw))
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> std::task::Poll<Self::Output> {
let this = self.project();
let dispatch = this.dispatch;
let future = this.inner;
dispatcher::with_default(dispatch, || future.poll(cx))
}
}

Expand Down