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

lang: Implementation of tuples and tuple structs in IDL #971

Open
cherryman opened this issue Nov 3, 2021 · 1 comment
Open

lang: Implementation of tuples and tuple structs in IDL #971

cherryman opened this issue Nov 3, 2021 · 1 comment
Labels
help wanted Extra attention is needed

Comments

@cherryman
Copy link
Contributor

Since JavaScript arrays are heterogenous, they're the canonical representation of tuples in JS. TypeScript also supports typing them, so for a homogenous array of T you might do T[], but tuples are instead of the form [T, U, V]. So a rust type Struct(U, V) maps to [U, V]. This can be represented in the IDL by adding another "variant" called "tuple", and the associated "fields" being a list of IDL types. This also creates an elegant translation of rust coproduct types, so we map

enum Enum {
    VariantOne(T, U, V),
    VaraintTwo { x: T },
 }

to

type Enum = { variantOne: [T, U, V] } | { variantTwo: { x: T } }

IDL Example

"type": {
  "kind": "tuple",
  "fields": [
    "u8",
    "publicKey",
    { "array": ["i64", 20] },
    { "defined": "CustomType" }
  ]
}

Related to #232.

@dovahcrow
Copy link

This will come very handy for upgrading the PDAs.
Instead of having

#[account]
pub struct PDA {
    field1: u64,
    _pad: [u8; 32],
}

Where _pad is for future upgrades. You can now have

#[account]
pub enum PDA {
    V1 {
        field1: u64,
    },
    Pad([u8; 32])
}

so that you can easily add a new variant to the enum for account upgrade, as long as the size of the whole account does not exceeds the size of Pad.

@acheroncrypto acheroncrypto mentioned this issue Feb 25, 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

3 participants