如何对底物哈希特性进行相等比较?
说我有以下代码,其中owned_vec
包含Hash
的向量:
use support::{decl_module, decl_storage, decl_event, dispatch::Result,
StorageValue, StorageMap, ensure, traits::Currency };
use system::ensure_signed;
// this is needed when you want to use Vec and Box
use rstd::prelude::*;
use runtime_primitives::traits::{ As, Hash };
use parity_codec::{ Encode, Decode };
// Our own Cat struct
#[derive(Encode, Decode, Default, Clone, PartialEq, Debug)]
pub struct Kitty<Hash, Balance> {
id: Hash,
name: Option<Vec<u8>>,
base_price: Balance, // when 0, it is not for sale
}
// This module's storage items.
decl_storage! {
trait Store for Module<T: Trait> as CatAuction {
Kitties get(kitties): map T::Hash => Kitty<T::Hash, T::Balance>;
KittyOwner get(owner_of): map T::Hash => Option<T::AccountId>;
OwnedKitties get(kitties_owned): map T::AccountId => Vec<T::Hash> = Vec::new();
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event<T>() = default;
pub fn transaction(origin, kitty_id: T::Hash) -> Result {
let sender = ensure_signed(origin)?;
let kitty_owner = Self::owner_of(kitty_id).ok_or("Kitty has no owner.")?;
let mut kitty = Self::kitties(kitty_id);
<OwnedKitties<T>>::mutate(kitty_owner, |owned_vec| {
let kitty_index = 0;
for (i, el) in owned_vec.iter().enumerate() {
// This is where the compilation error occurs!
if el != kitty.id { continue }
kitty_index = i;
}
owned_vec.remove(kitty_index);
});
}
}
}
它给了我编译器错误:
no implementation for `&<T as srml_system::Trait>::Hash == <T as srml_system::Trait>::Hash
help: the trait `core::cmp::PartialEq<<T as srml_system::Trait>::Hash>` is not implemented for `&<T as srml_system::Trait>::Hash`
help: consider adding a `where &<T as srml_system::Trait>::Hash: core::cmp::PartialEq<<T as srml_system::Trait>::Hash>` bound
谢谢!
p.s:知道该教程说,在运行时模块的实现中不鼓励循环通过向量。