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 typesize_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§
Provided Methods§
Sourcefn size_padded() -> usize
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.