]> git.lizzy.rs Git - rust.git/blob - src/librustc/util/common.rs
Auto merge of #67312 - cuviper:clone-box-slice, r=SimonSapin
[rust.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 use rustc_span::symbol::{sym, Symbol};
9
10 #[cfg(test)]
11 mod tests;
12
13 // The name of the associated type for `Fn` return types.
14 pub const FN_OUTPUT_NAME: Symbol = sym::Output;
15
16 pub use errors::ErrorReported;
17
18 pub fn to_readable_str(mut val: usize) -> String {
19     let mut groups = vec![];
20     loop {
21         let group = val % 1000;
22
23         val /= 1000;
24
25         if val == 0 {
26             groups.push(group.to_string());
27             break;
28         } else {
29             groups.push(format!("{:03}", group));
30         }
31     }
32
33     groups.reverse();
34
35     groups.join("_")
36 }
37
38 pub fn record_time<T, F>(accu: &Lock<Duration>, f: F) -> T
39 where
40     F: FnOnce() -> T,
41 {
42     let start = Instant::now();
43     let rv = f();
44     let duration = start.elapsed();
45     let mut accu = accu.lock();
46     *accu = *accu + duration;
47     rv
48 }
49
50 pub fn indent<R, F>(op: F) -> R
51 where
52     R: Debug,
53     F: FnOnce() -> R,
54 {
55     // Use in conjunction with the log post-processor like `src/etc/indenter`
56     // to make debug output more readable.
57     debug!(">>");
58     let r = op();
59     debug!("<< (Result = {:?})", r);
60     r
61 }
62
63 pub struct Indenter {
64     _cannot_construct_outside_of_this_module: (),
65 }
66
67 impl Drop for Indenter {
68     fn drop(&mut self) {
69         debug!("<<");
70     }
71 }
72
73 pub fn indenter() -> Indenter {
74     debug!(">>");
75     Indenter { _cannot_construct_outside_of_this_module: () }
76 }