我想Struct在Parity Substrate自定义运行时内部使用创建数据类型。数据类型是通用的,因此我可以在不同的类型上使用它。
我正在尝试以下操作,但尚未编译。编译器抱怨找不到的子类型T。
pub struct CustomDataType<T> {
data: Vec<u8>,
balance: T::Balance,
owner: T::AccountId,
}
Run Code Online (Sandbox Code Playgroud)
我应该能够编译通用结构。
不幸的是,Sven Marnach给出的答案不适用于奇偶校验底物。在该结构的顶部使用了其他派生宏,这些宏在沿“直观”路径前进时会引起问题。
在这种情况下,您应该将所需的特征直接传递到自定义类型中,并为该结构的上下文创建新的泛型。
像这样:
use srml_support::{StorageMap, dispatch::Result};
pub trait Trait: balances::Trait {}
#[derive(Encode, Decode, Default)]
pub struct CustomDataType <Balance, Account> {
data: Vec<u8>,
balance: Balance,
owner: Account,
}
decl_module! {
// ... removed for brevity
}
decl_storage! {
trait Store for Module<T: Trait> as RuntimeExampleStorage {
Value get(value): CustomDataType<T::Balance, T::AccountId>;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
207 次 |
| 最近记录: |