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

Make a mismatch of PK in an IndexedMap impossible (alt) #91

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

use proc_macro::TokenStream;
use syn::{
Ident,
Ident, Type, TypePath,
__private::{quote::quote, Span},
parse_macro_input, ItemStruct,
parse_macro_input, ItemStruct, Lifetime,
};

#[proc_macro_attribute]
Expand All @@ -29,12 +29,28 @@
})
.collect::<Vec<_>>();

let mut first_ix_type = input.fields.iter().next().unwrap().ty.clone();

Check warning on line 32 in macros/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

macros/src/lib.rs#L32

Added line #L32 was not covered by tests
// change all lifetime params to 'static
if let Type::Path(TypePath { ref mut path, .. }) = first_ix_type {
path.segments.iter_mut().for_each(|seg| {
if let syn::PathArguments::AngleBracketed(ref mut args) = seg.arguments {
for arg in args.args.iter_mut() {
if let syn::GenericArgument::Lifetime(ref mut lt) = arg {
*lt = Lifetime::new("'static", Span::call_site());

Check warning on line 39 in macros/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

macros/src/lib.rs#L34-L39

Added lines #L34 - L39 were not covered by tests
}
}
}
});
}

let expanded = quote! {
#input

impl cw_storage_plus::IndexList<#ty> for #struct_ty<'_> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn cw_storage_plus::Index<#ty>> + '_> {
let v: Vec<&dyn cw_storage_plus::Index<#ty>> = vec![#(#names),*];
type PK = <#first_ix_type as ::cw_storage_plus::Index<#ty>>::PK;

fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn cw_storage_plus::Index<#ty, PK = Self::PK>> + '_> {
let v: Vec<&dyn cw_storage_plus::Index<#ty, PK = Self::PK>> = vec![#(#names),*];
Box::new(v.into_iter())
}
}
Expand Down
53 changes: 38 additions & 15 deletions src/indexed_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use crate::prefix::{namespaced_prefix_range, Prefix};
use crate::{Bound, Path};

pub trait IndexList<T> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<T>> + '_>;
type PK;

fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<T, PK = Self::PK>> + '_>;
}

/// `IndexedMap` works like a `Map` but has a secondary index
Expand Down Expand Up @@ -335,8 +337,13 @@ mod test {

// Future Note: this can likely be macro-derived
impl<'a> IndexList<Data> for DataIndexes<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<Data>> + '_> {
let v: Vec<&dyn Index<Data>> = vec![&self.name, &self.age, &self.name_lastname];
type PK = String;

fn get_indexes(
&'_ self,
) -> Box<dyn Iterator<Item = &'_ dyn Index<Data, PK = Self::PK>> + '_> {
let v: Vec<&dyn Index<Data, PK = Self::PK>> =
vec![&self.name, &self.age, &self.name_lastname];
Box::new(v.into_iter())
}
}
Expand All @@ -349,8 +356,12 @@ mod test {

// Future Note: this can likely be macro-derived
impl<'a> IndexList<Data> for DataCompositeMultiIndex<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<Data>> + '_> {
let v: Vec<&dyn Index<Data>> = vec![&self.name_age];
type PK = String;

fn get_indexes(
&'_ self,
) -> Box<dyn Iterator<Item = &'_ dyn Index<Data, PK = Self::PK>> + '_> {
let v: Vec<&dyn Index<Data, PK = Self::PK>> = vec![&self.name_age];
Box::new(v.into_iter())
}
}
Expand Down Expand Up @@ -1453,13 +1464,17 @@ mod test {
mod bounds_unique_index {
use super::*;

struct Indexes<'a> {
secondary: UniqueIndex<'a, u64, u64, ()>,
struct Indexes<'a, 's> {
secondary: UniqueIndex<'a, u64, u64, &'s str>,
}

impl<'a> IndexList<u64> for Indexes<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<u64>> + '_> {
let v: Vec<&dyn Index<u64>> = vec![&self.secondary];
impl<'a, 's> IndexList<u64> for Indexes<'a, 's> {
type PK = &'s str;

fn get_indexes(
&'_ self,
) -> Box<dyn Iterator<Item = &'_ dyn Index<u64, PK = Self::PK>> + '_> {
let v: Vec<&dyn Index<u64, PK = Self::PK>> = vec![&self.secondary];
Box::new(v.into_iter())
}
}
Expand Down Expand Up @@ -1498,7 +1513,7 @@ mod test {
.collect::<Result<_, _>>()
.unwrap();

assert_eq!(items, vec![((), 3)]);
matches!(items.as_slice(), [(_, 3)]);
}
}

Expand All @@ -1511,8 +1526,12 @@ mod test {
}

impl<'a> IndexList<u64> for Indexes<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<u64>> + '_> {
let v: Vec<&dyn Index<u64>> = vec![&self.secondary];
type PK = &'a str;

fn get_indexes(
&'_ self,
) -> Box<dyn Iterator<Item = &'_ dyn Index<u64, PK = Self::PK>> + '_> {
let v: Vec<&dyn Index<u64, PK = Self::PK>> = vec![&self.secondary];
Box::new(v.into_iter())
}
}
Expand Down Expand Up @@ -1584,8 +1603,12 @@ mod test {
}

impl<'a> IndexList<Uint128> for Indexes<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<Uint128>> + '_> {
let v: Vec<&dyn Index<Uint128>> = vec![&self.spender];
type PK = (Addr, Addr);

fn get_indexes(
&'_ self,
) -> Box<dyn Iterator<Item = &'_ dyn Index<Uint128, PK = Self::PK>> + '_> {
let v: Vec<&dyn Index<Uint128, PK = Self::PK>> = vec![&self.spender];
Box::new(v.into_iter())
}
}
Expand Down
17 changes: 13 additions & 4 deletions src/indexed_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,13 @@ mod test {

// Future Note: this can likely be macro-derived
impl<'a> IndexList<Data> for DataIndexes<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<Data>> + '_> {
let v: Vec<&dyn Index<Data>> = vec![&self.name, &self.age, &self.name_lastname];
type PK = String;

fn get_indexes(
&'_ self,
) -> Box<dyn Iterator<Item = &'_ dyn Index<Data, PK = Self::PK>> + '_> {
let v: Vec<&dyn Index<Data, PK = Self::PK>> =
vec![&self.name, &self.age, &self.name_lastname];
Box::new(v.into_iter())
}
}
Expand All @@ -340,8 +345,12 @@ mod test {

// Future Note: this can likely be macro-derived
impl<'a> IndexList<Data> for DataCompositeMultiIndex<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<Data>> + '_> {
let v: Vec<&dyn Index<Data>> = vec![&self.name_age];
type PK = String;

fn get_indexes(
&'_ self,
) -> Box<dyn Iterator<Item = &'_ dyn Index<Data, PK = Self::PK>> + '_> {
let v: Vec<&dyn Index<Data, PK = Self::PK>> = vec![&self.name_age];
Box::new(v.into_iter())
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/indexes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub trait Index<T>
where
T: Serialize + DeserializeOwned + Clone,
{
type PK;

fn save(&self, store: &mut dyn Storage, pk: &[u8], data: &T) -> StdResult<()>;
fn remove(&self, store: &mut dyn Storage, pk: &[u8], old_data: &T) -> StdResult<()>;
}
Expand Down
2 changes: 2 additions & 0 deletions src/indexes/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ where
T: Serialize + DeserializeOwned + Clone,
IK: PrimaryKey<'a>,
{
type PK = PK;

fn save(&self, store: &mut dyn Storage, pk: &[u8], data: &T) -> StdResult<()> {
let idx = (self.index)(pk, data).joined_extra_key(pk);
self.idx_map.save(store, idx, &(pk.len() as u32))
Expand Down
2 changes: 2 additions & 0 deletions src/indexes/unique.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ where
T: Serialize + DeserializeOwned + Clone,
IK: PrimaryKey<'a>,
{
type PK = PK;

fn save(&self, store: &mut dyn Storage, pk: &[u8], data: &T) -> StdResult<()> {
let idx = (self.index)(data);
// error if this is already set
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ extern crate cw_storage_macro;
/// #[index_list(TestStruct)] // <- Add this line right here.
/// struct TestIndexes<'a> {
/// id: MultiIndex<'a, u32, TestStruct, u64>,
/// addr: UniqueIndex<'a, Addr, TestStruct, ()>,
/// addr: UniqueIndex<'a, Addr, TestStruct, u64>,
/// }
/// ```
///
Expand Down
4 changes: 2 additions & 2 deletions tests/index_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod test {
#[index_list(TestStruct)]
struct TestIndexes<'a> {
id: MultiIndex<'a, u32, TestStruct, u64>,
addr: UniqueIndex<'a, Addr, TestStruct, ()>,
addr: UniqueIndex<'a, Addr, TestStruct, u64>,
}

let _: IndexedMap<u64, TestStruct, TestIndexes> = IndexedMap::new(
Expand All @@ -41,7 +41,7 @@ mod test {
#[index_list(TestStruct)]
struct TestIndexes<'a> {
id: MultiIndex<'a, u32, TestStruct, u64>,
addr: UniqueIndex<'a, Addr, TestStruct, ()>,
addr: UniqueIndex<'a, Addr, TestStruct, u64>,
}

let mut storage = MockStorage::new();
Expand Down