From 77cf02b114386e0b7e6055ae77abda7d2612d2c3 Mon Sep 17 00:00:00 2001 From: CAD97 Date: Wed, 2 Mar 2022 17:00:58 -0600 Subject: [PATCH] Relax hook to work on non-'static diagnostics --- src/eyreish/mod.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/eyreish/mod.rs b/src/eyreish/mod.rs index 386daba..b9b15b1 100644 --- a/src/eyreish/mod.rs +++ b/src/eyreish/mod.rs @@ -54,8 +54,7 @@ pub struct Report { } /// -pub type ErrorHook = - Box Box + Sync + Send + 'static>; +pub type ErrorHook = Box Box + Sync + Send>; static HOOK: OnceCell = OnceCell::new(); @@ -80,10 +79,19 @@ pub fn set_hook(hook: ErrorHook) -> Result<(), InstallError> { HOOK.set(hook).map_err(|_| InstallError) } +/** +Get the error hook. + +If the error hook has not been set, it is set to its default value. +*/ +pub fn get_hook() -> &'static ErrorHook { + HOOK.get_or_init(|| Box::new(get_default_printer)) +} + #[cfg_attr(track_caller, track_caller)] #[cfg_attr(not(track_caller), allow(unused_mut))] fn capture_handler(error: &(dyn Diagnostic + 'static)) -> Box { - let hook = HOOK.get_or_init(|| Box::new(get_default_printer)).as_ref(); + let hook = get_hook().as_ref(); #[cfg(track_caller)] { @@ -97,7 +105,7 @@ fn capture_handler(error: &(dyn Diagnostic + 'static)) -> Box } } -fn get_default_printer(_err: &(dyn Diagnostic + 'static)) -> Box { +fn get_default_printer(_err: &dyn Diagnostic) -> Box { #[cfg(feature = "fancy")] return Box::new(MietteHandler::new()); #[cfg(not(feature = "fancy"))]