use std::{ io::{self, Write}, os::unix::net::UnixStream, path::Path, process::Command, }; use crate::read_line; pub trait BinderArg { fn from_u8_bind(b: u8) -> Self; } pub struct DynLib { stream: UnixStream, } impl DynLib { pub fn load(dyn_lib: &str) -> io::Result { if !Path::new(&format!("/tmp/{dyn_lib}")).exists() { let path = format!("./{dyn_lib}.dyn"); Command::new(path).spawn().unwrap(); std::thread::sleep(std::time::Duration::from_millis(100)); } Ok(Self { stream: UnixStream::connect(format!("/tmp/{dyn_lib}"))?, }) } pub fn call(&mut self, fn_id: u8) -> Option { self.stream.write_all(&[fn_id, 255]).ok()?; self.stream.flush().ok()?; let msg = read_line(&mut self.stream)?; Some(T::from_u8_bind(msg[0])) } } impl BinderArg for () { fn from_u8_bind(_: u8) -> Self {} }