Derive Macro anchor_lang::InitSpace

source ·
#[derive(InitSpace)]
{
    // Attributes available to this derive:
    #[max_len]
}
Expand description

Implements a Space trait on the given struct or enum.

For types that have a variable size like String and Vec, it is necessary to indicate the size by the max_len attribute. For nested types, it is necessary to specify a size for each variable type (see example).

Example

#[account]
#[derive(InitSpace)]
pub struct ExampleAccount {
    pub data: u64,
    #[max_len(50)]
    pub string_one: String,
    #[max_len(10, 5)]
    pub nested: Vec<Vec<u8>>,
}

#[derive(Accounts)]
pub struct Initialize<'info> {
   #[account(mut)]
   pub payer: Signer<'info>,
   pub system_program: Program<'info, System>,
   #[account(init, payer = payer, space = 8 + ExampleAccount::INIT_SPACE)]
   pub data: Account<'info, ExampleAccount>,
}