aligned_sized/lib.rs
1//! **aligned-sized** is a library providing the `aligned_sized` macro, which:
2//!
3//! * Calculates a size of the given struct and provides a `LEN` constant with
4//! that value.
5//!
6//! Future plans:
7//!
8//! * Ensuring that the struct is aligned, adding padding fields when
9//! neccessary.
10//!
11//! # Motivation
12//!
13//! Calculating the size of a struct is often a necessity when developing
14//! project in Rust, in particular:
15//!
16//! * [Solana](https://solana.com/) programs, also when using
17//! [Anchor](https://www.anchor-lang.com/) framework.
18//! * [eBPF](https://ebpf.io/) programs written in [Aya](https://aya-rs.dev/).
19//!
20//! This library provides a macro which automatically calculates the size,
21//! also taking in account factors which make a straightforward use of
22//! `core::mem::size_of::<T>` for the whole struct impossible (discriminants,
23//! vectors etc.).
24
25use proc_macro::TokenStream;
26use syn::{parse_macro_input, ItemStruct};
27
28mod expand;
29
30#[proc_macro_attribute]
31pub fn aligned_sized(attr: TokenStream, input: TokenStream) -> TokenStream {
32 let args = parse_macro_input!(attr as expand::AlignedSizedArgs);
33 let strct = parse_macro_input!(input as ItemStruct);
34 expand::aligned_sized(args, strct)
35 .unwrap_or_else(|err| err.to_compile_error())
36 .into()
37}