Trait SizedData

Source
pub trait SizedData {
    // Required method
    fn size() -> usize;

    // Provided method
    fn size_padded() -> usize { ... }
}
Expand description

A trait for types that can calculate their size at compile time.

Used primarily with Anchor accounts to determine required space allocation. The trait provides two methods:

  • size(): Returns the raw size of the type
  • size_padded(): Returns the size including Anchor’s 8-byte discriminator

§Examples

use sized_data::SizedData;
use anchor_lang::prelude::*;

#[derive(SizedData)]
pub struct UserAccount {
    pub authority: Pubkey,  // 32 bytes
    pub counter: u64,       // 8 bytes
}

assert_eq!(UserAccount::size(), 40);       // Raw size
assert_eq!(UserAccount::size_padded(), 48); // Including discriminator

Required Methods§

Source

fn size() -> usize

Returns the raw size of the type in bytes

Provided Methods§

Source

fn size_padded() -> usize

Returns the size including Anchor’s 8-byte discriminator

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl SizedData for u8

Source§

impl SizedData for u64

Source§

impl SizedData for Pubkey

Source§

impl SizedData for [u8; 32]

Implementors§