How can I get a hashmap as list of bytes and construct a hashmap again from that?

Currently I decided to go with capnp for serialization and deserialization but apparently it does not have support for maps and my struct contains 2 fields with maps.
Is there a way for me to convert those to list of bytes and back so I can just use Data type in capnp ?

You could convert it to one of these types and serialize that:

struct Map1<K, V> {
    keys: Vec<K>,
    values: Vec<V>,
struct Map2<K, V> {
    contents: Vec<(K, V)>,

first one works but the order of the keys and values might me changed I believe which will make it unusable.
In second case I dont think capnp supports tuples but I can I guess use

struct KV<K,V>{

struct Map<K,V>{

What is wrong with changing the order? Sure, the same hash map might be representable in several different ways, but so what?

if order changes different key will point to a different value and that will destroy the purpose of the map

but what exactly I am asking is if there is a more direct approach like get all the bytes starting from the first byte of the map to the last

I imagine you would perform the conversion when you need to serialize it, so you can simply ensure that they are inserted in the correct order. I don't think the hash map provides a more direct approach.

1 Like

isnt transmute designed for something like this ? I never understood the usage of transmute

No, transmute will not help you here. Just copying the bytes from one computer to another will not give you the same hash map.


Thanks So I will go with a KV struct I think

Alright, good luck with your serialization.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.