Crates.io | sized-data |
lib.rs | sized-data |
version | 0.1.1 |
created_at | 2025-01-29 20:55:28.497763+00 |
updated_at | 2025-01-29 21:07:21.962811+00 |
description | Sized data trait + macro implementation for use in Anchor framework. |
homepage | https://github.com/simke9445/sized-data |
repository | https://github.com/simke9445/sized-data |
max_upload_size | |
id | 1535412 |
size | 7,464 |
Rust trait and derive macro for calculating struct sizes in Solana programs using the Anchor framework.
sized-data
provides compile-time size calculations for Solana program data structures. It's particularly useful when working with account data in Anchor programs where you need to know exact sizes for account initialization.
Add to your Cargo.toml
:
[dependencies]
sized-data = "0.1.1"
Example usage:
use sized_data::SizedData;
use anchor_lang::prelude::*;
#[derive(SizedData)]
pub struct UserAccount {
pub authority: Pubkey,
pub counter: u64,
pub data: [u8; 32],
}
// Initialize account with correct size
#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(
init,
payer = user,
space = UserAccount::size_padded() // Uses padded size for Anchor
)]
pub user_account: Account<'info, UserAccount>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>,
}
Type | Size (bytes) |
---|---|
u8 | 1 |
u64 | 8 |
Pubkey | 32 |
[u8; 32] | 32 |
MIT License
For more details, visit GitHub Repository