Optionally instruments a future with custom tracing. If a tracer has been injected via `set_tracer`, the future's output is boxed (erasing its type), passed to the tracer, and then downcast back to the expected type. If no tracer is set, the original future is returned. # Type Parameters `T` - The concrete output type of the future. `F` - The future type. # Parameters `future` - The future to p
(future: F)
| 128 | /// # Parameters |
| 129 | /// * `future` - The future to potentially instrument. |
| 130 | pub fn trace_future<T, F>(future: F) -> BoxFuture<'static, T> |
| 131 | where |
| 132 | F: Future<Output = T> + Send + 'static, |
| 133 | T: Send + 'static, |
| 134 | { |
| 135 | // Erase the future’s output type first: |
| 136 | let erased_future = async move { |
| 137 | let result = future.await; |
| 138 | Box::new(result) as Box<dyn Any + Send> |
| 139 | } |
| 140 | .boxed(); |
| 141 | |
| 142 | // Forward through the global tracer: |
| 143 | get_tracer() |
| 144 | .trace_future(erased_future) |
| 145 | // Downcast from `Box<dyn Any + Send>` back to `T`: |
| 146 | .map(|any_box| { |
| 147 | *any_box |
| 148 | .downcast::<T>() |
| 149 | .expect("Tracer must preserve the future’s output type!") |
| 150 | }) |
| 151 | .boxed() |
| 152 | } |
| 153 | |
| 154 | /// Optionally instruments a blocking closure with custom tracing. |
| 155 | /// |
no test coverage detected
searching dependent graphs…