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

KeyDeserialize trait is private making custom keys and generics over keys not possible. #691

Closed
0xekez opened this issue Mar 27, 2022 · 1 comment · Fixed by #692
Closed

Comments

@0xekez
Copy link
Contributor

0xekez commented Mar 27, 2022

The KeyDeserialize trait which is required for a type that would like to be a key for a Map is private in the cw_storage_plus::de module. This makes implementing custom map keys and functions that are generic over the map key type not possible.

For example, a generic function which lists all of the keys in a Map can't be implemented because the trait is private:

fn list_items<'a, K, V>(
    deps: Deps,
    map: Map<K, V>,
    start_at: Option<K>,
    limit: Option<u64>,
) -> StdResult<Vec<K>>
where
    K: cw_storage_plus::Bounder<'a>
        + cw_storage_plus::PrimaryKey<'a>
        + cw_storage_plus::de::KeyDeserialize,
    V: serde::de::DeserializeOwned + serde::Serialize,
{
    let items = map.keys(
        deps.storage,
        start_at.map(Bound::inclusive),
        None,
        cosmwasm_std::Order::Descending,
    );
    match limit {
        Some(limit) => Ok(items.take(limit as usize).collect::<Result<Vec<K>, _>>()?),
        None => Ok(items.collect::<Result<Vec<K>, _>>()?),
    }
}

Would be really cool to be able to do this. :)

@maurolacy
Copy link
Contributor

Sure.

In the mean time, you can try removing that bound and using keys_raw instead, to list the raw keys.

I guess you know this already, but the approach you have regarding limit can be gas consuming, as there is no default / max limit.

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.

2 participants