【问题标题】:Returning structure in Substrate RPCSubstrate RPC 中的返回结构
【发布时间】:2020-10-07 23:37:30
【问题描述】:

我们正在尝试在 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)

问题是我们使用来自pallet_timestampT::Moment 并且它不可序列化,所以我们停留在这一点:

    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, Decode, Clone, Eq, PartialEq, RuntimeDebug, Serialize, Deserialize)]
pub struct WhoAndWhen<T: Trait> {
    pub account: T::AccountId,
    pub block: T::BlockNumber,
    pub time: T::Moment,
}

【问题讨论】:

    标签: rust substrate


    【解决方案1】:

    您的主要问题是您在这里混合了stdno-std。 Substrate 仅在 std 模式下依赖于 serde,正如您可以在项目中的任何 Cargo.toml 文件中了解的那样。

    首先解决这个问题:只有在 std 模式下才能派生 serde::*

    #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
    #[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug)]
    pub struct Space<T: Trait> {
      // snip.. 
    } 
    

    【讨论】:

      猜你喜欢
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 2020-03-07
      相关资源
      最近更新 更多