Something I don't quite understand about serde: What's the purpose for using a state machine for serialisation? Take the following snippet from the repo:
Actually you are asking about the Serializer not the Deserializer. Your second code differs from the first code (it won't even compile). The above code allows you to serialize one field, and then call some code of your own. An alternative would be to use closures.
Note that e.g. json uses this to not produce a comma after the last field. There are probably other ways to solve this, this is just the way that has been chosen (back in serde 0.2)
They do actually, I'll have to dig into the json serialiser to find out why... I would've thought each field would emit a comma, then the key and value unless the last non whitespace char was a brace or something to that effect.
That would be quite the magical hackery. Instead serde-json simply has a first field in the map/struct serialization code, that makes sure the first field doesn't get a comma prepended. It's exactly that structure that requires the state machine.
It seems like the way first is set in the serializer is enough to get around putting commas in the right places without the state machine.
I have some fairly complex manual serialisation that's never used the state machine approach and has always produced the correct output... Whether or not that makes it a good idea I'm not sure
I haven't thought so much about the deserialiser though, I haven't seen any alternative ways of deserialising values.