use crate::{Run, RunSync, rr, string, vec};
pub struct AsyncRunner<T> {
inner: std::sync::Arc<std::sync::Mutex<T>>,
}
impl<T> AsyncRunner<T> {
pub fn new(runner: T) -> Self
where
T: RunSync + Send + 'static,
{
Self {
inner: std::sync::Arc::new(std::sync::Mutex::new(runner)),
}
}
pub fn inner(&self) -> &std::sync::Arc<std::sync::Mutex<T>> {
&self.inner
}
}
impl<T> Clone for AsyncRunner<T> {
fn clone(&self) -> Self {
Self {
inner: std::sync::Arc::clone(&self.inner),
}
}
}
impl<T> Run for AsyncRunner<T>
where
T: RunSync + Send + 'static,
T::Prepared: Clone + Send + Sync,
T::Error: Send,
{
type Error = T::Error;
type Prepared = T::Prepared;
async fn prepare(&self, program: rr::Program) -> Result<Self::Prepared, Self::Error> {
let mut guard = self.inner.lock().unwrap();
guard.prepare_sync(program)
}
async fn execute(
&self,
program: &Self::Prepared,
input: &[u8],
) -> Result<vec::Vec<u8>, Self::Error> {
let mut guard = self.inner.lock().unwrap();
guard.execute_sync(program, input)
}
async fn get_interface(&self) -> Result<string::String, Self::Error> {
let mut guard = self.inner.lock().unwrap();
guard.get_interface_sync()
}
async fn shutdown(&self) -> Result<(), Self::Error> {
let mut guard = self.inner.lock().unwrap();
guard.shutdown_sync()
}
}