Skip to content

Commit e0b41b0

Browse files
committed
Add enter_trace_span! that checks #[cfg("tracing")]
1 parent 6acaee8 commit e0b41b0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/helpers.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,3 +1431,25 @@ impl ToU64 for usize {
14311431
self.try_into().unwrap()
14321432
}
14331433
}
1434+
1435+
/// This struct is needed to enforce `#[must_use]` on values produced by [enter_trace_span] even
1436+
/// when the "tracing" feature is not enabled.
1437+
#[must_use]
1438+
pub struct MaybeEnteredSpan {
1439+
#[cfg(feature = "tracing")]
1440+
pub _entered_span: tracing::span::EnteredSpan,
1441+
}
1442+
1443+
/// Enters a [tracing::info_span] only if the "tracing" feature is enabled, otherwise does nothing.
1444+
/// This is like [rustc_const_eval::enter_trace_span] except that it does not depend on the
1445+
/// [Machine] trait to check if tracing is enabled, because from the Miri codebase we can directly
1446+
/// check whether the "tracing" feature is enabled, unlike from the rustc_const_eval codebase.
1447+
#[macro_export]
1448+
macro_rules! enter_trace_span {
1449+
($($tt:tt)*) => {
1450+
$crate::helpers::MaybeEnteredSpan {
1451+
#[cfg(feature = "tracing")]
1452+
_entered_span: tracing::info_span!($($tt)*).entered()
1453+
}
1454+
}
1455+
}

0 commit comments

Comments
 (0)