-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: serde helpers for hashmaps and btreemaps with quantity key types #1579
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some comment nits,
this is also not quite right: we are only supposed to encode the keys as quantity, the value can be anything
crates/serde/src/quantity.rs
Outdated
@@ -164,6 +164,133 @@ pub mod u128_vec_vec_opt { | |||
} | |||
} | |||
|
|||
/// Serde functions for encoding a hashmap of primitive numbers using the Ethereum "quantity" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Serde functions for encoding a hashmap of primitive numbers using the Ethereum "quantity" | |
/// Serde functions for encoding a `HashMap` of primitive numbers using the Ethereum "quantity" |
crates/serde/src/quantity.rs
Outdated
use serde::{de::MapAccess, ser::SerializeMap, Deserializer, Serializer}; | ||
use std::collections::HashMap; | ||
|
||
/// Serializes a hashmap of primitive numbers as a "quantity" hex string. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Serializes a hashmap of primitive numbers as a "quantity" hex string. | |
/// Serializes a `HashMap` of primitive numbers as a "quantity" hex string. |
crates/serde/src/quantity.rs
Outdated
map_ser.end() | ||
} | ||
|
||
/// Deserializes a hashmap of primitive numbers from a "quantity" hex string. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Deserializes a hashmap of primitive numbers from a "quantity" hex string. | |
/// Deserializes a `HashMap` of primitive numbers from a "quantity" hex string. |
crates/serde/src/quantity.rs
Outdated
} | ||
} | ||
|
||
/// Serde functions for encoding a BTreeMap of primitive numbers using the Ethereum "quantity" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Serde functions for encoding a BTreeMap of primitive numbers using the Ethereum "quantity" | |
/// Serde functions for encoding a `BTreeMap` of primitive numbers using the Ethereum "quantity" |
crates/serde/src/quantity.rs
Outdated
where | ||
A: MapAccess<'de>, | ||
{ | ||
let mut values = HashMap::new(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can pre allocate with the map's size hint
crates/serde/src/quantity.rs
Outdated
use std::collections::HashMap; | ||
|
||
/// Serializes a hashmap of primitive numbers as a "quantity" hex string. | ||
pub fn serialize<K, V, S>(map: &HashMap<K, V>, serializer: S) -> Result<S::Ok, S::Error> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should also relax the hasher generic, see for ref https://github.com/serde-rs/serde/blob/418062165f9fe395461db9f61569c3142c584854/serde/src/ser/impls.rs#L495-L506
crates/serde/src/quantity.rs
Outdated
} | ||
|
||
/// Deserializes a hashmap of primitive numbers from a "quantity" hex string. | ||
pub fn deserialize<'de, K, V, D>(deserializer: D) -> Result<HashMap<K, V>, D::Error> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should also relax the hasher
Resolves #1502
Motivation
Existing
mod vec
implementationSolution
Created the same for hashmaps/btreemaps with quantity key types and tests.
PR Checklist