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

Create PrimaryKey, etc., derive macro for enums, maybe some other simple types. #28

Open
ewoolsey opened this issue Jan 25, 2023 · 1 comment
Labels
help wanted Extra attention is needed

Comments

@ewoolsey
Copy link
Contributor

Here is an example implementation for an enum. This would lend itself very well to a derive macro.

/// Unique addresses
#[derive(Copy, Display, FromPrimitive)]
#[cw_serde]
pub enum ConfigUnique {
    PriceOracle,
    Market,
    InterestModel,
    WarChest,
    Governance,
    AdminDoubleSig,
    AdminTripleSig,
    Owner,
}

impl<'a> PrimaryKey<'a> for ConfigUnique {
    type Prefix = ();
    type SubPrefix = ();
    type Suffix = Self;
    type SuperSuffix = Self;

    fn key(&self) -> Vec<Key> {
        vec![Key::Val8([*self as u8])]
    }
}

impl KeyDeserialize for ConfigUnique {
    type Output = ConfigUnique;

    #[inline(always)]
    fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
        FromPrimitive::from_u8(value[0]).ok_or_else(|| StdError::generic_err("Invalid ConfigUnique"))
    }
}
@ewoolsey
Copy link
Contributor Author

Another slightly less clean way to accomplish this is

impl<'a, T> PrimaryKey<'a> for T
    where
        T: FromPrimitive
{
    ...
}

@uint uint added the help wanted Extra attention is needed label Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants