Refactor AsyncBlock::run

This commit is contained in:
Jeff 2024-06-21 18:51:42 -04:00
parent b3e04f987f
commit d98f724355

View File

@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
use crate::{
context::Context,
error::{PoisonError, RuntimeError, ValidationError},
error::{RuntimeError, ValidationError},
};
use super::{Evaluation, ExpectedType, Run, Statement, Type, Validate};
@ -43,28 +43,23 @@ impl Run for AsyncBlock {
self.statements
.into_par_iter()
.enumerate()
.find_map_any(|(index, statement)| {
.find_map_any(
|(index, statement)| -> Option<Result<Option<Evaluation>, RuntimeError>> {
let result = statement.run(&mut _context.clone(), false);
if index == statement_count - 1 {
let get_write_lock = final_result.lock();
if result.is_err() {
return Some(result);
}
if index == statement_count - 1 {
// It is safe to unwrap here because only one thread uses the Mutex
*final_result.lock().unwrap() = result;
}
match get_write_lock {
Ok(mut final_result) => {
*final_result = result;
None
}
Err(_error) => Some(Err(RuntimeError::RwLockPoison(PoisonError))),
}
} else {
None
}
})
.unwrap_or(
final_result
.into_inner()
.map_err(|_| RuntimeError::RwLockPoison(PoisonError)),
)?
},
)
.unwrap_or(final_result.into_inner()?)
}
}