boxed.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //! Box<T> type to save stack space.
  2. //!
  3. //! Sometimes accounts are too large for the stack,
  4. //! leading to stack violations.
  5. //!
  6. //! Boxing the account can help.
  7. //!
  8. //! # Example
  9. //! ```ignore
  10. //! #[derive(Accounts)]
  11. //! pub struct Example {
  12. //! pub my_acc: Box<Account<'info, MyData>>
  13. //! }
  14. //! ```
  15. use crate::{Accounts, AccountsClose, AccountsExit, ToAccountInfos, ToAccountMetas};
  16. use solana_program::account_info::AccountInfo;
  17. use solana_program::entrypoint::ProgramResult;
  18. use solana_program::instruction::AccountMeta;
  19. use solana_program::program_error::ProgramError;
  20. use solana_program::pubkey::Pubkey;
  21. use std::collections::BTreeMap;
  22. use std::ops::Deref;
  23. impl<'info, T: Accounts<'info>> Accounts<'info> for Box<T> {
  24. fn try_accounts(
  25. program_id: &Pubkey,
  26. accounts: &mut &[AccountInfo<'info>],
  27. ix_data: &[u8],
  28. bumps: &mut BTreeMap<String, u8>,
  29. ) -> Result<Self, ProgramError> {
  30. T::try_accounts(program_id, accounts, ix_data, bumps).map(Box::new)
  31. }
  32. }
  33. impl<'info, T: AccountsExit<'info>> AccountsExit<'info> for Box<T> {
  34. fn exit(&self, program_id: &Pubkey) -> ProgramResult {
  35. T::exit(Deref::deref(self), program_id)
  36. }
  37. }
  38. impl<'info, T: ToAccountInfos<'info>> ToAccountInfos<'info> for Box<T> {
  39. fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
  40. T::to_account_infos(self)
  41. }
  42. }
  43. impl<T: ToAccountMetas> ToAccountMetas for Box<T> {
  44. fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
  45. T::to_account_metas(self, is_signer)
  46. }
  47. }
  48. impl<'info, T: AccountsClose<'info>> AccountsClose<'info> for Box<T> {
  49. fn close(&self, sol_destination: AccountInfo<'info>) -> ProgramResult {
  50. T::close(self, sol_destination)
  51. }
  52. }