0

I want to create a HashMap storage item in ink!.

#[ink(storage)]
pub struct item {
    shipment: ink_storage::collections::HashMap<
        (AccountId, AccountId),
        ink_storage::collections::Vec<u128>,
    >,
}

And initialize it:

#[ink(constructor)]
pub fn new() -> Self {
    Self {
        shipment: ink_storage::collections::HashMap::new(),
    }
}

I encountered this error message

the trait PackedLayout is not implemented for `ink_storage::Vec

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • From their docs it seems this trait is implemented for [`Vec` from standard library](https://paritytech.github.io/ink/ink_storage/traits/trait.PackedLayout.html#impl-PackedLayout-for-Vec%3CT%3E) – Alexey S. Larionov Jul 12 '21 at 10:47

1 Answers1

0

ink_storage::Vec does not implement PackedLayout because its layout is not packed! According to the docs:

Despite the similarity to Rust’s Vec type this storage Vec has many differences in its internal data layout. While it stores its data in contiguous storage slots this does not mean that the data is actually densely stored in memory.

The data is organised in contiguous chunks, but each chunk may not be adjacent to the previous in memory.

On the other hand, std::vec::Vec does implement PackedLayout, so you should be able to use that instead.

Peter Hall
  • 53,120
  • 14
  • 139
  • 204