微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

基板RPC中的返回结构

如何解决基板RPC中的返回结构

我们正在尝试在RPC中返回结构,但据我了解,该结构应该可序列化:

    error[E0277]: the trait bound `pallet_spaces::Space<T>: serde::de::Deserialize<'_>` is not satisfied
  --> pallets/spaces/rpc/src/lib.rs:15:1
   |
15 | #[rpc]
   | ^^^^^^ the trait `serde::de::Deserialize<'_>` is not implemented for `pallet_spaces::Space<T>`
   |
   = note: required because of the requirements on the impl of `for<'de> serde::de::Deserialize<'de>` for `std::vec::Vec<pallet_spaces::Space<T>>`
   = note: required because of the requirements on the impl of `serde::de::DeserializeOwned` for `std::vec::Vec<pallet_spaces::Space<T>>`
   = note: this error originates in an attribute macro (in Nightly builds,run with -Z macro-backtrace for more info)

问题是我们使用T::Moment中的pallet_timestamp并且它不可序列化,所以我们停留在这一点:

    error[E0277]: the trait bound `<T as pallet_timestamp::Trait>::Moment: _::_serde::Serialize` is not satisfied
  --> pallets/spaces/src/lib.rs:25:5
   |
25 |     pub created: WhoAndWhen<T>,|     ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `_::_serde::Serialize` is not implemented for `<T as pallet_timestamp::Trait>::Moment`
   |
   = note: required because of the requirements on the impl of `_::_serde::Serialize` for `pallet_utils::WhoAndWhen<T>`
   = note: required by `_::_serde::ser::SerializeStruct::serialize_field`

您建议如何轻松返回这样的结构?

#[derive(Encode,Decode,Clone,Eq,PartialEq,RuntimeDebug,Serialize,Deserialize)]
pub struct Space<T: Trait> {
    pub id: SpaceId,pub created: WhoAndWhen<T>,pub updated: Option<WhoAndWhen<T>>,pub owner: T::AccountId,// Can be updated by the owner:
    pub parent_id: Option<SpaceId>,pub handle: Option<Vec<u8>>,pub content: Content,pub hidden: bool,pub posts_count: u32,pub hidden_posts_count: u32,pub followers_count: u32,pub score: i32,/// Allows to override the default permissions for this space.
    pub permissions: Option<SpacePermissions>,}
#[derive(Encode,Deserialize)]
pub struct WhoAndWhen<T: Trait> {
    pub account: T::AccountId,pub block: T::BlockNumber,pub time: T::Moment,}

解决方法

您的主要问题是您在这里混合使用rasterstd。底物仅在no-std模式下取决于serde,因为您可以从项目中的任何std文件中真正地了解到。

从解决此问题开始:仅在处于Cargo.toml模式时派生serde::*

std

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。