Skip to content

How best to implement a custom Option type #595

Answered by jonasbb
markcatley asked this question in Q&A
Discussion options

You must be logged in to vote

Peeking is impossible with serde, because any deserialize_* method on Deserializer consumes the Deserializer. What serde does to simulate peeking is deserialize into a Value-like type, which can hold any data, and then deserialize from the Value, potentially multiple times. That strategy has some downsides though.

In your case you can deserialize a String or Option<String> and inspect that. You can turn the String into a Deserializer for example with StringDeserializer.
The StringDeserializer is relativly strict and only provides Strings, whereas the original Deserializer might be more lenient and offer integers or booleans (I don't recall what csv can do exactly). If you need that you mu…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jonasbb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants