0

我正在尝试在 Rust 中实现一个基本的迭代深化算法:

pub async fn iterative_best_move(pos: &mut Position, col: PieceColour, timeout: u64) -> (Option<movegen::Move>, i32) {
    let mut ret = (None, -1);
    let mut time_remaining = std::time::Duration::from_secs(timeout);
    let mut current_depth = 3;

    loop {
        let start = time::Instant::now();

        let mut pos = pos.shallow_clone();

        let x = tokio::time::timeout(time_remaining, async move {
            depth_best_move(&mut pos, col, current_depth).await
        }).await;

        if let Err(_) = x {
            // We ran out of time!
            println!("Time's up!");
            break;
        }

        ret = x.unwrap().unwrap();

        time_remaining -= start.elapsed();
        current_depth += 1;
    }

    println!("Depth {} in {} seconds.", current_depth, timeout);

    ret
}

我的实现的问题是它似乎永远不会超时,而是继续下一个循环迭代并在它试图表示负持续时间时引起恐慌。

从 main 调用该函数,如下所示:

#[tokio::main]
async fn main() {
    let mut pos = Position::random_legal();

    let start = time::Instant::now();

    search::iterative_best_move(&mut pos, piece::PieceColour::White, 5).await;
}

为什么函数没有按原样超时并返回?

4

0 回答 0