I want to create a data type using Struct
inside a Parity Substrate custom runtime. The data type is intended to be generic so that I can use it over different types.
I am trying the following, but it's not compiling. The compiler complains about sub-types not found for T
.
pub struct CustomDataType<T> {
data: Vec<u8>,
balance: T::Balance,
owner: T::AccountId,
}
I should be able to compile a generic struct.
Unfortunately, the answer that Sven Marnach gives does not work in the context of Parity Substrate. There are additional derive macros which are used on top of the struct which cause issues when going down the "intuitive" path.
In this case, you should pass the traits needed directly into your custom type and create new generics for the context of the struct.
Something like this:
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>;
}
}
We just created a doc for this exact scenario which I hope helps.