Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Avoid generating too much code in the (storage) macros #1820

Open
tomusdrw opened this issue Feb 19, 2019 · 4 comments
Open

Avoid generating too much code in the (storage) macros #1820

tomusdrw opened this issue Feb 19, 2019 · 4 comments
Labels
I7-refactor Code needs refactoring. U3-nice_to_have Issue is worth doing eventually.
Milestone

Comments

@tomusdrw
Copy link
Contributor

tomusdrw commented Feb 19, 2019

Debugging code generated inside macros is really hard. We should try to implement generic structs outside of the macro code and then just implement a tiny bit of glue code (for instance trait implementation of the part that is instance-specific) inside the macro.

So instead of (

impl<#traitinstance: #traittype> #scrate::storage::generator::StorageMap<#kty, #typ> for #name<#traitinstance> {
):

pub struct #name();
impl StorageMap for #name {
 ...(all the methods implementations)
}

we do something similar to:

struct #name_glue;
impl StorageItem for #name_glue {
  fn prefix() -> &'[u8];
}
pub type #name = StorageMap<#name_glue, #kty, #typ>;
@tomusdrw tomusdrw added I7-refactor Code needs refactoring. U3-nice_to_have Issue is worth doing eventually. labels Feb 19, 2019
@tomusdrw tomusdrw added this to the As-and-when milestone Feb 19, 2019
@gui1117
Copy link
Contributor

gui1117 commented Mar 23, 2020

ah I missed this issue.

I think current trait generator are following this pattern,
StorageMap, StorageDoubleMap IterableStorageMap etc... Only requires a few item to be implemented, which are for StorageMap:

	/// Hasher. Used for generating final key.
	type Hasher: StorageHasher;

	/// Module prefix. Used for generating final key.
	fn module_prefix() -> &'static [u8];

	/// Storage prefix. Used for generating final key.
	fn storage_prefix() -> &'static [u8];

	/// Convert an optional value retrieved from storage to the type queried.
	fn from_optional_value_to_query(v: Option<V>) -> Self::Query;

	/// Convert a query to an optional value into storage.
	fn from_query_to_optional_value(v: Self::Query) -> Option<V>;

So maybe we can improve other aspect of code generation, I think storage macros are quite fine now.

@gui1117
Copy link
Contributor

gui1117 commented Mar 23, 2020

though we don't do the second suggestion totally as we curently do:

struct #name;
impl StorageItem for #name {
  fn prefix() -> &'[u8];
}

and we don't make use of name_glue and type defintion

@tomusdrw
Copy link
Contributor Author

The idea with name_glue was to move the storage logic completely off the macro (so the only thing that stays there is just the prefix). The motivation was to make it easier to work on the logic (i.e. avoid getting in-macro errors).
Feel free to close the issue if you feel this is no longer relevant.

@shawntabrizi
Copy link
Member

I think this can be closed?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
I7-refactor Code needs refactoring. U3-nice_to_have Issue is worth doing eventually.
Projects
None yet
Development

No branches or pull requests

3 participants