2021-01-22 03:35:57 -08:00
|
|
|
extern crate proc_macro;
|
|
|
|
|
|
|
|
use quote::quote;
|
|
|
|
use syn::parse_macro_input;
|
|
|
|
|
2021-02-07 22:22:50 -08:00
|
|
|
/// The `#[state]` attribute defines the program's state struct, i.e., the
|
|
|
|
/// program's global account singleton giving the program the illusion of state.
|
2021-03-24 10:35:51 -07:00
|
|
|
///
|
|
|
|
/// To allocate space into the account on initialization, pass in the account
|
|
|
|
/// size into the macro, e.g., `#[state(SIZE)]`. Otherwise, the size of the
|
|
|
|
/// account returned by the struct's `new` constructor will determine the
|
|
|
|
/// account size. When determining a size, make sure to reserve enough space
|
|
|
|
/// for the 8 byte account discriminator prepended to the account. That is,
|
|
|
|
/// always use 8 extra bytes.
|
2021-04-18 09:54:18 -07:00
|
|
|
///
|
|
|
|
/// # Zero Copy Deserialization
|
|
|
|
///
|
|
|
|
/// Similar to the `#[account]` attribute one can enable zero copy
|
|
|
|
/// deserialization by using the `zero_copy` argument:
|
|
|
|
///
|
|
|
|
/// ```ignore
|
|
|
|
/// #[state(zero_copy)]
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// For more, see the [`account`](./attr.account.html) attribute.
|
2021-01-22 03:35:57 -08:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn state(
|
2021-03-24 10:35:51 -07:00
|
|
|
args: proc_macro::TokenStream,
|
2021-01-22 03:35:57 -08:00
|
|
|
input: proc_macro::TokenStream,
|
|
|
|
) -> proc_macro::TokenStream {
|
|
|
|
let item_struct = parse_macro_input!(input as syn::ItemStruct);
|
2021-03-24 10:35:51 -07:00
|
|
|
let struct_ident = &item_struct.ident;
|
2021-04-18 09:54:18 -07:00
|
|
|
let is_zero_copy = args.to_string() == "zero_copy";
|
2021-03-24 10:35:51 -07:00
|
|
|
|
|
|
|
let size_override = {
|
|
|
|
if args.is_empty() {
|
|
|
|
// No size override given. The account size is whatever is given
|
|
|
|
// as the initialized value. Use the default implementation.
|
|
|
|
quote! {
|
2021-04-18 09:54:18 -07:00
|
|
|
impl anchor_lang::__private::AccountSize for #struct_ident {
|
2021-03-24 20:19:29 -07:00
|
|
|
fn size(&self) -> std::result::Result<u64, anchor_lang::solana_program::program_error::ProgramError> {
|
2021-03-24 10:35:51 -07:00
|
|
|
Ok(8 + self
|
|
|
|
.try_to_vec()
|
2021-06-09 13:02:50 -07:00
|
|
|
.map_err(|_| anchor_lang::__private::ErrorCode::AccountDidNotSerialize)?
|
2021-03-24 10:35:51 -07:00
|
|
|
.len() as u64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-27 17:09:46 -07:00
|
|
|
} else if is_zero_copy {
|
|
|
|
quote! {
|
|
|
|
impl anchor_lang::__private::AccountSize for #struct_ident {
|
|
|
|
fn size(&self) -> std::result::Result<u64, anchor_lang::solana_program::program_error::ProgramError> {
|
|
|
|
let len = anchor_lang::__private::bytemuck::bytes_of(self).len() as u64;
|
|
|
|
Ok(8 + len)
|
2021-04-18 09:54:18 -07:00
|
|
|
}
|
|
|
|
}
|
2021-06-27 17:09:46 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let size = proc_macro2::TokenStream::from(args);
|
|
|
|
// Size override given to the macro. Use it.
|
|
|
|
quote! {
|
|
|
|
impl anchor_lang::__private::AccountSize for #struct_ident {
|
|
|
|
fn size(&self) -> std::result::Result<u64, anchor_lang::solana_program::program_error::ProgramError> {
|
|
|
|
Ok(#size)
|
2021-03-24 10:35:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-01-22 03:35:57 -08:00
|
|
|
|
2021-04-18 09:54:18 -07:00
|
|
|
let attribute = match is_zero_copy {
|
2021-05-25 14:50:12 -07:00
|
|
|
false => quote! {#[account("state")]},
|
|
|
|
true => quote! {#[account("state", zero_copy)]},
|
2021-04-18 09:54:18 -07:00
|
|
|
};
|
|
|
|
|
2021-01-22 03:35:57 -08:00
|
|
|
proc_macro::TokenStream::from(quote! {
|
2021-04-18 09:54:18 -07:00
|
|
|
#attribute
|
2021-01-22 03:35:57 -08:00
|
|
|
#item_struct
|
2021-03-24 10:35:51 -07:00
|
|
|
|
|
|
|
#size_override
|
2021-01-22 03:35:57 -08:00
|
|
|
})
|
|
|
|
}
|