1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use atty::{self, Stream};
use libc::{self, signal};
use std::{fmt::Display, time::SystemTime};
pub fn print_solution(verdict: &str) {
write_to_stdout!("s {}\n", verdict);
}
pub fn print_key_value(key: &str, value: impl Display) {
requires!(key.len() < 35);
comment!("{:<35} {:>15}", format!("{}:", key), value);
}
pub fn install_signal_handler() {
assert!(unsafe { signal(libc::SIGPIPE, libc::SIG_DFL) } != libc::SIG_ERR);
}
pub fn unreachable() -> ! {
invariant!(false, "unreachable");
unsafe { std::hint::unreachable_unchecked() }
}
pub fn is_a_tty() -> bool {
atty::is(Stream::Stdout)
}
pub struct Timer {
name: &'static str,
start: SystemTime,
pub disabled: bool,
}
impl Timer {
pub fn name(name: &'static str) -> Timer {
Timer {
name,
start: SystemTime::now(),
disabled: false,
}
}
}
impl Drop for Timer {
fn drop(&mut self) {
if self.disabled {
return;
}
let elapsed_time = self.start.elapsed().expect("failed to get time");
print_key_value(
&format!("{} (s)", self.name),
format!(
"{}.{:03}",
elapsed_time.as_secs(),
elapsed_time.subsec_millis()
),
);
}
}