Avoid using time API when we don't need it

This avoids a syscall to the time API when the result is ignored later anyway.

This allows to use the library with default options on wasm32-unknown-unknown, where the unimplemented syscall would panic otherwise.
This commit is contained in:
Ingvar Stepanyan 2019-08-05 21:05:20 +01:00
parent 239ca81db7
commit 48a136b3ba

View file

@ -784,19 +784,25 @@ fn perform_reductions(
try_alpha_reductions(png, &opts.alphas, eval); try_alpha_reductions(png, &opts.alphas, eval);
} }
struct DeadlineImp {
start: Instant,
timeout: Duration,
print_message: AtomicBool,
}
/// Keep track of processing timeout /// Keep track of processing timeout
pub(crate) struct Deadline { pub(crate) struct Deadline {
start: Instant, imp: Option<DeadlineImp>,
timeout: Option<Duration>,
print_message: AtomicBool,
} }
impl Deadline { impl Deadline {
pub fn new(timeout: Option<Duration>, verbose: bool) -> Self { pub fn new(timeout: Option<Duration>, verbose: bool) -> Self {
Self { Self {
imp: timeout.map(|timeout| DeadlineImp {
start: Instant::now(), start: Instant::now(),
timeout, timeout,
print_message: AtomicBool::new(verbose), print_message: AtomicBool::new(verbose),
})
} }
} }
@ -804,11 +810,11 @@ impl Deadline {
/// ///
/// If the verbose option is on, it also prints a timeout message once. /// If the verbose option is on, it also prints a timeout message once.
pub fn passed(&self) -> bool { pub fn passed(&self) -> bool {
if let Some(timeout) = self.timeout { if let Some(imp) = &self.imp {
let elapsed = self.start.elapsed(); let elapsed = imp.start.elapsed();
if elapsed > timeout { if elapsed > imp.timeout {
if self.print_message.load(Ordering::Relaxed) { if imp.print_message.load(Ordering::Relaxed) {
self.print_message.store(false, Ordering::Relaxed); imp.print_message.store(false, Ordering::Relaxed);
eprintln!("Timed out after {} second(s)", elapsed.as_secs()); eprintln!("Timed out after {} second(s)", elapsed.as_secs());
} }
return true; return true;