serde_extract
Enables turning a value that has Serialize into a Deserializer
Effectively this enables extracting a struct that implements Deserialize from a struct that
implements Serialize.
Usage
TL;DR
assert_eq!;
More realistic example
Let's say we're in a scenario where we want to implement an SDK where results are paginated, we only need to send
the original query once, but we need to re-send page_size if it was provided in the original query.
Since the code that manages pagination has no knowledge of the underlying struct, and because adding a page_size
argument to our make_paginated_request function would be very un-ergonomic because (let's say) it would be very
rarely used and it's nicer to specify it in the same struct as the rest of the query parameters, this is a good
use-case for this crate.
// This will be our original query
// Let's say make_request is our generic function that makes a call to the server
make_paginated_request
.expect;
Limitations
- Sequences are not supported for now (although support could theoritically be added, algorithmic complexity of
generated code would be O(n²) where n is the number of elements in the sequence because we would need to re-drive
the
Serializerfor each element called for by theVisitorthroughMapAccess) - For the same reason, deserializing into
maps is currently unsupported. Specifically, currently we can only extract struct fields if the fields names are hinted bydeserialize_struct. (This enables driving theSerializeronly as many times as there are fields to extract. In practice if both sides are regular structs, the optimizer probably turns that into zero-cost extraction. In theory again, support for deserializing into maps could be added with O(n²) complexity where n is the number of input fields.)