From 9cc223c16046bd01a5a5928bef0b96a1cc576fda Mon Sep 17 00:00:00 2001 From: Rhys Lloyd Date: Tue, 9 Jun 2026 09:46:54 -0700 Subject: [PATCH] use struct in serialize impl --- card_game/src/lib.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/card_game/src/lib.rs b/card_game/src/lib.rs index 94cf754..862bd65 100644 --- a/card_game/src/lib.rs +++ b/card_game/src/lib.rs @@ -716,8 +716,18 @@ where } } - use serde::ser::SerializeStruct; - let mut map = serializer.serialize_struct("Session", 3)?; + #[derive(serde_derive::Serialize)] + #[serde(bound(serialize = " + G: serde::Serialize, + SessionConfig: serde::Serialize, + G::Instruction: serde::Serialize, + "))] + struct SerializedSession<'a, G: Game> { + config: &'a SessionConfig, + state: &'a G, + history: History<'a, G>, + } + // serialize the initial state of the game. // if there is history, it is the first snapshot's state, // otherwise it is the current game state since there are no moves. @@ -726,9 +736,11 @@ where } else { &self.state.state }; - map.serialize_field("config", &self.config)?; - map.serialize_field("state", state)?; - map.serialize_field("history", &History(&self.state.history))?; - map.end() + let session = SerializedSession { + config: &self.config, + state, + history: History(&self.state.history), + }; + session.serialize(serializer) } }