decl_storage中的"pub"的作用是什么? [英] What is the purpose of `pub` in decl_storage?

查看:89
本文介绍了decl_storage中的"pub"的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基板中实现运行时模块时,应提供以下存储空间

When implementing a runtime module in substrate, given the following storage

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 => T::Hash;

    pub AllKittiesCount get(all_kitties_cnt): u64;
    Nonce: u64;

    // if you want to initialize value in storage, use genesis block
  }
}

AllKittiesCount前面的pub的目的是什么?因为是否存在pub,所以polkadot UI仍然可以查询它,就好像它是一个公共变量一样.

What is the purpose of pub in front of AllKittiesCount? Because whether there is pub or not, polkadot UI can still query it, as if it is a public variable.

推荐答案

要像任何Rust类型一样在此处进行扩展,需要明确说明不同类型的可见性. decl_storage宏为您的每个存储项目生成一个struct.例如:

To expand here a bit, just like any Rust type, you need to be explicit about the visibility of different types. The decl_storage macro generates a struct for each of your storage items. For example:

decl_storage! {
    trait Store for Module<T: Trait> as TemplateModule {
        Something get(something): u32;
    }
}

会导致(为清楚起见,删除了一些东西):

Would result in (some stuff removed for clarity):

struct Something<T: Trait>(...);

impl <T: Trait> ... for Something<T> {
    fn get<S: ... >(storage: &S) -> Self::Query {
        storage.get(...).unwrap_or_else(|| Default::default())
    }
    fn take<S: ...>(storage: &S) -> Self::Query {
        storage.take(...).unwrap_or_else(|| Default::default())
    }
    fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: ...>(f: F, storage: &S) -> R {
        let mut val = <Self as ...>::get(storage);
        let ret = f(&mut val);
        <Self as ...>::put(&val, storage);
        ret
    }
}

如果将存储项目设置为pub,则只需在struct Something中引入一个pub标记.这意味着,您现在可以从其他模块调用由结构公开的所有这些功能,例如gettakemutate.否则,您将需要创建自己的公用函数,该公用函数公开用于修改存储的API.

If you make the storage item pub you simply introduce a pub tag to the struct Something. This means, you can now call all these functions exposed by the struct like get, take, mutate from other modules. Otherwise, you would need to create your own public functions which exposes an API to modify the storage.

这篇关于decl_storage中的"pub"的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆