| 373 | type BoxedHook = Box<dyn Fn(&Frame, &mut HookContext<Frame>) -> bool + Send + Sync>; |
| 374 | |
| 375 | fn into_boxed_hook<T: Send + Sync + 'static>( |
| 376 | hook: impl Fn(&T, &mut HookContext<T>) + Send + Sync + 'static, |
| 377 | ) -> BoxedHook { |
| 378 | Box::new(move |frame: &Frame, context: &mut HookContext<Frame>| { |
| 379 | #[cfg(nightly)] |
| 380 | { |
| 381 | frame |
| 382 | .request_ref::<T>() |
| 383 | .map(|value| hook(value, context.cast())) |
| 384 | .or_else(|| { |
| 385 | frame |
| 386 | .request_value::<T>() |
| 387 | .map(|ref value| hook(value, context.cast())) |
| 388 | }) |
| 389 | .is_some() |
| 390 | } |
| 391 | |
| 392 | // emulate the behavior from nightly by searching for |
| 393 | // - `Context::provide`: not available |
| 394 | // - `Attachment`s: provide themself, emulated by `downcast_ref` |
| 395 | #[cfg(not(nightly))] |
| 396 | matches!(frame.kind(), crate::FrameKind::Attachment(_)) |
| 397 | .then_some(frame) |
| 398 | .and_then(Frame::downcast_ref::<T>) |
| 399 | .map(|value| hook(value, context.cast())) |
| 400 | .is_some() |
| 401 | }) |
| 402 | } |
| 403 | |
| 404 | /// Holds list of hooks. |
| 405 | /// |