Rust 如何在Near-contract中实现多维哈希

Rust 如何在Near-contract中实现多维哈希,rust,traits,nearprotocol,Rust,Traits,Nearprotocol,在一个用Rust编写的近乎智能的契约中,我试图存储一组由两个键“vtype”和“vsubtype”索引的“seedID”散列。我使用的是Near的键索引集合UnorderedMap和UnorderedSet,如下所示: // a group of seed IDs pub type SeedIdSet = UnorderedSet<SeedId>; // a map from vsubtype to seedIdSet pub type SeedSubIndex =

在一个用Rust编写的近乎智能的契约中,我试图存储一组由两个键“vtype”和“vsubtype”索引的“seedID”散列。我使用的是Near的键索引集合UnorderedMap和UnorderedSet,如下所示:

    // a group of seed IDs
pub type SeedIdSet = UnorderedSet<SeedId>;
    // a map from vsubtype to seedIdSet
pub type SeedSubIndex = UnorderedMap< VSubType, SeedIdSet >;
    // an array of those, indexed by vtype 
pub type SeedIndex = [SeedSubIndex; 3];
//一组种子ID
发布类型seedSet=无序集;
//从VSUBYPE到seedIdSet的映射
发布类型SeedSubIndex=UnorderedMap;
//由vtype索引的一个数组
发布类型SeedIndex=[SeedSubIndex;3];
SeedIndex类型是我试图存储在链上的类型。但是当我试图编译这个时,我得到了关于未实现特征的错误:

error[E0277]: the trait bound `UnorderedMap<u8, UnorderedSet<u64>>: Default` is not satisfied
   --> src/lib.rs:496:10
    |
496 | #[derive(BorshDeserialize, BorshSerialize)]
    |          ^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `UnorderedMap<u8, UnorderedSet<u64>>`
    |
    = note: required because of the requirements on the impl of `BorshDeserialize` for `[UnorderedMap<u8, UnorderedSet<u64>>; 3]`
    = help: see issue #48214
    = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `UnorderedMap<u8, UnorderedSet<u64>>: Copy` is not satisfied
   --> src/lib.rs:496:10
    |
496 | #[derive(BorshDeserialize, BorshSerialize)]
    |          ^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `UnorderedMap<u8, UnorderedSet<u64>>`
    |
    = note: required because of the requirements on the impl of `BorshDeserialize` for `[UnorderedMap<u8, UnorderedSet<u64>>; 3]`
    = help: see issue #48214
    = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]:不满足特性绑定'UnorderedMap:Default'
-->src/lib.rs:496:10
|
496 |#[派生(BorshDeserialize,BorshSerialize)]
|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^未为“无序映射”实现特性“默认”`
|
=注:由于“[UnorderedMap;3]`
=帮助:见第48214期
=注意:此错误源于派生宏(在夜间构建中,使用-Z宏反向跟踪运行以获取更多信息)
错误[E0277]:不满足特性绑定'UnorderedMap:Copy'
-->src/lib.rs:496:10
|
496 |#[派生(BorshDeserialize,BorshSerialize)]
|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^未为“无序映射”实现特征“复制”`
|
=注:由于“[UnorderedMap;3]`
=帮助:见第48214期
=注意:此错误源于派生宏(在夜间构建中,使用-Z宏反向跟踪运行以获取更多信息)
我不知道如何调试这个。这是因为无序映射实现中缺少某些内容吗?难道不允许它们筑巢吗?我的整个方法有什么问题吗?非常感谢您的建议。

有两种选择:

  • 您不需要迭代器。您可以使用组合键,例如:
  • struct键{
    键a:字符串,
    键b:字符串,
    }
    kv:查找图;
    
  • 你需要迭代器。然后您需要自己初始化内部映射:
  • pub结构合同{
    酒吧外部地图:无序地图,
    }
    发布结构内部映射{
    酒吧内部地图:无序地图,
    }
    impl内部映射{
    pub fn new(外键:OuterKey)->Self{
    让mut-internal_-map_-prefix=b“i”。到_-vec();
    //散列外部键以创建此内部映射的唯一前缀。
    extend(env::sha256(outer_key));
    自我{
    内部映射:无序映射::新建(内部映射前缀)
    }
    }   
    }
    
    您可以在旧的可替换令牌实现中找到这方面的示例:

    谢谢!我确实需要迭代器(至少在内部映射上)。。。我想我明白了。我想在实例化无序映射时需要一个唯一的前缀,这就是为什么它不能具有这些特性?