1
0

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::{ use crate::{
context::Context, context::Context,
error::{PoisonError, RuntimeError, ValidationError}, error::{RuntimeError, ValidationError},
}; };
use super::{Evaluation, ExpectedType, Run, Statement, Type, Validate}; use super::{Evaluation, ExpectedType, Run, Statement, Type, Validate};
@ -43,28 +43,23 @@ impl Run for AsyncBlock {
self.statements self.statements
.into_par_iter() .into_par_iter()
.enumerate() .enumerate()
.find_map_any(|(index, statement)| { .find_map_any(
|(index, statement)| -> Option<Result<Option<Evaluation>, RuntimeError>> {
let result = statement.run(&mut _context.clone(), false); let result = statement.run(&mut _context.clone(), false);
if index == statement_count - 1 { if result.is_err() {
let get_write_lock = final_result.lock(); 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 None
} },
Err(_error) => Some(Err(RuntimeError::RwLockPoison(PoisonError))), )
} .unwrap_or(final_result.into_inner()?)
} else {
None
}
})
.unwrap_or(
final_result
.into_inner()
.map_err(|_| RuntimeError::RwLockPoison(PoisonError)),
)?
} }
} }