use crate::logging_driver::DriverOp;
use std::{
fmt,
sync::{Arc, Mutex},
};
use toasty_core::driver::{ExecResponse, Operation};
pub struct ExecLog {
ops: Arc<Mutex<Vec<DriverOp>>>,
}
impl ExecLog {
pub(crate) fn new(ops: Arc<Mutex<Vec<DriverOp>>>) -> Self {
Self { ops }
}
pub fn len(&self) -> usize {
self.ops.lock().unwrap().len()
}
pub fn is_empty(&self) -> bool {
self.ops.lock().unwrap().is_empty()
}
pub fn clear(&mut self) {
self.ops.lock().unwrap().clear();
}
#[track_caller]
pub fn pop(&mut self) -> (Operation, ExecResponse) {
let mut ops = self.ops.lock().unwrap();
if ops.is_empty() {
panic!("no operations in log");
} else {
let driver_op = ops.remove(0);
(driver_op.operation, driver_op.response)
}
}
#[track_caller]
pub fn pop_op(&mut self) -> Operation {
self.pop().0
}
}
impl fmt::Debug for ExecLog {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let ops = self.ops.lock().unwrap();
f.debug_struct("ExecLog").field("ops", &*ops).finish()
}
}