Skip to content

futures: add support for std::futures::Future #40

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

Merged
merged 12 commits into from
Jul 15, 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
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ jobs:
- script: cargo test --features=doctest-readme --all
name: "doctest readme"
rust: nightly
- script: (cd tracing-futures/test_std_future && cargo test)
name: "futures nightly"
rust: nightly
7 changes: 5 additions & 2 deletions tracing-futures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ authors = ["Eliza Weisman <[email protected]>"]
edition = "2018"

[features]
default = ["tokio"]
default = ["futures-01", "tokio"]
futures-01 = ["futures"]
std-future = ["pin-utils"]

[dependencies]
futures = "0.1"
futures = { version = "0.1", optional = true }
pin-utils = { version = "0.1.0-alpha.4", optional = true }
tracing = "0.1"
tokio = { version = "0.1", optional = true }
tokio-executor = { version = "0.1", optional = true }
Expand Down
25 changes: 17 additions & 8 deletions tracing-futures/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use crate::{Instrument, Instrumented, WithDispatch};
#[cfg(feature = "futures-01")]
use futures::{
future::{ExecuteError, Executor},
Future,
};
#[cfg(feature = "futures-01")]
use tokio::executor::{Executor as TokioExecutor, SpawnError};
use tokio::runtime::{current_thread, Runtime, TaskExecutor};

#[cfg(feature = "tokio")]
use tokio::{
executor::{Executor as TokioExecutor, SpawnError},
runtime::{current_thread, Runtime, TaskExecutor},
};

#[cfg(feature = "futures-01")]
macro_rules! deinstrument_err {
($e:expr) => {
$e.map_err(|e| {
Expand All @@ -20,6 +19,7 @@ macro_rules! deinstrument_err {
};
}

#[cfg(feature = "futures-01")]
impl<T, F> Executor<F> for Instrumented<T>
where
T: Executor<Instrumented<F>>,
Expand All @@ -31,7 +31,7 @@ where
}
}

#[cfg(feature = "tokio")]
#[cfg(all(feature = "futures-01", feature = "tokio"))]
impl<T> TokioExecutor for Instrumented<T>
where
T: TokioExecutor,
Expand All @@ -56,6 +56,7 @@ impl Instrumented<Runtime> {
///
/// This method simply wraps a call to `tokio::runtime::Runtime::spawn`,
/// instrumenting the spawned future beforehand.
#[cfg(feature = "futures-01")]
pub fn spawn<F>(&mut self, future: F) -> &mut Self
where
F: Future<Item = (), Error = ()> + Send + 'static,
Expand All @@ -80,6 +81,7 @@ impl Instrumented<Runtime> {
///
/// This function panics if the executor is at capacity, if the provided
/// future panics, or if called within an asynchronous execution context.
#[cfg(feature = "futures-01")]
pub fn block_on<F, R, E>(&mut self, future: F) -> Result<R, E>
where
F: Send + 'static + Future<Item = R, Error = E>,
Expand Down Expand Up @@ -108,6 +110,7 @@ impl Instrumented<current_thread::Runtime> {
///
/// This method simply wraps a call to `current_thread::Runtime::spawn`,
/// instrumenting the spawned future beforehand.
#[cfg(feature = "futures-01")]
pub fn spawn<F>(&mut self, future: F) -> &mut Self
where
F: Future<Item = (), Error = ()> + 'static,
Expand Down Expand Up @@ -141,6 +144,7 @@ impl Instrumented<current_thread::Runtime> {
///
/// This function panics if the executor is at capacity, if the provided
/// future panics, or if called within an asynchronous execution context.
#[cfg(feature = "futures-01")]
pub fn block_on<F, R, E>(&mut self, future: F) -> Result<R, E>
where
F: 'static + Future<Item = R, Error = E>,
Expand All @@ -165,6 +169,7 @@ impl Instrumented<current_thread::Runtime> {
}
}

#[cfg(feature = "futures-01")]
impl<T, F> Executor<F> for WithDispatch<T>
where
T: Executor<WithDispatch<F>>,
Expand All @@ -176,7 +181,7 @@ where
}
}

#[cfg(feature = "tokio")]
#[cfg(all(feature = "futures-01", feature = "tokio"))]
impl<T> TokioExecutor for WithDispatch<T>
where
T: TokioExecutor,
Expand All @@ -202,6 +207,7 @@ impl WithDispatch<Runtime> {
///
/// This method simply wraps a call to `tokio::runtime::Runtime::spawn`,
/// instrumenting the spawned future beforehand.
#[cfg(feature = "futures-01")]
pub fn spawn<F>(&mut self, future: F) -> &mut Self
where
F: Future<Item = (), Error = ()> + Send + 'static,
Expand All @@ -227,6 +233,7 @@ impl WithDispatch<Runtime> {
///
/// This function panics if the executor is at capacity, if the provided
/// future panics, or if called within an asynchronous execution context.
#[cfg(feature = "futures-01")]
pub fn block_on<F, R, E>(&mut self, future: F) -> Result<R, E>
where
F: Send + 'static + Future<Item = R, Error = E>,
Expand Down Expand Up @@ -257,6 +264,7 @@ impl WithDispatch<current_thread::Runtime> {
///
/// This method simply wraps a call to `current_thread::Runtime::spawn`,
/// instrumenting the spawned future beforehand.
#[cfg(feature = "futures-01")]
pub fn spawn<F>(&mut self, future: F) -> &mut Self
where
F: Future<Item = (), Error = ()> + 'static,
Expand Down Expand Up @@ -290,6 +298,7 @@ impl WithDispatch<current_thread::Runtime> {
///
/// This function panics if the executor is at capacity, if the provided
/// future panics, or if called within an asynchronous execution context.
#[cfg(feature = "futures-01")]
pub fn block_on<F, R, E>(&mut self, future: F) -> Result<R, E>
where
F: 'static + Future<Item = R, Error = E>,
Expand Down
Loading