]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_span/src/profiling.rs
Auto merge of #107328 - matthiaskrgr:rollup-lfqwo0o, r=matthiaskrgr
[rust.git] / compiler / rustc_span / src / profiling.rs
1 use std::borrow::Borrow;
2
3 use rustc_data_structures::profiling::EventArgRecorder;
4
5 /// Extension trait for self-profiling purposes: allows to record spans within a generic activity's
6 /// event arguments.
7 pub trait SpannedEventArgRecorder {
8     /// Records the following event arguments within the current generic activity being profiled:
9     /// - the provided `event_arg`
10     /// - a string representation of the provided `span`
11     ///
12     /// Note: when self-profiling with costly event arguments, at least one argument
13     /// needs to be recorded. A panic will be triggered if that doesn't happen.
14     fn record_arg_with_span<A>(&mut self, event_arg: A, span: crate::Span)
15     where
16         A: Borrow<str> + Into<String>;
17 }
18
19 impl SpannedEventArgRecorder for EventArgRecorder<'_> {
20     fn record_arg_with_span<A>(&mut self, event_arg: A, span: crate::Span)
21     where
22         A: Borrow<str> + Into<String>,
23     {
24         self.record_arg(event_arg);
25
26         let span_arg = crate::with_session_globals(|session_globals| {
27             if let Some(source_map) = &*session_globals.source_map.borrow() {
28                 source_map.span_to_embeddable_string(span)
29             } else {
30                 format!("{span:?}")
31             }
32         });
33         self.record_arg(span_arg);
34     }
35 }