有个编译错误,把pub改成pub(crate)后就解决了,但是不知道为什么。

写了个闭包作为参数的函数:

pub fn internal_creator_use_twitter_pool<F>(&mut self, pool_id: &PoolId, mut f: F) 
    where F: FnMut(&mut TwitterPool);

结果编译就报:

error[E0277]: the trait bound `F: near_sdk::serde::Serialize` is not satisfied
   --> src/twitter_giveaway.rs:148:1
    |
148 | #[near_bindgen]
    | ^^^^^^^^^^^^^^^ the trait `near_sdk::serde::Serialize` is not implemented for `F`
    | 
   ::: /Users/xxx/.cargo/registry/src/github.com-1ecc6299db9ec823/serde_json-1.0.68/src/value/mod.rs:940:8
    |
940 |     T: Serialize,
    |        --------- required by this bound in `to_value`
    |
    = note: required because of the requirements on the impl of `near_sdk::serde::Serialize` for `&F`
    = note: this error originates in the macro `json_internal` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider restricting type parameter `F`
    |
160 |     pub fn internal_creator_use_twitter_pool<F: near_sdk::serde::Serialize>(&mut self, pool_id: &PoolId,mut f: F)
    |                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

改成pub(crate)后,编译通过,但是不懂为什么。

阅读 1.4k
1 个回答

可见度不同

  • pub(crate) 在当前crate中可见, 外部 crate 不可见
  • pub 在外部 crate 可见

pubpub(crate) 可见度高, 可能有冲突

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏