sized-data
Rust trait and derive macro for calculating struct sizes in Solana programs using the Anchor framework.
Overview
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.
Usage
Add to your Cargo.toml
:
[]
= "0.1.1"
Example usage:
use SizedData;
use *;
// Initialize account with correct size
Features
- Automatic size calculation for structs
- Built-in implementations for common Solana types (Pubkey, u64, u8, [u8; 32])
- Padded size calculation (adds 8 bytes for Anchor's discriminator)
- Zero-cost abstraction - all calculations done at compile time
Size Implementations
Type | Size (bytes) |
---|---|
u8 | 1 |
u64 | 8 |
Pubkey | 32 |
[u8; 32] | 32 |
Requirements
- Rust 1.83.0 or later
- anchor-lang 0.30.1 or later
License
MIT License
For more details, visit GitHub Repository