Optionally instruments a blocking closure with custom tracing. If a tracer has been injected via `set_tracer`, the closure is wrapped so that its return value is boxed (erasing its type), passed to the tracer, and then the result is downcast back to the original type. If no tracer is set, the closure is returned unmodified (except for being boxed). # Type Parameters `T` - The concrete return typ
(f: F)
| 165 | /// # Parameters |
| 166 | /// * `f` - The blocking closure to potentially instrument. |
| 167 | pub fn trace_block<T, F>(f: F) -> Box<dyn FnOnce() -> T + Send> |
| 168 | where |
| 169 | F: FnOnce() -> T + Send + 'static, |
| 170 | T: Send + 'static, |
| 171 | { |
| 172 | // Erase the closure’s return type first: |
| 173 | let erased_closure = Box::new(|| { |
| 174 | let result = f(); |
| 175 | Box::new(result) as Box<dyn Any + Send> |
| 176 | }); |
| 177 | |
| 178 | // Forward through the global tracer: |
| 179 | let traced_closure = get_tracer().trace_block(erased_closure); |
| 180 | |
| 181 | // Downcast from `Box<dyn Any + Send>` back to `T`: |
| 182 | Box::new(move || { |
| 183 | let any_box = traced_closure(); |
| 184 | *any_box |
| 185 | .downcast::<T>() |
| 186 | .expect("Tracer must preserve the closure’s return type!") |
| 187 | }) |
| 188 | } |
no test coverage detected
searching dependent graphs…