Does not constraint on BufRead trait, create bufreader dynamically

This commit is contained in:
Baptiste Fouques 2023-01-16 11:49:42 +01:00
parent d30cfbabfe
commit 08a39c258e
2 changed files with 7 additions and 6 deletions

View File

@ -31,8 +31,8 @@ use chess::{Game, Board, ChessMove};
/// // .. uci.init(); .. /// // .. uci.init(); ..
/// uci.push_raw("quit\n"); /// uci.push_raw("quit\n");
/// ``` /// ```
pub struct UciEngine<Fi: BufRead, Fo: Write> { pub struct UciEngine<Fi: Read, Fo: Write> {
source: Fi, source: BufReader<Fi>,
destination: Fo, destination: Fo,
uciok: bool, uciok: bool,
id: Id, id: Id,
@ -40,14 +40,15 @@ pub struct UciEngine<Fi: BufRead, Fo: Write> {
game: Game, game: Game,
} }
impl<Fi: BufRead, Fo: Write> UciEngine<Fi, Fo> { impl<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
/// Create new game manager /// Create new game manager
/// ///
/// Requires line by line input and output streams to communicate with uci engine /// Requires line by line input and output streams to communicate with uci engine
pub fn new(source: Fi, destination: Fo) -> UciEngine<Fi, Fo> { pub fn new(source: Fi, destination: Fo) -> UciEngine<Fi, Fo> {
UciEngine::<Fi, Fo>{ UciEngine::<Fi, Fo>{
source, destination, source: BufReader::new(source),
destination,
id: Id::new(), id: Id::new(),
uciok: false, uciok: false,
initial: Board::default(), initial: Board::default(),
@ -146,6 +147,7 @@ impl<Fi: BufRead, Fo: Write> UciEngine<Fi, Fo> {
/// Retrieve a line from the uci engine input stream and parse it /// Retrieve a line from the uci engine input stream and parse it
pub fn pull(&mut self) { pub fn pull(&mut self) {
let mut command = String::new(); let mut command = String::new();
match self.source.read_line(&mut command) { match self.source.read_line(&mut command) {
Ok(0) => self.terminate("Chess engine closed connection."), Ok(0) => self.terminate("Chess engine closed connection."),

View File

@ -5,7 +5,6 @@ use chess::ChessMove;
use chess_uci::*; use chess_uci::*;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
use std::io::BufReader;
use std::str::FromStr; use std::str::FromStr;
pub fn main(){ pub fn main(){
@ -23,7 +22,7 @@ pub fn main(){
}; };
let (sf_in,sf_out) = (process.stdin.expect("Program stdin"), process.stdout.expect("Program stdout")); let (sf_in,sf_out) = (process.stdin.expect("Program stdin"), process.stdout.expect("Program stdout"));
let mut uci = UciEngine::new(BufReader::new(sf_out), sf_in); let mut uci = UciEngine::new(sf_out, sf_in);
uci.init(); uci.init();
println!("Engine: {} \nby: {}", println!("Engine: {} \nby: {}",