tracing_futures/executor/futures_01.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
use crate::{Instrument, Instrumented, WithDispatch};
use futures_01::{
future::{ExecuteError, Executor},
Future,
};
impl<T, F> Executor<F> for Instrumented<T>
where
T: Executor<Instrumented<F>>,
F: Future<Item = (), Error = ()>,
{
fn execute(&self, future: F) -> Result<(), ExecuteError<F>> {
let future = future.instrument(self.span.clone());
self.inner.execute(future).map_err(|e| {
let kind = e.kind();
let future = e.into_future().into_inner();
ExecuteError::new(kind, future)
})
}
}
impl<T, F> Executor<F> for WithDispatch<T>
where
T: Executor<WithDispatch<F>>,
F: Future<Item = (), Error = ()>,
{
fn execute(&self, future: F) -> Result<(), ExecuteError<F>> {
let future = self.with_dispatch(future);
self.inner.execute(future).map_err(|e| {
let kind = e.kind();
let future = e.into_future().inner;
ExecuteError::new(kind, future)
})
}
}
#[cfg(feature = "tokio")]
#[allow(unreachable_pub, unused_imports)] // https://github.com/rust-lang/rust/issues/57411
pub use self::tokio::*;
#[cfg(feature = "tokio")]
mod tokio {
use crate::{Instrument, Instrumented, WithDispatch};
use futures_01::Future;
use tokio_01::{
executor::{Executor, SpawnError, TypedExecutor},
runtime::{current_thread, Runtime, TaskExecutor},
};
impl<T> Executor for Instrumented<T>
where
T: Executor,
{
fn spawn(
&mut self,
future: Box<dyn Future<Error = (), Item = ()> + 'static + Send>,
) -> Result<(), SpawnError> {
// TODO: get rid of double box somehow?
let future = Box::new(future.instrument(self.span.clone()));
self.inner.spawn(future)
}
}
impl<T, F> TypedExecutor<F> for Instrumented<T>
where
T: TypedExecutor<Instrumented<F>>,
{
fn spawn(&mut self, future: F) -> Result<(), SpawnError> {
self.inner.spawn(future.instrument(self.span.clone()))
}
fn status(&self) -> Result<(), SpawnError> {
self.inner.status()
}
}
impl Instrumented<Runtime> {
/// Spawn an instrumented future onto the Tokio runtime.
///
/// This spawns the given future onto the runtime's executor, usually a
/// thread pool. The thread pool is then responsible for polling the
/// future until it completes.
///
/// This method simply wraps a call to `tokio::runtime::Runtime::spawn`,
/// instrumenting the spawned future beforehand.
pub fn spawn<F>(&mut self, future: F) -> &mut Self
where
F: Future<Item = (), Error = ()> + Send + 'static,
{
let future = future.instrument(self.span.clone());
self.inner.spawn(future);
self
}
/// Run an instrumented future to completion on the Tokio runtime.
///
/// This runs the given future on the runtime, blocking until it is
/// complete, and yielding its resolved result. Any tasks or timers which
/// the future spawns internally will be executed on the runtime.
///
/// This method should not be called from an asynchronous context.
///
/// This method simply wraps a call to `tokio::runtime::Runtime::block_on`,
/// instrumenting the spawned future beforehand.
///
/// # Panics
///
/// This function panics if the executor is at capacity, if the provided
/// future panics, or if called within an asynchronous execution context.
pub fn block_on<F, R, E>(&mut self, future: F) -> Result<R, E>
where
F: Send + 'static + Future<Item = R, Error = E>,
R: Send + 'static,
E: Send + 'static,
{
let future = future.instrument(self.span.clone());
self.inner.block_on(future)
}
/// Return an instrumented handle to the runtime's executor.
///
/// The returned handle can be used to spawn tasks that run on this runtime.
///
/// The instrumented handle functions identically to a
/// `tokio::runtime::TaskExecutor`, but instruments the spawned
/// futures prior to spawning them.
pub fn executor(&self) -> Instrumented<TaskExecutor> {
self.inner.executor().instrument(self.span.clone())
}
}
impl Instrumented<current_thread::Runtime> {
/// Spawn an instrumented future onto the single-threaded Tokio runtime.
///
/// This method simply wraps a call to `current_thread::Runtime::spawn`,
/// instrumenting the spawned future beforehand.
pub fn spawn<F>(&mut self, future: F) -> &mut Self
where
F: Future<Item = (), Error = ()> + 'static,
{
let future = future.instrument(self.span.clone());
self.inner.spawn(future);
self
}
/// Instruments and runs the provided future, blocking the current thread
/// until the future completes.
///
/// This function can be used to synchronously block the current thread
/// until the provided `future` has resolved either successfully or with an
/// error. The result of the future is then returned from this function
/// call.
///
/// Note that this function will **also** execute any spawned futures on the
/// current thread, but will **not** block until these other spawned futures
/// have completed. Once the function returns, any uncompleted futures
/// remain pending in the `Runtime` instance. These futures will not run
/// until `block_on` or `run` is called again.
///
/// The caller is responsible for ensuring that other spawned futures
/// complete execution by calling `block_on` or `run`.
///
/// This method simply wraps a call to `current_thread::Runtime::block_on`,
/// instrumenting the spawned future beforehand.
///
/// # Panics
///
/// This function panics if the executor is at capacity, if the provided
/// future panics, or if called within an asynchronous execution context.
pub fn block_on<F, R, E>(&mut self, future: F) -> Result<R, E>
where
F: 'static + Future<Item = R, Error = E>,
R: 'static,
E: 'static,
{
let future = future.instrument(self.span.clone());
self.inner.block_on(future)
}
/// Get a new instrumented handle to spawn futures on the single-threaded
/// Tokio runtime
///
/// Different to the runtime itself, the handle can be sent to different
/// threads.
///
/// The instrumented handle functions identically to a
/// `tokio::runtime::current_thread::Handle`, but instruments the spawned
/// futures prior to spawning them.
pub fn handle(&self) -> Instrumented<current_thread::Handle> {
self.inner.handle().instrument(self.span.clone())
}
}
impl<T> Executor for WithDispatch<T>
where
T: Executor,
{
fn spawn(
&mut self,
future: Box<dyn Future<Error = (), Item = ()> + 'static + Send>,
) -> Result<(), SpawnError> {
// TODO: get rid of double box?
let future = Box::new(self.with_dispatch(future));
self.inner.spawn(future)
}
}
impl<T, F> TypedExecutor<F> for WithDispatch<T>
where
T: TypedExecutor<WithDispatch<F>>,
{
fn spawn(&mut self, future: F) -> Result<(), SpawnError> {
self.inner.spawn(self.with_dispatch(future))
}
fn status(&self) -> Result<(), SpawnError> {
self.inner.status()
}
}
impl WithDispatch<Runtime> {
/// Spawn a future onto the Tokio runtime, in the context of this
/// `WithDispatch`'s trace dispatcher.
///
/// This spawns the given future onto the runtime's executor, usually a
/// thread pool. The thread pool is then responsible for polling the
/// future until it completes.
///
/// This method simply wraps a call to `tokio::runtime::Runtime::spawn`,
/// instrumenting the spawned future beforehand.
pub fn spawn<F>(&mut self, future: F) -> &mut Self
where
F: Future<Item = (), Error = ()> + Send + 'static,
{
let future = self.with_dispatch(future);
self.inner.spawn(future);
self
}
/// Run a future to completion on the Tokio runtime, in the context of this
/// `WithDispatch`'s trace dispatcher.
///
/// This runs the given future on the runtime, blocking until it is
/// complete, and yielding its resolved result. Any tasks or timers which
/// the future spawns internally will be executed on the runtime.
///
/// This method should not be called from an asynchronous context.
///
/// This method simply wraps a call to `tokio::runtime::Runtime::block_on`,
/// instrumenting the spawned future beforehand.
///
/// # Panics
///
/// This function panics if the executor is at capacity, if the provided
/// future panics, or if called within an asynchronous execution context.
pub fn block_on<F, R, E>(&mut self, future: F) -> Result<R, E>
where
F: Send + 'static + Future<Item = R, Error = E>,
R: Send + 'static,
E: Send + 'static,
{
let future = self.with_dispatch(future);
self.inner.block_on(future)
}
/// Return a handle to the runtime's executor, in the context of this
/// `WithDispatch`'s trace dispatcher.
///
/// The returned handle can be used to spawn tasks that run on this runtime.
///
/// The instrumented handle functions identically to a
/// `tokio::runtime::TaskExecutor`, but instruments the spawned
/// futures prior to spawning them.
pub fn executor(&self) -> WithDispatch<TaskExecutor> {
self.with_dispatch(self.inner.executor())
}
}
impl WithDispatch<current_thread::Runtime> {
/// Spawn a future onto the single-threaded Tokio runtime, in the context
/// of this `WithDispatch`'s trace dispatcher.
///
/// This method simply wraps a call to `current_thread::Runtime::spawn`,
/// instrumenting the spawned future beforehand.
pub fn spawn<F>(&mut self, future: F) -> &mut Self
where
F: Future<Item = (), Error = ()> + 'static,
{
let future = self.with_dispatch(future);
self.inner.spawn(future);
self
}
/// Runs the provided future in the context of this `WithDispatch`'s trace
/// dispatcher, blocking the current thread until the future completes.
///
/// This function can be used to synchronously block the current thread
/// until the provided `future` has resolved either successfully or with an
/// error. The result of the future is then returned from this function
/// call.
///
/// Note that this function will **also** execute any spawned futures on the
/// current thread, but will **not** block until these other spawned futures
/// have completed. Once the function returns, any uncompleted futures
/// remain pending in the `Runtime` instance. These futures will not run
/// until `block_on` or `run` is called again.
///
/// The caller is responsible for ensuring that other spawned futures
/// complete execution by calling `block_on` or `run`.
///
/// This method simply wraps a call to `current_thread::Runtime::block_on`,
/// instrumenting the spawned future beforehand.
///
/// # Panics
///
/// This function panics if the executor is at capacity, if the provided
/// future panics, or if called within an asynchronous execution context.
pub fn block_on<F, R, E>(&mut self, future: F) -> Result<R, E>
where
F: 'static + Future<Item = R, Error = E>,
R: 'static,
E: 'static,
{
let future = self.with_dispatch(future);
self.inner.block_on(future)
}
/// Get a new handle to spawn futures on the single-threaded Tokio runtime,
/// in the context of this `WithDispatch`'s trace dispatcher.\
///
/// Different to the runtime itself, the handle can be sent to different
/// threads.
///
/// The instrumented handle functions identically to a
/// `tokio::runtime::current_thread::Handle`, but the spawned
/// futures are run in the context of the trace dispatcher.
pub fn handle(&self) -> WithDispatch<current_thread::Handle> {
self.with_dispatch(self.inner.handle())
}
}
}