UsiEngineHandler

Struct 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§

Source§

impl UsiEngineHandler

Source

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.

Source

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.

Source

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.

Source

pub fn send_command(&mut self, command: &GuiCommand) -> Result<(), Error>

Sends a command to the engine.

Source

pub fn kill(&mut self) -> Result<(), Error>

Terminates the engine.

Source

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§

Source§

impl Debug for UsiEngineHandler

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for UsiEngineHandler

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.