2 Commits

Author SHA1 Message Date
Quaternions 9cc223c160 use struct in serialize impl 2026-06-09 09:46:54 -07:00
Quaternions f13d08a0a8 move struct into deserialize impl 2026-06-09 09:46:44 -07:00
+29 -20
View File
@@ -653,20 +653,6 @@ where
} }
} }
#[cfg_attr(
feature = "serde",
derive(serde_derive::Deserialize),
serde(bound(deserialize = "
G: serde::Deserialize<'de>,
SessionConfig<G::Config>: serde::Deserialize<'de>,
Vec<G::Instruction>: serde::Deserialize<'de>,
"))
)]
struct SerializedSession<G: Game> {
config: SessionConfig<G::Config>,
state: G,
history: Vec<G::Instruction>,
}
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
impl<'de, G: Game<Score = i32>> serde::de::Deserialize<'de> for Session<G> impl<'de, G: Game<Score = i32>> serde::de::Deserialize<'de> for Session<G>
where where
@@ -680,6 +666,17 @@ where
where where
D: serde::Deserializer<'de>, D: serde::Deserializer<'de>,
{ {
#[derive(serde_derive::Deserialize)]
#[serde(bound(deserialize = "
G: serde::Deserialize<'de>,
SessionConfig<G::Config>: serde::Deserialize<'de>,
Vec<G::Instruction>: serde::Deserialize<'de>,
"))]
struct SerializedSession<G: Game> {
config: SessionConfig<G::Config>,
state: G,
history: Vec<G::Instruction>,
}
let serialized = SerializedSession::deserialize(deserializer)?; let serialized = SerializedSession::deserialize(deserializer)?;
let mut session = Session::new(serialized.state, serialized.config); let mut session = Session::new(serialized.state, serialized.config);
for instruction in serialized.history { for instruction in serialized.history {
@@ -719,8 +716,18 @@ where
} }
} }
use serde::ser::SerializeStruct; #[derive(serde_derive::Serialize)]
let mut map = serializer.serialize_struct("Session", 3)?; #[serde(bound(serialize = "
G: serde::Serialize,
SessionConfig<G::Config>: serde::Serialize,
G::Instruction: serde::Serialize,
"))]
struct SerializedSession<'a, G: Game> {
config: &'a SessionConfig<G::Config>,
state: &'a G,
history: History<'a, G>,
}
// serialize the initial state of the game. // serialize the initial state of the game.
// if there is history, it is the first snapshot's state, // if there is history, it is the first snapshot's state,
// otherwise it is the current game state since there are no moves. // otherwise it is the current game state since there are no moves.
@@ -729,9 +736,11 @@ where
} else { } else {
&self.state.state &self.state.state
}; };
map.serialize_field("config", &self.config)?; let session = SerializedSession {
map.serialize_field("state", state)?; config: &self.config,
map.serialize_field("history", &History(&self.state.history))?; state,
map.end() history: History(&self.state.history),
};
session.serialize(serializer)
} }
} }