how can i use the complex structure(like Mapping<String, Vec<Map<String, String>>>)
in ink!
#![cfg_attr(not(feature = "std"), no_std)]
use ink_lang as ink;
#[ink::contract]
mod mycontract {
use ink_prelude::string::String;
use ink_prelude::vec::Vec;
use ink_storage::{traits::SpreadAllocate, Mapping};
/// Defines the storage of your contract.
/// Add new fields to the below struct in order
/// to add new static storage fields to your contract.
#[ink(storage)]
#[derive(SpreadAllocate)]
pub struct Mycontract {
/// 存储 key 对应的哈希值
hash_value_map: Mapping<String, Vec<Mapping<String, String>>>,
}
impl Mycontract {
/// 合约构造器,用于合约的初始化
#[ink(constructor)]
pub fn default() -> Self {
ink_lang::utils::initialize_contract(|_: &mut Self| {})
}
#[ink(message)]
pub fn set_data(&mut self, key_value:String, data:i32) {
match self.hash_value_map.get(&key_value) {
None => {
}
Some(test) => {
}
}
// match self.hash_value_map.get(&key_value) {
// None => {
// let mut vec:Vec<i32> = Vec::new();
// vec.push(data);
// self.hash_value_map.insert(key_value, &vec);
// }
// Some(mut vec) => {
// vec.push(data);
// self.hash_value_map.insert(key_value, &vec);
// }
// }
}
#[ink(message)]
pub fn get_data(&self, hash_key: String) -> Result<String,String> {
// match self.hash_value_map.get(&hash_key) {
// None => {
// return Err(String::from("DATA_NOT_FOUND"))
// }
// Some(vec) => {
// return Ok(vec)
// }
// };
return Ok(String::from("Test"))
}
}
/// Unit tests in Rust are normally defined within such a `#[cfg(test)]`
/// module and test functions are marked with a `#[test]` attribute.
/// The below code is technically just normal Rust code.
#[cfg(test)]
mod tests {
/// Imports all the definitions from the outer scope so we can use them here.
use super::*;
/// Imports `ink_lang` so we can use `#[ink::test]`.
use ink_lang as ink;
/// We test if the default constructor does its job.
#[ink::test]
fn default_works() {
let _mycontract = Mycontract::default();
}
}
}
i try this, but there are some errors
error[E0599]: the method `get` exists for struct `ink_storage::Mapping<std::string::String, std::vec::Vec<ink_storage::Mapping<std::string::String, std::string::String>>>`, but its trait bounds were not satisfied
--> lib.rs:30:39
|
30 | match self.hash_value_map.get(&key_value) {
| ^^^ method cannot be called on `ink_storage::Mapping<std::string::String, std::vec::Vec<ink_storage::Mapping<std::string::String, std::string::String>>>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`std::vec::Vec<ink_storage::Mapping<std::string::String, std::string::String>>: ink_storage::traits::PackedLayout`
For more information about this error, try `rustc --explain E0599`.
error: could not compile `mycontract` due to previous error
Error: Compilation failed with toolchain `nightly-2022-06-30-x86_64-unknown-linux-gnu`
Caused by:
command failed: "cargo" "check" "--target-dir" "/root/mycontract/target/dylint/target/nightly-2022-06-30-x86_64-unknown-linux-gnu" "--manifest-path" "/root/mycontract/Cargo.toml"
ERROR: `"/root/.rustup/toolchains/nightly-2022-12-24-x86_64-unknown-linux-gnu/bin/cargo" "dylint" "--lib" "ink_linting" "--manifest-path=/root/mycontract/Cargo.toml"` failed with exit code: Some(1)