This commit is contained in:
2026-06-08 22:41:45 -07:00
parent ea066706ee
commit 9ab2eb0668
+32 -51
View File
@@ -653,66 +653,46 @@ where
}
}
#[cfg_attr(
feature = "serde",
derive(serde_derive::Deserialize),
serde(bound = "
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")]
impl<'de, G: Game> serde::de::DeserializeSeed<'de> for Session<G>
impl<'de, G: Game<Score = i32>> serde::de::Deserialize<'de> for Session<G>
where
G: serde::Deserialize<'de> + Clone,
G::Instruction: serde::Deserialize<'de>,
G: serde::Deserialize<'de> + Clone + Eq + core::hash::Hash,
G::Stats: Default,
G::Instruction: serde::Deserialize<'de> + Eq + core::hash::Hash,
SessionConfig<G::Config>: serde::Deserialize<'de>,
Vec<G::Instruction>: serde::Deserialize<'de>,
{
type Value = SessionState<G>;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct SessionStateVisitor<G: Game> {
state: G,
stats: SessionStats<G::Stats>,
config: SessionConfig<G::Config>,
let serialized = SerializedSession::deserialize(deserializer)?;
let mut session = Session::new(serialized.state, serialized.config);
for instruction in serialized.history {
session.process_instruction(instruction);
}
impl<'de, G: Game> serde::de::Visitor<'de> for SessionStateVisitor<G>
where
G: serde::Deserialize<'de> + Clone,
G::Instruction: serde::Deserialize<'de>,
{
type Value = SessionState<G>;
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "State History")
}
fn visit_seq<A>(mut self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let mut state = self.state.clone();
let mut history = match seq.size_hint() {
Some(capacity) => Vec::with_capacity(capacity),
None => Vec::new(),
};
while let Some(instruction) = seq.next_element::<G::Instruction>()? {
history.push(StateSnapshot {
state: state.clone(),
instruction: instruction.clone(),
});
state.process_instruction(
&mut self.stats.inner,
&self.config.inner,
instruction,
);
}
Ok(SessionState { state, history })
}
}
let state = G::deserialize(deserializer)?;
deserializer.deserialize_seq(SessionStateVisitor {
state,
stats: self.stats,
config: self.config,
})
Ok(session)
}
}
#[cfg(feature = "serde")]
impl<G: Game> serde::Serialize for SessionState<G>
impl<G: Game> serde::Serialize for Session<G>
where
G: serde::Serialize,
G::Config: serde::Serialize,
G::Instruction: serde::Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
@@ -744,13 +724,14 @@ where
// 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.
let state = if let Some(snapshot) = self.history.first() {
let state = if let Some(snapshot) = self.state.history.first() {
snapshot.state()
} else {
&self.state
&self.state.state
};
map.serialize_entry("config", &self.config)?;
map.serialize_entry("state", state)?;
map.serialize_entry("history", &History(&self.history))?;
map.serialize_entry("history", &History(&self.state.history))?;
map.end()
}
}