Struct usi::UsiEngineHandler
source · [−]pub struct UsiEngineHandler { /* private fields */ }
Expand description
UsiEngineHandler
provides a type-safe interface to the USI engine process.
Examples
use usi::{BestMoveParams, Error, EngineCommand, GuiCommand, UsiEngineHandler};
let mut handler = UsiEngineHandler::spawn("/path/to/usi_engine", "/path/to/working_dir").unwrap();
// Get the USI engine information.
let info = handler.get_info().unwrap();
assert_eq!("engine name", info.name());
// Set options and prepare the engine.
handler.send_command(&GuiCommand::SetOption("USI_Ponder".to_string(), Some("true".to_string()))).unwrap();
handler.prepare().unwrap();
handler.send_command(&GuiCommand::UsiNewGame).unwrap();
// Start listening to the engine output.
// You can pass the closure which will be called
// everytime new command is received from the engine.
handler.listen(move |output| -> Result<(), Error> {
match output.response() {
Some(EngineCommand::BestMove(BestMoveParams::MakeMove(
ref best_move_sfen,
ref ponder_move,
))) => {
assert_eq!("5g5f", best_move_sfen);
}
_ => {}
}
Ok(())
}).unwrap();
handler.send_command(&GuiCommand::Usi).unwrap();
Implementations
sourceimpl UsiEngineHandler
impl UsiEngineHandler
sourcepub fn spawn<P: AsRef<OsStr>, Q: AsRef<Path>>(
engine_path: P,
working_dir: Q
) -> Result<Self, Error>
pub fn spawn<P: AsRef<OsStr>, Q: AsRef<Path>>(
engine_path: P,
working_dir: Q
) -> Result<Self, Error>
Spanws a new process of the specific USI engine.
sourcepub fn get_info(&mut self) -> Result<EngineInfo, Error>
pub fn get_info(&mut self) -> Result<EngineInfo, Error>
Request metadata such as a name and available options.
Internally get_info()
sends usi
command and
records id
and option
commands until usiok
is received.
Returns Error::IllegalOperation
when called after listen
method.
sourcepub fn prepare(&mut self) -> Result<(), Error>
pub fn prepare(&mut self) -> Result<(), Error>
Prepare the engine to be ready to start a new game.
Internally, prepare()
sends isready
command and waits until readyok
is received.
Returns Error::IllegalOperation
when called after listen
method.
sourcepub fn send_command(&mut self, command: &GuiCommand) -> Result<(), Error>
pub fn send_command(&mut self, command: &GuiCommand) -> Result<(), Error>
Sends a command to the engine.
sourcepub fn listen<F, E>(&mut self, hook: F) -> Result<(), Error> where
F: FnMut(&EngineOutput) -> Result<(), E> + Send + 'static,
E: Error + Send + Sync + 'static,
pub fn listen<F, E>(&mut self, hook: F) -> Result<(), Error> where
F: FnMut(&EngineOutput) -> Result<(), E> + Send + 'static,
E: Error + Send + Sync + 'static,
Spanws a new thread to monitor outputs from the engine.
hook
will be called for each USI command received.
prepare
method can only be called before listen
method.
Trait Implementations
sourceimpl Debug for UsiEngineHandler
impl Debug for UsiEngineHandler
Auto Trait Implementations
impl RefUnwindSafe for UsiEngineHandler
impl Send for UsiEngineHandler
impl Sync for UsiEngineHandler
impl Unpin for UsiEngineHandler
impl UnwindSafe for UsiEngineHandler
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more