From f672095ecf7a09b85a93e3c2dcfddff6b65dd21c Mon Sep 17 00:00:00 2001 From: Techcable Date: Tue, 2 Jan 2024 20:47:40 -0700 Subject: [PATCH] Move ErrorRef wrapping logic to __slog_builtin! Avoids duplicating multiple times across multiple macros. Explicitly suppress unused_import warning (needed with clippy deny warnings) --- src/lib.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f86cf3bf..b2329751 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -405,10 +405,10 @@ macro_rules! kv( kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@format_args "{:#?}", $v))), $args_ready); $($args)* ) }; (@ $args_ready:expr; $k:expr => #$v:expr) => { - kv!(@ ($crate::SingleKV::from(($k, { use $crate::{ErrorRefKind, ErrorValueKind}; let v = $v; let w = $crate::ErrorTagWrapper(v); (&&w).slog_error_kind().wrap(w.0) } )), $args_ready); ) + kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@wrap_error $v) )), $args_ready); ) }; (@ $args_ready:expr; $k:expr => #$v:expr, $($args:tt)* ) => { - kv!(@ ($crate::SingleKV::from(($k, { use $crate::{ErrorRefKind, ErrorValueKind}; let v = $v; let w = $crate::ErrorTagWrapper(v); (&&w).slog_error_kind().wrap(w.0) } )), $args_ready); $($args)* ) + kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@wrap_error $v))), $args_ready); $($args)* ) }; (@ $args_ready:expr; $k:expr => $v:expr) => { kv!(@ ($crate::SingleKV::from(($k, $v)), $args_ready); ) @@ -462,10 +462,10 @@ macro_rules! slog_kv( slog_kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@format_args "{:#?}", $v))), $args_ready); $($args)* ) }; (@ $args_ready:expr; $k:expr => #$v:expr) => { - slog_kv!(@ ($crate::SingleKV::from(($k, { use $crate::{ErrorRefKind, ErrorValueKind}; let v = $v; let w = $crate::ErrorTagWrapper(v); (&&w).slog_error_kind().wrap(w.0) } )), $args_ready); ) + slog_kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@wrap_error $v) )), $args_ready); ) }; (@ $args_ready:expr; $k:expr => #$v:expr, $($args:tt)* ) => { - slog_kv!(@ ($crate::SingleKV::from(($k, { use $crate::{ErrorRefKind, ErrorValueKind}; let v = $v; let w = $crate::ErrorTagWrapper(v); (&&w).slog_error_kind().wrap(w.0) } )), $args_ready); $($args)* ) + slog_kv!(@ ($crate::SingleKV::from(($k, __slog_builtin!(@wrap_error $v) )), $args_ready); $($args)* ) }; (@ $args_ready:expr; $k:expr => $v:expr) => { slog_kv!(@ ($crate::SingleKV::from(($k, $v)), $args_ready); ) @@ -1068,6 +1068,16 @@ macro_rules! __slog_builtin { (@line) => ( line!() ); (@column) => ( column!() ); (@module_path) => ( module_path!() ); + (@wrap_error $v:expr) => ({ + // this magical sequence of code is used to wrap either with + // slog::ErrorValue or slog::ErrorRef as appropriate + // See PR #328 for details + #[allow(unused_imports)] + use $crate::{ErrorRefKind, ErrorValueKind}; + let v = $v; + let w = $crate::ErrorTagWrapper(v); + (&&w).slog_error_kind().wrap(w.0) + }); } // }}}