lib.rs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. extern crate proc_macro;
  2. use quote::quote;
  3. use syn::parse_macro_input;
  4. /// The `#[state]` attribute defines the program's state struct, i.e., the
  5. /// program's global account singleton giving the program the illusion of state.
  6. ///
  7. /// To allocate space into the account on initialization, pass in the account
  8. /// size into the macro, e.g., `#[state(SIZE)]`. Otherwise, the size of the
  9. /// account returned by the struct's `new` constructor will determine the
  10. /// account size. When determining a size, make sure to reserve enough space
  11. /// for the 8 byte account discriminator prepended to the account. That is,
  12. /// always use 8 extra bytes.
  13. ///
  14. /// # Zero Copy Deserialization
  15. ///
  16. /// Similar to the `#[account]` attribute one can enable zero copy
  17. /// deserialization by using the `zero_copy` argument:
  18. ///
  19. /// ```ignore
  20. /// #[state(zero_copy)]
  21. /// ```
  22. ///
  23. /// For more, see the [`account`](./attr.account.html) attribute.
  24. #[deprecated(
  25. since = "0.14.0",
  26. note = "#[state] will be removed in a future version. Use a PDA with static seeds instead"
  27. )]
  28. #[proc_macro_attribute]
  29. pub fn state(
  30. args: proc_macro::TokenStream,
  31. input: proc_macro::TokenStream,
  32. ) -> proc_macro::TokenStream {
  33. let item_struct = parse_macro_input!(input as syn::ItemStruct);
  34. let struct_ident = &item_struct.ident;
  35. let is_zero_copy = args.to_string() == "zero_copy";
  36. let size_override = {
  37. if args.is_empty() {
  38. // No size override given. The account size is whatever is given
  39. // as the initialized value. Use the default implementation.
  40. quote! {
  41. impl anchor_lang::__private::AccountSize for #struct_ident {
  42. fn size(&self) -> std::result::Result<u64, anchor_lang::solana_program::program_error::ProgramError> {
  43. Ok(8 + self
  44. .try_to_vec()
  45. .map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotSerialize)?
  46. .len() as u64)
  47. }
  48. }
  49. }
  50. } else if is_zero_copy {
  51. quote! {
  52. impl anchor_lang::__private::AccountSize for #struct_ident {
  53. fn size(&self) -> std::result::Result<u64, anchor_lang::solana_program::program_error::ProgramError> {
  54. let len = anchor_lang::__private::bytemuck::bytes_of(self).len() as u64;
  55. Ok(8 + len)
  56. }
  57. }
  58. }
  59. } else {
  60. let size = proc_macro2::TokenStream::from(args);
  61. // Size override given to the macro. Use it.
  62. quote! {
  63. impl anchor_lang::__private::AccountSize for #struct_ident {
  64. fn size(&self) -> std::result::Result<u64, anchor_lang::solana_program::program_error::ProgramError> {
  65. Ok(#size)
  66. }
  67. }
  68. }
  69. }
  70. };
  71. let attribute = match is_zero_copy {
  72. false => quote! {
  73. #[cfg_attr(feature = "anchor-deprecated-state", account)]
  74. #[cfg_attr(not(feature = "anchor-deprecated-state"), account("state"))]
  75. },
  76. true => quote! {
  77. #[cfg_attr(feature = "anchor-deprecated-state", account(zero_copy))]
  78. #[cfg_attr(not(feature = "anchor-deprecated-state"), account("state", zero_copy))]
  79. },
  80. };
  81. proc_macro::TokenStream::from(quote! {
  82. #attribute
  83. #item_struct
  84. #size_override
  85. })
  86. }