From 4fd9203800f88fa42363efea944951fd4d61ec19 Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Thu, 16 Apr 2020 23:20:57 +0100 Subject: [PATCH] Bring back RefCell It was accidentally removed as part of #208. Fixes #214. --- src/evaluate.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/evaluate.rs b/src/evaluate.rs index ba58a8f0..254a0928 100644 --- a/src/evaluate.rs +++ b/src/evaluate.rs @@ -12,14 +12,15 @@ use crate::png::STD_WINDOW; #[cfg(not(feature = "parallel"))] use crate::rayon; use crate::Deadline; -#[cfg(feature = "parallel")] -use rayon; use rayon::prelude::*; +#[cfg(not(feature = "parallel"))] +use std::cell::RefCell; use std::sync::atomic::AtomicUsize; use std::sync::atomic::Ordering::SeqCst; #[cfg(feature = "parallel")] use std::sync::mpsc::*; use std::sync::Arc; +#[cfg(feature = "parallel")] use std::thread; struct Candidate { @@ -54,7 +55,7 @@ pub(crate) struct Evaluator { eval_thread: thread::JoinHandle>, // in non-parallel mode, images are evaluated synchronously #[cfg(not(feature = "parallel"))] - eval_best_candidate: Option, + eval_best_candidate: RefCell>, } impl Evaluator { @@ -70,7 +71,7 @@ impl Evaluator { #[cfg(feature = "parallel")] eval_thread: thread::spawn(move || rx.into_iter().min_by_key(Candidate::cmp_key)), #[cfg(not(feature = "parallel"))] - eval_best_candidate: None, + eval_best_candidate: RefCell::new(None), } } @@ -84,7 +85,7 @@ impl Evaluator { #[cfg(not(feature = "parallel"))] fn get_best_candidate(self) -> Option { - self.eval_best_candidate + self.eval_best_candidate.into_inner() } pub fn get_result(self) -> Option { @@ -151,9 +152,9 @@ impl Evaluator { #[cfg(not(feature = "parallel"))] { - match self.eval_best_candidate { + match &mut *self.eval_best_candidate.borrow_mut() { Some(prev) if prev.cmp_key() < new.cmp_key() => {} - _ => self.eval_best_candidate = Some(new), + best => *best = Some(new), } } }