anchor_lang/accounts/
boxed.rs

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
16use crate::{Accounts, AccountsClose, AccountsExit, Result, ToAccountInfos, ToAccountMetas};
17use solana_program::account_info::AccountInfo;
18use solana_program::instruction::AccountMeta;
19use solana_program::pubkey::Pubkey;
20use std::collections::BTreeSet;
21use std::ops::Deref;
22
23impl<'info, B, T: Accounts<'info, B>> Accounts<'info, B> for Box<T> {
24    fn try_accounts(
25        program_id: &Pubkey,
26        accounts: &mut &'info [AccountInfo<'info>],
27        ix_data: &[u8],
28        bumps: &mut B,
29        reallocs: &mut BTreeSet<Pubkey>,
30    ) -> Result<Self> {
31        T::try_accounts(program_id, accounts, ix_data, bumps, reallocs).map(Box::new)
32    }
33}
34
35impl<'info, T: AccountsExit<'info>> AccountsExit<'info> for Box<T> {
36    fn exit(&self, program_id: &Pubkey) -> Result<()> {
37        T::exit(Deref::deref(self), program_id)
38    }
39}
40
41impl<'info, T: ToAccountInfos<'info>> ToAccountInfos<'info> for Box<T> {
42    fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
43        T::to_account_infos(self)
44    }
45}
46
47impl<T: ToAccountMetas> ToAccountMetas for Box<T> {
48    fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
49        T::to_account_metas(self, is_signer)
50    }
51}
52
53impl<'info, T: AccountsClose<'info>> AccountsClose<'info> for Box<T> {
54    fn close(&self, sol_destination: AccountInfo<'info>) -> Result<()> {
55        T::close(self, sol_destination)
56    }
57}