Skip to content
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

Add binprot shapes for std::collections::{BTreeMap, HashMap} #8

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// TODO: handle recursive types!
use crate::traits::ShapeContext;
use crate::BinProtShape;
use std::collections::BTreeMap;
use std::collections::{BTreeMap, HashMap};

// In the OCaml version, uuids are used as strings.
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -235,6 +235,32 @@ impl<T: BinProtShape> BinProtShape for Vec<T> {
}
}

fn iterable_binable1_shape(caller_identity: Uuid, bin_shape_el: Shape) -> Shape {
Shape::Base(
caller_identity,
vec![Shape::Base(Uuid::from("ac8a9ff4-4994-11e6-9a1b-9fb4e933bd9d"), vec![bin_shape_el])],
)
}

impl<K: BinProtShape, V: BinProtShape, S> BinProtShape for HashMap<K, V, S>
where
S: 'static,
{
fn binprot_shape_impl(c: &mut ShapeContext) -> Shape {
let caller_identity = Uuid::from("8fabab0a-4992-11e6-8cca-9ba2c4686d9e");
let bin_shape_el = Shape::Tuple(vec![K::binprot_shape_loop(c), V::binprot_shape_loop(c)]);
iterable_binable1_shape(caller_identity, bin_shape_el)
}
}

impl<K: BinProtShape, V: BinProtShape> BinProtShape for BTreeMap<K, V> {
fn binprot_shape_impl(c: &mut ShapeContext) -> Shape {
let caller_identity = Uuid::from("dfb300f8-4992-11e6-9c15-73a2ac6b815c");
let bin_shape_el = Shape::Tuple(vec![K::binprot_shape_loop(c), V::binprot_shape_loop(c)]);
iterable_binable1_shape(caller_identity, bin_shape_el)
}
}

impl<T: BinProtShape> BinProtShape for Option<T> {
fn binprot_shape_impl(c: &mut ShapeContext) -> Shape {
Shape::Base(Uuid::from("option"), vec![T::binprot_shape_loop(c)])
Expand Down Expand Up @@ -297,5 +323,13 @@ mod tests {
Shape::Application(Box::new(inner), vec![])
};
assert_eq!(digest_str(&shape_rec), "2e92d51efb901fcf492f243fc1c3601d");
let shape_i64_i64_hashtbl = {
std::collections::HashMap::<i64, i64>::binprot_shape_impl(&mut ShapeContext::default())
};
assert_eq!(digest_str(&shape_i64_i64_hashtbl), "1fd943a5d8026fbd3e6746c972ab2127");
let shape_i64_i64_btreemap = {
std::collections::BTreeMap::<i64, i64>::binprot_shape_impl(&mut ShapeContext::default())
};
assert_eq!(digest_str(&shape_i64_i64_btreemap), "ed73a010af8ffc32cab7411d6be2d676");
}
}
Loading