@@ -48,10 +48,24 @@ pub(crate) fn create_static_alloc<'tcx>(
48
48
49
49
/// A marker trait returned by [crate::interpret::Machine::enter_trace_span], identifying either a
50
50
/// real [tracing::span::EnteredSpan] in case tracing is enabled, or the dummy type `()` when
51
- /// tracing is disabled.
52
- pub trait EnteredTraceSpan { }
53
- impl EnteredTraceSpan for ( ) { }
54
- impl EnteredTraceSpan for tracing:: span:: EnteredSpan { }
51
+ /// tracing is disabled. Also see [crate::enter_trace_span!] below.
52
+ pub trait EnteredTraceSpan {
53
+ /// Allows executing an alternative function when tracing is disabled. Useful for example if you
54
+ /// want to open a trace span when tracing is enabled, and alternatively just log a line when
55
+ /// tracing is disabled.
56
+ fn or_if_tracing_disabled ( self , f : impl FnOnce ( ) ) -> Self ;
57
+ }
58
+ impl EnteredTraceSpan for ( ) {
59
+ fn or_if_tracing_disabled ( self , f : impl FnOnce ( ) ) -> Self {
60
+ f ( ) ; // tracing is disabled, execute the function
61
+ self
62
+ }
63
+ }
64
+ impl EnteredTraceSpan for tracing:: span:: EnteredSpan {
65
+ fn or_if_tracing_disabled ( self , _f : impl FnOnce ( ) ) -> Self {
66
+ self // tracing is enabled, don't execute anything
67
+ }
68
+ }
55
69
56
70
/// Shortand for calling [crate::interpret::Machine::enter_trace_span] on a [tracing::info_span!].
57
71
/// This is supposed to be compiled out when [crate::interpret::Machine::enter_trace_span] has the
@@ -112,6 +126,19 @@ impl EnteredTraceSpan for tracing::span::EnteredSpan {}
112
126
/// # type M = rustc_const_eval::const_eval::CompileTimeMachine<'static>;
113
127
/// let _span = enter_trace_span!(M, step::eval_statement, tracing_separate_thread = tracing::field::Empty);
114
128
/// ```
129
+ ///
130
+ /// ### Executing something else when tracing is disabled
131
+ ///
132
+ /// [crate::interpret::Machine::enter_trace_span] returns [EnteredTraceSpan], on which you can call
133
+ /// [EnteredTraceSpan::or_if_tracing_disabled], to e.g. log a line as an alternative to the tracing
134
+ /// span for when tracing is disabled. For example:
135
+ /// ```rust
136
+ /// # use rustc_const_eval::enter_trace_span;
137
+ /// # use rustc_const_eval::interpret::EnteredTraceSpan;
138
+ /// # type M = rustc_const_eval::const_eval::CompileTimeMachine<'static>;
139
+ /// let _span = enter_trace_span!(M, step::eval_statement)
140
+ /// .or_if_tracing_disabled(|| tracing::info!("eval_statement"));
141
+ /// ```
115
142
#[ macro_export]
116
143
macro_rules! enter_trace_span {
117
144
( $machine: ident, $name: ident :: $subname: ident $( $tt: tt) * ) => { {
0 commit comments