-
Notifications
You must be signed in to change notification settings - Fork 34
Support non-self-describing serde formats #86
Copy link
Copy link
Open
Labels
documentationImprovements or additions to documentationImprovements or additions to documentationfeatureNew feature or requestNew feature or request
Description
It seems like non-self-describing formats (ie postcard, bincode) require a sized sequence when serializing. papaya::HashMap obviously can't provide a bounded size_hint (which would make serde's collect_map work as is) due to concurrent access. I've worked around this via
fn serialize_papaya_map<S, K, V>(map: &HashMap<K, V>, serializer: S) -> StdResult<S::Ok, S::Error>
where
S: Serializer,
K: Serialize + core::hash::Hash + Eq,
V: Serialize,
{
serializer.collect_seq(map.pin().iter().collect::<Vec<_>>())
}
fn deserialize_papaya_map<'de, D, K, V>(deserializer: D) -> StdResult<HashMap<K, V>, D::Error>
where
D: Deserializer<'de>,
K: Deserialize<'de> + core::hash::Hash + Eq,
V: Deserialize<'de>,
{
let entries: Vec<(K, V)> = Vec::deserialize(deserializer)?;
let map = HashMap::new();
for (k, v) in entries {
map.pin().insert(k, v);
}
Ok(map)
}If you don't want to bring that into the lib, it may be helpful to others to document the incompatibility due to concurrency here and suggest a similar workaround to what I've done. I can PR that if it's the way you'd like to approach it.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
documentationImprovements or additions to documentationImprovements or additions to documentationfeatureNew feature or requestNew feature or request