rust - Substrate 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
frompallet_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, Decode, Clone, Eq, PartialEq, RuntimeDebug, Serialize, Deserialize)]
pub struct WhoAndWhen<T: Trait> {
pub account: T::AccountId,
pub block: T::BlockNumber,
pub time: T::Moment,
}
解决方案
std
你的主要问题是你在no-std
这里混音。Substrate 仅取决于serde
instd
模式,因为您可以在项目中的任何Cargo.toml
文件中了解相关信息。
首先解决这个问题:只有serde::*
在std
模式下才能派生。
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug)]
pub struct Space<T: Trait> {
// snip..
}
推荐阅读
- python - 如何在 for 循环中获取单元测试的覆盖率?
- slurm - 如何使用 squeue 命令知道 slurm 脚本上的位置
- amazon-web-services - ECS Execution Role 导致容器启动时日志驱动失败?
- java - 什么时候适合在 Java 中使用 continue ?
- arrays - 向 R2000 RFID 阅读器发送十六进制命令
- qt - Qt5.12.1 QT_CONFIG(future) 在将编译器从 MinGW 更改为 MSVC 时中断
- permissions - 每次缓存清除后必须修复 var 文件夹权限,Symfony 4
- pandas - 如何更改熊猫分类数据类型中的代码顺序?
- python - 解决“GET /blog HTTP/1.1”404 2065(Python Django 教程:全功能 Web 应用第 2 部分 - 应用程序和路由)
- javascript - 单击时未触发 onClick 事件 - 反应