rust/compiler/rustc_middle/src/util/common.rs

68 lines
1.2 KiB
Rust
Raw Normal View History

2019-09-25 21:02:50 -04:00
use rustc_data_structures::sync::Lock;
std: Rename Show/String to Debug/Display This commit is an implementation of [RFC 565][rfc] which is a stabilization of the `std::fmt` module and the implementations of various formatting traits. Specifically, the following changes were performed: [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0565-show-string-guidelines.md * The `Show` trait is now deprecated, it was renamed to `Debug` * The `String` trait is now deprecated, it was renamed to `Display` * Many `Debug` and `Display` implementations were audited in accordance with the RFC and audited implementations now have the `#[stable]` attribute * Integers and floats no longer print a suffix * Smart pointers no longer print details that they are a smart pointer * Paths with `Debug` are now quoted and escape characters * The `unwrap` methods on `Result` now require `Display` instead of `Debug` * The `Error` trait no longer has a `detail` method and now requires that `Display` must be implemented. With the loss of `String`, this has moved into libcore. * `impl<E: Error> FromError<E> for Box<Error>` now exists * `derive(Show)` has been renamed to `derive(Debug)`. This is not currently warned about due to warnings being emitted on stage1+ While backwards compatibility is attempted to be maintained with a blanket implementation of `Display` for the old `String` trait (and the same for `Show`/`Debug`) this is still a breaking change due to primitives no longer implementing `String` as well as modifications such as `unwrap` and the `Error` trait. Most code is fairly straightforward to update with a rename or tweaks of method calls. [breaking-change] Closes #21436
2015-01-20 15:45:07 -08:00
use std::fmt::Debug;
use std::time::{Duration, Instant};
#[cfg(test)]
mod tests;
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;
} 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
F: FnOnce() -> T,
{
let start = Instant::now();
let rv = f();
let duration = start.elapsed();
let mut accu = accu.lock();
2021-11-09 23:45:17 +01:00
*accu += duration;
rv
}
2019-12-22 17:42:04 -05:00
pub fn indent<R, F>(op: F) -> R
where
std: Rename Show/String to Debug/Display This commit is an implementation of [RFC 565][rfc] which is a stabilization of the `std::fmt` module and the implementations of various formatting traits. Specifically, the following changes were performed: [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0565-show-string-guidelines.md * The `Show` trait is now deprecated, it was renamed to `Debug` * The `String` trait is now deprecated, it was renamed to `Display` * Many `Debug` and `Display` implementations were audited in accordance with the RFC and audited implementations now have the `#[stable]` attribute * Integers and floats no longer print a suffix * Smart pointers no longer print details that they are a smart pointer * Paths with `Debug` are now quoted and escape characters * The `unwrap` methods on `Result` now require `Display` instead of `Debug` * The `Error` trait no longer has a `detail` method and now requires that `Display` must be implemented. With the loss of `String`, this has moved into libcore. * `impl<E: Error> FromError<E> for Box<Error>` now exists * `derive(Show)` has been renamed to `derive(Debug)`. This is not currently warned about due to warnings being emitted on stage1+ While backwards compatibility is attempted to be maintained with a blanket implementation of `Display` for the old `String` trait (and the same for `Show`/`Debug`) this is still a breaking change due to primitives no longer implementing `String` as well as modifications such as `unwrap` and the `Error` trait. Most code is fairly straightforward to update with a rename or tweaks of method calls. [breaking-change] Closes #21436
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.
debug!(">>");
let r = op();
debug!("<< (Result = {:?})", r);
2013-02-15 04:14:34 -05:00
r
2012-04-05 20:59:07 -07:00
}
pub struct Indenter {
2017-03-13 02:12:13 +02:00
_cannot_construct_outside_of_this_module: (),
}
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
}
pub fn indenter() -> Indenter {
debug!(">>");
Indenter { _cannot_construct_outside_of_this_module: () }
2012-04-05 20:59:07 -07:00
}