diff --git a/tracing-futures/Cargo.toml b/tracing-futures/Cargo.toml index 52864875ff..aec7fde5cd 100644 --- a/tracing-futures/Cargo.toml +++ b/tracing-futures/Cargo.toml @@ -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" diff --git a/tracing-futures/src/lib.rs b/tracing-futures/src/lib.rs index ddb304a81d..ca1e5ee04b 100644 --- a/tracing-futures/src/lib.rs +++ b/tracing-futures/src/lib.rs @@ -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}; @@ -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 { + #[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 { + // cfg_attr doesn't work inside structs, apparently... + #[cfg(feature = "std-future")] + #[pin] + inner: T, + #[cfg(not(feature = "std-future"))] inner: T, dispatch: Dispatch, } impl Instrument for T {} -#[cfg(feature = "std-future")] -impl Instrumented { - pin_utils::unsafe_pinned!(inner: T); -} - #[cfg(feature = "std-future")] impl std::future::Future for Instrumented { type Output = T::Output; - fn poll(mut self: Pin<&mut Self>, lw: &mut Context) -> std::task::Poll { - 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 { + let this = self.project(); + let _enter = this.span.enter(); + this.inner.poll(cx) } } -#[cfg(feature = "std-future")] -impl Unpin for Instrumented {} - #[cfg(feature = "futures-01")] impl futures::Future for Instrumented { type Item = T::Item; @@ -192,18 +195,15 @@ impl futures::Future for WithDispatch { } } -#[cfg(feature = "std-future")] -impl WithDispatch { - pin_utils::unsafe_pinned!(inner: T); -} - #[cfg(feature = "std-future")] impl std::future::Future for WithDispatch { type Output = T::Output; - fn poll(mut self: Pin<&mut Self>, lw: &mut Context) -> std::task::Poll { - 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 { + let this = self.project(); + let dispatch = this.dispatch; + let future = this.inner; + dispatcher::with_default(dispatch, || future.poll(cx)) } }