You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As seen in #201 it might be confusing why this code doesn't work:
// Can't get this working at all#[serde_as(as = "BTreeMap<String, ADef>")]pub map:BTreeMap<String,A>,// Confusingly enough defining identity transform like this doesn't compile either#[serde_as(as = "BTreeMap<String, String>")]pub map2:BTreeMap<String,String>,
The reason is that a blanket implementation for DeserializeAs/SerializeAs is not possible as it would overlap with other implementations. It is possible to special case the impl SerializeAs<String> for String case. This has to be done for every type and would bless the standard library types compared to types from crates.
Another possibility might be to move this logic into the serde_as macro. If it detects two identical types it replaces the type in the as part with _/Same. In the map case the BTreeMap<..., ...> compares not equal, but the String subtype does and would be replaced. In the map2 case already the BTreeMap<..., ...> type is equal and it would convert it to as = "_".
This could technically change how the serialization is performed as it then uses Serialize instead of SerializeAs. So this behavior might require an opt-out which disables the type equality check.
Another option might be to limit this to leaf types, i.e., ones without any generic parameters, since I assume it might be more likely there.
A problem with this approach is how to generalize this to different transformations, for example the BTreeMap<K, V> to Vec<(K, V)> transformation. The first one has two generic arguments while the Vec only has one (the tuple). So the fear here is that it simplifies it for the trivial cases but as soon as somebody want to use other transformations the help would fail and that might cause even more confusion.
The text was updated successfully, but these errors were encountered:
As seen in #201 it might be confusing why this code doesn't work:
The reason is that a blanket implementation for
DeserializeAs
/SerializeAs
is not possible as it would overlap with other implementations. It is possible to special case theimpl SerializeAs<String> for String
case. This has to be done for every type and would bless the standard library types compared to types from crates.Another possibility might be to move this logic into the serde_as macro. If it detects two identical types it replaces the type in the
as
part with_
/Same
. In themap
case theBTreeMap<..., ...>
compares not equal, but theString
subtype does and would be replaced. In themap2
case already theBTreeMap<..., ...>
type is equal and it would convert it toas = "_"
.This could technically change how the serialization is performed as it then uses
Serialize
instead ofSerializeAs
. So this behavior might require an opt-out which disables the type equality check.Another option might be to limit this to leaf types, i.e., ones without any generic parameters, since I assume it might be more likely there.
A problem with this approach is how to generalize this to different transformations, for example the
BTreeMap<K, V>
toVec<(K, V)>
transformation. The first one has two generic arguments while the Vec only has one (the tuple). So the fear here is that it simplifies it for the trivial cases but as soon as somebody want to use other transformations the help would fail and that might cause even more confusion.The text was updated successfully, but these errors were encountered: