Skip to content

Commit d6750d0

Browse files
mbilkerhawkw
authored andcommitted
futures: add support for std::futures::Future (#40)
This PR adds a `std::future::Future` implementation to `Instrumented<T>` * futures: add support for std::futures::Future and futures 0.3 * futures: support std::futures::Future with !Unpin futures - Thank you Eliza Weisman (hawkw) for the solution using the pin-utils crate - Added feature gates for the futures 0.1, tokio, and std::future implementations * futures: fix formatting * futures: rustfmt * futures: use tokio-test for testing std::future::Future unit tests * futures: rustfmt * futures: remove futures_api conditional include * futures: add testing of all features under nightly * futures: separate std-future tests into submodule that is only run on the nightly toolchain * futures: move futures-01 tests to submodule * futures: fix travis build * future: remove extraneous part of function names - test_std_future is part of the namespace so having std_future as part of the test function name is not needed
1 parent 2cfc513 commit d6750d0

File tree

6 files changed

+311
-126
lines changed

6 files changed

+311
-126
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ jobs:
2828
- script: cargo test --features=doctest-readme --all
2929
name: "doctest readme"
3030
rust: nightly
31+
- script: (cd tracing-futures/test_std_future && cargo test)
32+
name: "futures nightly"
33+
rust: nightly

tracing-futures/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ authors = ["Eliza Weisman <[email protected]>"]
55
edition = "2018"
66

77
[features]
8-
default = ["tokio"]
8+
default = ["futures-01", "tokio"]
9+
futures-01 = ["futures"]
10+
std-future = ["pin-utils"]
911

1012
[dependencies]
11-
futures = "0.1"
13+
futures = { version = "0.1", optional = true }
14+
pin-utils = { version = "0.1.0-alpha.4", optional = true }
1215
tracing = "0.1"
1316
tokio = { version = "0.1", optional = true }
1417
tokio-executor = { version = "0.1", optional = true }

tracing-futures/src/executor.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use crate::{Instrument, Instrumented, WithDispatch};
2+
#[cfg(feature = "futures-01")]
23
use futures::{
34
future::{ExecuteError, Executor},
45
Future,
56
};
7+
#[cfg(feature = "futures-01")]
8+
use tokio::executor::{Executor as TokioExecutor, SpawnError};
9+
use tokio::runtime::{current_thread, Runtime, TaskExecutor};
610

7-
#[cfg(feature = "tokio")]
8-
use tokio::{
9-
executor::{Executor as TokioExecutor, SpawnError},
10-
runtime::{current_thread, Runtime, TaskExecutor},
11-
};
12-
11+
#[cfg(feature = "futures-01")]
1312
macro_rules! deinstrument_err {
1413
($e:expr) => {
1514
$e.map_err(|e| {
@@ -20,6 +19,7 @@ macro_rules! deinstrument_err {
2019
};
2120
}
2221

22+
#[cfg(feature = "futures-01")]
2323
impl<T, F> Executor<F> for Instrumented<T>
2424
where
2525
T: Executor<Instrumented<F>>,
@@ -31,7 +31,7 @@ where
3131
}
3232
}
3333

34-
#[cfg(feature = "tokio")]
34+
#[cfg(all(feature = "futures-01", feature = "tokio"))]
3535
impl<T> TokioExecutor for Instrumented<T>
3636
where
3737
T: TokioExecutor,
@@ -56,6 +56,7 @@ impl Instrumented<Runtime> {
5656
///
5757
/// This method simply wraps a call to `tokio::runtime::Runtime::spawn`,
5858
/// instrumenting the spawned future beforehand.
59+
#[cfg(feature = "futures-01")]
5960
pub fn spawn<F>(&mut self, future: F) -> &mut Self
6061
where
6162
F: Future<Item = (), Error = ()> + Send + 'static,
@@ -80,6 +81,7 @@ impl Instrumented<Runtime> {
8081
///
8182
/// This function panics if the executor is at capacity, if the provided
8283
/// future panics, or if called within an asynchronous execution context.
84+
#[cfg(feature = "futures-01")]
8385
pub fn block_on<F, R, E>(&mut self, future: F) -> Result<R, E>
8486
where
8587
F: Send + 'static + Future<Item = R, Error = E>,
@@ -108,6 +110,7 @@ impl Instrumented<current_thread::Runtime> {
108110
///
109111
/// This method simply wraps a call to `current_thread::Runtime::spawn`,
110112
/// instrumenting the spawned future beforehand.
113+
#[cfg(feature = "futures-01")]
111114
pub fn spawn<F>(&mut self, future: F) -> &mut Self
112115
where
113116
F: Future<Item = (), Error = ()> + 'static,
@@ -141,6 +144,7 @@ impl Instrumented<current_thread::Runtime> {
141144
///
142145
/// This function panics if the executor is at capacity, if the provided
143146
/// future panics, or if called within an asynchronous execution context.
147+
#[cfg(feature = "futures-01")]
144148
pub fn block_on<F, R, E>(&mut self, future: F) -> Result<R, E>
145149
where
146150
F: 'static + Future<Item = R, Error = E>,
@@ -165,6 +169,7 @@ impl Instrumented<current_thread::Runtime> {
165169
}
166170
}
167171

172+
#[cfg(feature = "futures-01")]
168173
impl<T, F> Executor<F> for WithDispatch<T>
169174
where
170175
T: Executor<WithDispatch<F>>,
@@ -176,7 +181,7 @@ where
176181
}
177182
}
178183

179-
#[cfg(feature = "tokio")]
184+
#[cfg(all(feature = "futures-01", feature = "tokio"))]
180185
impl<T> TokioExecutor for WithDispatch<T>
181186
where
182187
T: TokioExecutor,
@@ -202,6 +207,7 @@ impl WithDispatch<Runtime> {
202207
///
203208
/// This method simply wraps a call to `tokio::runtime::Runtime::spawn`,
204209
/// instrumenting the spawned future beforehand.
210+
#[cfg(feature = "futures-01")]
205211
pub fn spawn<F>(&mut self, future: F) -> &mut Self
206212
where
207213
F: Future<Item = (), Error = ()> + Send + 'static,
@@ -227,6 +233,7 @@ impl WithDispatch<Runtime> {
227233
///
228234
/// This function panics if the executor is at capacity, if the provided
229235
/// future panics, or if called within an asynchronous execution context.
236+
#[cfg(feature = "futures-01")]
230237
pub fn block_on<F, R, E>(&mut self, future: F) -> Result<R, E>
231238
where
232239
F: Send + 'static + Future<Item = R, Error = E>,
@@ -257,6 +264,7 @@ impl WithDispatch<current_thread::Runtime> {
257264
///
258265
/// This method simply wraps a call to `current_thread::Runtime::spawn`,
259266
/// instrumenting the spawned future beforehand.
267+
#[cfg(feature = "futures-01")]
260268
pub fn spawn<F>(&mut self, future: F) -> &mut Self
261269
where
262270
F: Future<Item = (), Error = ()> + 'static,
@@ -290,6 +298,7 @@ impl WithDispatch<current_thread::Runtime> {
290298
///
291299
/// This function panics if the executor is at capacity, if the provided
292300
/// future panics, or if called within an asynchronous execution context.
301+
#[cfg(feature = "futures-01")]
293302
pub fn block_on<F, R, E>(&mut self, future: F) -> Result<R, E>
294303
where
295304
F: 'static + Future<Item = R, Error = E>,

0 commit comments

Comments
 (0)