From cb7e3d4f98d80c762e5405bf36adf3d0887f1f66 Mon Sep 17 00:00:00 2001 From: Dmitry Ustalov Date: Sat, 24 Aug 2024 17:01:34 +0200 Subject: [PATCH] Make Winner hashable --- python/evalica/test_evalica.py | 4 ++++ src/python.rs | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/python/evalica/test_evalica.py b/python/evalica/test_evalica.py index 3f4343d..d1875ba 100644 --- a/python/evalica/test_evalica.py +++ b/python/evalica/test_evalica.py @@ -35,6 +35,10 @@ def test_exports() -> None: assert hasattr(evalica, attr), f"missing attribute: {attr}" +def test_winner_hashable() -> None: + assert len(evalica.WINNERS) == len(set(evalica.WINNERS)) + + def test_winner_pickle() -> None: for w in evalica.WINNERS: dumped = pickle.dumps(w) diff --git a/src/python.rs b/src/python.rs index 196e9d3..4205453 100644 --- a/src/python.rs +++ b/src/python.rs @@ -17,6 +17,11 @@ impl Winner { Winner::Ignore } + fn __hash__(&self) -> u64 { + let value: u8 = self.clone().into(); + value.into() + } + fn __getstate__(&self) -> PyResult { Ok(self.clone().into()) }