From 0b06c62fd9984c08938e1092a5555bbfb1d88101 Mon Sep 17 00:00:00 2001 From: Henri Bourcereau Date: Wed, 25 Mar 2026 16:04:06 +0100 Subject: [PATCH] refact: add cargo "python" feature for pyo3 --- README.md | 2 +- bot/Cargo.toml | 2 +- client_cli/Cargo.toml | 2 +- spiel_bot/Cargo.toml | 2 +- store/Cargo.toml | 6 +++++- store/src/lib.rs | 1 + store/src/player.rs | 3 ++- 7 files changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e5a0f39..e74fb69 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Training of AI bots is the work in progress. - game rules and game state are implemented in the _store/_ folder. - the command-line application is implemented in _client_cli/_; it allows you to play against a bot, or to have two bots play against each other -- the bots algorithms and the training of their models are implemented in the _bot/_ folder +- the bots algorithms and the training of their models are implemented in the _bot/_ and _spiel_bot_ folders. ### _store_ package diff --git a/bot/Cargo.toml b/bot/Cargo.toml index de957df..d24adcc 100644 --- a/bot/Cargo.toml +++ b/bot/Cargo.toml @@ -13,7 +13,7 @@ path = "src/burnrl/main.rs" pretty_assertions = "1.4.0" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -trictrac-store = { path = "../store" } +trictrac-store = { path = "../store", features = ["python"] } rand = "0.9" env_logger = "0.10" burn = { version = "0.20", features = ["ndarray", "autodiff"] } diff --git a/client_cli/Cargo.toml b/client_cli/Cargo.toml index 52318cb..d85dd8b 100644 --- a/client_cli/Cargo.toml +++ b/client_cli/Cargo.toml @@ -13,7 +13,7 @@ bincode = "1.3.3" pico-args = "0.5.0" pretty_assertions = "1.4.0" renet = "0.0.13" -trictrac-store = { path = "../store" } +trictrac-store = { path = "../store", features = ["python"] } trictrac-bot = { path = "../bot" } spiel_bot = { path = "../spiel_bot" } itertools = "0.13.0" diff --git a/spiel_bot/Cargo.toml b/spiel_bot/Cargo.toml index b541adc..1458d66 100644 --- a/spiel_bot/Cargo.toml +++ b/spiel_bot/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -trictrac-store = { path = "../store" } +trictrac-store = { path = "../store", features = ["python"] } trictrac-bot = { path = "../bot" } anyhow = "1" rand = "0.9" diff --git a/store/Cargo.toml b/store/Cargo.toml index 935a2a0..fbb4f6d 100644 --- a/store/Cargo.toml +++ b/store/Cargo.toml @@ -12,6 +12,10 @@ name = "trictrac_store" # "staticlib" → used by the C++ OpenSpiel game (cxxengine) crate-type = ["cdylib", "rlib", "staticlib"] +[features] +# Enable Python bindings (required for maturin / AI training). Not available on wasm32. +python = ["pyo3"] + [dependencies] anyhow = "1.0" base64 = "0.21.7" @@ -20,7 +24,7 @@ cxx = "1.0" log = "0.4.20" merge = "0.1.0" # generate python lib (with maturin) to be used in AI training -pyo3 = { version = "0.23", features = ["extension-module", "abi3-py38"] } +pyo3 = { version = "0.23", features = ["extension-module", "abi3-py38"], optional = true } rand = "0.9" serde = { version = "1.0", features = ["derive"] } transpose = "0.2.2" diff --git a/store/src/lib.rs b/store/src/lib.rs index 4fc8dff..25d2dcb 100644 --- a/store/src/lib.rs +++ b/store/src/lib.rs @@ -20,6 +20,7 @@ pub use dice::{Dice, DiceRoller}; pub mod training_common; // python interface "trictrac_engine" (for AI training..) +#[cfg(feature = "python")] mod pyengine; // C++ interface via cxx.rs (for OpenSpiel C++ integration) diff --git a/store/src/player.rs b/store/src/player.rs index 1e48593..cca02b5 100644 --- a/store/src/player.rs +++ b/store/src/player.rs @@ -1,3 +1,4 @@ +#[cfg(feature = "python")] use pyo3::prelude::*; use serde::{Deserialize, Serialize}; use std::fmt; @@ -5,7 +6,7 @@ use std::fmt; // This just makes it easier to dissern between a player id and any ol' u64 pub type PlayerId = u64; -#[pyclass(eq, eq_int)] +#[cfg_attr(feature = "python", pyclass(eq, eq_int))] #[derive(Copy, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub enum Color { White,