I'm a newbie in Rust, Substrate, and blockchain development. I'm developing an ink! smart contract that store a list of items. Since the storage version of Vec
is not available yet, I use a Mapping
to store the items instead. I've implemented contract methods for adding and getting items successfully. Here is my code:
#[ink(storage)]
#[derive(SpreadAllocate)]
pub struct MyContract {
map: Mapping<u32, MyStruct>,
}
impl MyContract {
#[ink(constructor)]
pub fn default() -> Self {
ink_lang::utils::initialize_contract(|_| {})
}
#[ink(message)]
pub fn add_item(&mut self, item: MyStruct) {
self.map.insert(item.id, &item);
}
#[ink(message)]
pub fn get_item(&self, id: u32) -> MyStruct {
self.map.get(&id).unwrap().clone()
}
}
Now I'm trying to create a method that returns the whole list to the caller. But I'm not able to find a way to do that. Maybe I should convert Mapping
to Vec
, but again I can't find a way to do it. Could you please help me? Thanks.