Is there a way for ink! smart contract developers to use custom structs with HashMap fields inside storage struct?
What I want to do
pub struct SampleStruct {
sample_vec: ink_prelude::vec::Vec<AccountId>,
sample_hash_map: ink_prelude::collections::HashMap<AccountId, ()>,
}
#[ink(storage)]
pub struct SampleStorage {
sample_struct: SampleStruct,
}
To do so, SampleStruct needs to implement scale::Encode, scale::Decode, scale_info::TypeInfo etc with derive macro like below.
#[derive(scale::Encode, scale::Decode)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo)
)]
pub struct SampleStruct {
sample_vec: ink_prelude::vec::Vec<AccountId>, // <- Vec has no issue
sample_hash_map: ink_prelude::collections::HashMap<AccountId, ()>,
}
However this derive implementation fails at compilation due to error the trait bound 'HashMap<ink_env::AccountId, ()>: WrapperTypeDecode\' is not satisfied the following other types implement trait 'WrapperTypeDecode': Arc<T> Box<T> Rc<T> required because of the requirements on the impl of 'parity_scale_codec::Decode' for 'HashMap<ink_env::AccountId, ()>'
.
ink_storage::Mapping<AccountId, ()>
is also not doable.
Dependencies
[dependencies]
ink_primitives = { version = "3.0", default-features = false }
ink_metadata = { version = "3.0", default-features = false, features = ["derive"], optional = true }
ink_env = { version = "3.0", default-features = false }
ink_storage = { version = "3.0", default-features = false }
ink_lang = { version = "3.0", default-features = false }
ink_prelude = { version = "3.0", default-features = false }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2", default-features = false, features = ["derive"], optional = true }