2019-09-25 21:02:50 -04:00
|
|
|
use rustc_data_structures::sync::Lock;
|
2018-04-01 08:17:25 +02:00
|
|
|
|
2015-01-20 15:45:07 -08:00
|
|
|
use std::fmt::Debug;
|
2016-08-23 13:23:58 -04:00
|
|
|
use std::time::{Duration, Instant};
|
2014-11-10 12:27:56 -08:00
|
|
|
|
2019-08-01 03:35:26 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2016-11-04 11:37:39 -04:00
|
|
|
pub fn to_readable_str(mut val: usize) -> String {
|
|
|
|
let mut groups = vec![];
|
|
|
|
loop {
|
|
|
|
let group = val % 1000;
|
|
|
|
|
|
|
|
val /= 1000;
|
|
|
|
|
|
|
|
if val == 0 {
|
2018-07-27 11:11:18 +02:00
|
|
|
groups.push(group.to_string());
|
2017-03-13 02:12:13 +02:00
|
|
|
break;
|
2016-11-04 11:37:39 -04:00
|
|
|
} else {
|
|
|
|
groups.push(format!("{:03}", group));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
groups.reverse();
|
|
|
|
|
|
|
|
groups.join("_")
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn record_time<T, F>(accu: &Lock<Duration>, f: F) -> T
|
|
|
|
where
|
2016-08-23 13:23:58 -04:00
|
|
|
F: FnOnce() -> T,
|
|
|
|
{
|
|
|
|
let start = Instant::now();
|
|
|
|
let rv = f();
|
|
|
|
let duration = start.elapsed();
|
2018-04-01 08:17:25 +02:00
|
|
|
let mut accu = accu.lock();
|
2021-11-09 23:45:17 +01:00
|
|
|
*accu += duration;
|
2016-08-23 13:23:58 -04:00
|
|
|
rv
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn indent<R, F>(op: F) -> R
|
|
|
|
where
|
2015-01-20 15:45:07 -08:00
|
|
|
R: Debug,
|
2014-12-08 20:26:43 -05:00
|
|
|
F: FnOnce() -> R,
|
|
|
|
{
|
2012-04-05 20:59:07 -07:00
|
|
|
// Use in conjunction with the log post-processor like `src/etc/indenter`
|
|
|
|
// to make debug output more readable.
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(">>");
|
2012-10-23 11:28:20 -07:00
|
|
|
let r = op();
|
2014-12-20 00:09:35 -08:00
|
|
|
debug!("<< (Result = {:?})", r);
|
2013-02-15 04:14:34 -05:00
|
|
|
r
|
2012-04-05 20:59:07 -07:00
|
|
|
}
|
|
|
|
|
2014-06-06 15:51:42 +02:00
|
|
|
pub struct Indenter {
|
2017-03-13 02:12:13 +02:00
|
|
|
_cannot_construct_outside_of_this_module: (),
|
2013-02-27 19:13:53 -05:00
|
|
|
}
|
|
|
|
|
2014-06-06 15:51:42 +02:00
|
|
|
impl Drop for Indenter {
|
2019-12-22 17:42:04 -05:00
|
|
|
fn drop(&mut self) {
|
|
|
|
debug!("<<");
|
|
|
|
}
|
2012-04-05 20:59:07 -07:00
|
|
|
}
|
|
|
|
|
2014-06-06 15:51:42 +02:00
|
|
|
pub fn indenter() -> Indenter {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(">>");
|
2014-06-06 15:51:42 +02:00
|
|
|
Indenter { _cannot_construct_outside_of_this_module: () }
|
2012-04-05 20:59:07 -07:00
|
|
|
}
|