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