问题描述
我们正在尝试在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,}
解决方法
您的主要问题是您在这里混合使用raster
和std
。底物仅在no-std
模式下取决于serde
,因为您可以从项目中的任何std
文件中真正地了解到。
从解决此问题开始:仅在处于Cargo.toml
模式时派生serde::*
。
std