Expand description
§init_space_derive
A procedural macro that automatically calculates the space required to store a struct in Solana programs (or Borsh-serialized data).
- Computes size for fixed types like
u8,u64,bool, etc. - Supports dynamic types like
StringandVec<u8>by requiring a#[max_len = N]attribute.
§Example
use init_space_derive::InitSpace;
use borsh::{BorshSerialize, BorshDeserialize};
#[derive(BorshSerialize, BorshDeserialize, InitSpace)]
pub struct Movie {
pub title: u64,
pub rating: u8,
#[max_len = 1000]
pub description: Vec<u8>,
}
let space_needed = Movie::space();
assert_eq!(space_needed, 13 + 1004); // 13 for fixed, 4+1000 for Vec