Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide proc macro package for automatic IndexList<T> implementation on any index struct #736

Closed
y-pakorn opened this issue Jun 14, 2022 · 0 comments · Fixed by #737
Closed

Comments

@y-pakorn
Copy link
Contributor

y-pakorn commented Jun 14, 2022

Currently, it would be kinda troublesome to use IndexedMap because we need to manually implement IndexList for every struct that we want to use as the index.

Rust's procedural macro feature enabled us to easily provide 1 line of code instead of manually typing the impl by ourselves for every struct we created.

Implementing this and reexport it in cw-storage-plus package (may be in an additional cfg features) will makes dev life much easier

Proposal: reduce this,

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Token {
  pub owner: Addr,
  pub ticker: String,
  pub identifier: u8, // <---- unique value
}

pub struct TokenIndexes<'a> {
  pub identifier: UniqueIndex<'a, U8Key, Token>,
}

// IndexList is just boilerplate code for fetching a struct's indexes
impl<'a> IndexList<Token> for TokenIndexes<'a> {
  fn get_indexes(&'_ self) -> Box<dyn Iterator<Item=&'_ dyn Index<Token>> + '_> {
    let v: Vec<&dyn Index<Token>> = vec![&self.identifier];
    Box::new(v.into_iter())
  }
}

to this

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Token {
  pub owner: Addr,
  pub ticker: String,
  pub identifier: u8, // <---- unique value
}

#[index_list(Token)]
pub struct TokenIndexes<'a> {
  pub identifier: UniqueIndex<'a, U8Key, Token>,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant