Skip to content

Support non-self-describing serde formats #86

Description

@skeet70

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationfeatureNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions