]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/util/common.rs
Rollup merge of #94640 - Pointerbender:issue-71146-partial-stabilization, r=yaahc
[rust.git] / compiler / rustc_middle / src / util / common.rs
1 use rustc_data_structures::sync::Lock;
2
3 use std::fmt::Debug;
4 use std::time::{Duration, Instant};
5
6 #[cfg(test)]
7 mod tests;
8
9 pub fn to_readable_str(mut val: usize) -> String {
10     let mut groups = vec![];
11     loop {
12         let group = val % 1000;
13
14         val /= 1000;
15
16         if val == 0 {
17             groups.push(group.to_string());
18             break;
19         } else {
20             groups.push(format!("{:03}", group));
21         }
22     }
23
24     groups.reverse();
25
26     groups.join("_")
27 }
28
29 pub fn record_time<T, F>(accu: &Lock<Duration>, f: F) -> T
30 where
31     F: FnOnce() -> T,
32 {
33     let start = Instant::now();
34     let rv = f();
35     let duration = start.elapsed();
36     let mut accu = accu.lock();
37     *accu += duration;
38     rv
39 }
40
41 pub fn indent<R, F>(op: F) -> R
42 where
43     R: Debug,
44     F: FnOnce() -> R,
45 {
46     // Use in conjunction with the log post-processor like `src/etc/indenter`
47     // to make debug output more readable.
48     debug!(">>");
49     let r = op();
50     debug!("<< (Result = {:?})", r);
51     r
52 }
53
54 pub struct Indenter {
55     _cannot_construct_outside_of_this_module: (),
56 }
57
58 impl Drop for Indenter {
59     fn drop(&mut self) {
60         debug!("<<");
61     }
62 }
63
64 pub fn indenter() -> Indenter {
65     debug!(">>");
66     Indenter { _cannot_construct_outside_of_this_module: () }
67 }