serde impl #16
+32
-10
@@ -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
Quaternions
commented
Outdated
Review
- [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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user