MCPcopy Create free account
hub / github.com/hashintel/hash / into_boxed_hook

Function into_boxed_hook

libs/error-stack/src/fmt/hook.rs:375–402  ·  view source on GitHub ↗
(
    hook: impl Fn(&T, &mut HookContext<T>) + Send + Sync + 'static,
)

Source from the content-addressed store, hash-verified

373type BoxedHook = Box<dyn Fn(&Frame, &mut HookContext<Frame>) -> bool + Send + Sync>;
374
375fn 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///

Callers 1

insertMethod · 0.85

Calls 3

hookFunction · 0.50
mapMethod · 0.45
castMethod · 0.45

Tested by

no test coverage detected