#size #struct #derive #struct-fields #traits-structs

macro sized_data_derive

Sized data macro implementation for use in Anchor framework

2 releases

0.1.1 Jan 29, 2025
0.1.0 Jan 29, 2025

#42 in #traits-structs

Download history 226/week @ 2025-01-28 24/week @ 2025-02-04 16/week @ 2025-02-11

266 downloads per month
Used in sized-data

MIT license

6KB

sized-data-derive

Procedural macro implementation for sized-data crate, used with Solana's Anchor framework.

Implementation

Automatically derives the SizedData trait for structs by:

  1. Analyzing struct fields at compile time
  2. Generating size calculation code for each field
  3. Producing a total size implementation
#[derive(SizedData)]
pub struct UserAccount {
    pub authority: Pubkey,    // 32 bytes
    pub counter: u64,         // 8 bytes
}

// Generates:
impl SizedData for UserAccount {
    fn size() -> usize {
        <Pubkey as SizedData>::size() + 
        <u64 as SizedData>::size()
    }
}

Supported Field Types

  • Named fields (struct Example { field: Type })
  • Unnamed fields (struct Example(Type))
  • Unit structs (struct Example;)

Requirements

  • Rust 1.83.0+
  • quote = "1.0"
  • syn = { version = "1.0", features = ["full"] }

Usage

This crate is typically used as a dependency of sized-data. Direct usage:

[dependencies]
sized_data_derive = "0.1.1"
use sized_data_derive::SizedData;

License

MIT License

Dependencies

~1.5MB
~38K SLoC