鉴于在底物运行时内生成的某个Hash
值,我如何修改或访问该哈希的各个字节?
答案 0 :(得分:3)
Hash
特征Output
具有AsRef
和AsMut
特征,使您可以作为字节片([u8]
)与哈希进行交互:>
pub trait Hash: 'static + MaybeSerializeDebug + Clone + Eq + PartialEq {
type Output: Member + MaybeSerializeDebug + AsRef<[u8]> + AsMut<[u8]>;
// ... removed for brevity
}
在哈希上使用as_ref()
或as_mut()
将返回一片字节,您可以照常使用它们:
例如:
// Iterate over a hash
let hash1 = <T as system::Trait>::Hashing::hash(1);
for hash_byte in hash1.as_ref().iter() {
// ... do something
}
或
// Add one to the first byte of a hash
let mut hash2 = <T as system::Trait>::Hashing::hash(2);
hash2.as_mut()[0] += 1;