[][src]Macro srml_support_procedural::decl_storage

decl_storage!() { /* proc-macro */ }

Declares strongly-typed wrappers around codec-compatible types in storage.

Example

decl_storage! {
	trait Store for Module<T: Trait> as Example {
		Foo get(foo) config(): u32=12;
		Bar: map u32 => u32;
		pub Zed build(|config| vec![(0, 0)]): linked_map u32 => u32;
	}
}

Declaration is set with the header (pub) trait Store for Module<T: Trait> as Example, with Store a (pub) trait generated associating each storage item to the Module and as Example setting the prefix used for storage items of this module. Example must be unique: another module with the same name and the same inner storage item name will conflict.

Basic storage consists of a name and a type; supported types are:

Supported hashers (ordered from least to best security):

Basic storage can be extended as such:

#vis #name get(#getter) config(#field_name) build(#closure): #type = #default;

Storage items are accessible in multiple ways:

GenesisConfig

An optional GenesisConfig struct for storage initialization can be defined, either when at least one storage field requires default initialization (both get and config or build), or specifically as in:

decl_storage! {
	trait Store for Module<T: Trait> as Example {

		// Your storage items
	}
	add_extra_genesis {
		config(genesis_field): GenesisFieldType;
		config(genesis_field2): GenesisFieldType;
		...
		build(|_: &mut StorageOverlay, _: &mut ChildrenStorageOverlay, _: &GenesisConfig<T>| {
			// Modification of storage
		})
	}
}

This struct can be exposed as ExampleConfig by the construct_runtime! macro like follows:

construct_runtime!(
	pub enum Runtume with ... {
        ...,
        Example: example::{Module, Storage, ..., Config<T>},
        ...,
}
);

Module with Instances

The decl_storage! macro supports building modules with instances with the following syntax (DefaultInstance type is optional):

trait Store for Module<T: Trait<I>, I: Instance=DefaultInstance> as Example {}

Accessing the structure no requires the instance as generic parameter:

Where clause

This macro supports a where clause which will be replicated to all generated types.

trait Store for Module<T: Trait> as Example where T::AccountId: std::fmt::Display {}

Limitations

Instancing and generic GenesisConfig

If your module supports instancing and you see an error like parameter I is never used for your decl_storage!, you are hitting a limitation of the current implementation. You probably try to use an associated type of a non-instantiable trait. To solve this, add the following to your macro call:

add_extra_genesis {
	config(phantom): std::marker::PhantomData<I>,
}
...

This adds a field to your `GenesisConfig` with the name `phantom` that you can initialize with
`Default::default()`.