[][src]Crate chess_clock

A futures-based chess clock which provides timeout-like functionality for other futures.

In addition to the typical configurable base time and time-per-turn, ChessClock supports [1, usize::max] players, which can be useful in time-controlled, turn-based games that support more than two players, such as Hanabi.

Per FIDE rules, time-per-turn is added on the very first move.

Example usage:

  extern crate futures;
  extern crate tokio;
  extern crate chess_clock;

  use chess_clock::{ChessClock, BaseTime, TimePerTurn};

  use std::thread::sleep;
  use std::time::{Duration, Instant};

  use futures::{prelude::*, future::{self, ok, err, Either, FutureResult}};
  use tokio;
  use tokio::timer::Delay;

  let mut clock = ChessClock::new(
      BaseTime(Duration::new(2, 0)),
      TimePerTurn(Duration::new(2, 0)),

  let when = Instant::now() + Duration::from_secs(2);
  let task = Delay::new(when)
      .map_err(|_| ());
  let clocked_task = clock.bind(task)
      .then(|res| {
          match res {
              Ok(Some(_)) => {
                  println!("Task succeeded");
              Ok(None) => {
                  println!("Task timed out");
              _ => {

Output: Task succeeded, since the example task has duration less than the first player's base time + time-per-turn

Implementation details

ChessClock is ARCed and (rw-mutexed)std::sync::RwLock so that it works nicely with the borrow checker, but by nature of its design, which is sequential passing of turns, it shouldn't be used simultaneously by two threads.

It tracks the time of each player, a single global time-per-turn, the active player index, and the time the last turn was passed, so it can calculate the time to subtract from the next active player's clock.



Duration newtype


Represents the combination of a future f with a chess clock through bind.


Duration newtype