serde impl #16

Merged
Quaternions merged 25 commits from serde into master 2026-06-09 17:15:25 +00:00
Showing only changes of commit ea066706ee - Show all commits
+32 -10
View File
1
@@ -719,16 +719,38 @@ where
where
S: serde::Serializer,
{
if let Some(state) = self.history.first() {
state.serialize(serializer)?;
struct History<'a, G: Game>(&'a [StateSnapshot<G>]);
impl<G: Game> serde::Serialize for History<'_, G>
where
G: serde::Serialize,
G::Instruction: serde::Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let &History(history) = self;
use serde::ser::SerializeSeq;
let mut seq = serializer.serialize_seq(Some(history.len()))?;
Quaternions marked this conversation as resolved Outdated
Outdated
Review
  • use a real struct
- [x] use a real struct
for snapshot in history {
seq.serialize_element(&snapshot.instruction)?;
}
seq.end()
}
}
use serde::ser::SerializeMap;
let mut map = serializer.serialize_map(Some(2))?;
// 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() {
snapshot.state()
} else {
self.state.serialize(serializer)?;
}
use serde::ser::SerializeSeq;
let mut seq = serializer.serialize_seq(Some(self.history.len()))?;
for snapshot in &self.history {
seq.serialize_element(&snapshot.instruction)?;
}
seq.end()
&self.state
};
map.serialize_entry("state", state)?;
map.serialize_entry("history", &History(&self.history))?;
map.end()
}
}