typhoon_context/
array.rs

1use {
2    crate::{Context, HandlerContext},
3    core::mem::MaybeUninit,
4};
5
6/// An extractor to handle fixed array contexts.
7///
8/// This struct allows you to deserialize arrays of context types from program
9/// entrypoint. Each element in the array is deserialized using the same
10/// `HandlerContext::from_entrypoint` method, consuming accounts and instruction data
11/// in sequence.
12///
13/// # Type Parameters
14/// - `T`: The context type
15/// - `N`: The compile-time constant size of the array
16pub struct Array<T, const N: usize>(pub [T; N]);
17
18impl<'a, 'b, 'c, T, const N: usize> HandlerContext<'a, 'b, 'c> for Array<T, N>
19where
20    T: HandlerContext<'a, 'b, 'c> + Context,
21{
22    #[inline(always)]
23    fn from_entrypoint(
24        program_id: &'a pinocchio::pubkey::Pubkey,
25        accounts: &mut &'b [pinocchio::account_info::AccountInfo],
26        instruction_data: &mut &'c [u8],
27    ) -> Result<Self, typhoon_errors::Error> {
28        let mut result = [const { MaybeUninit::uninit() }; N];
29
30        for r in result.iter_mut() {
31            r.write(T::from_entrypoint(program_id, accounts, instruction_data)?);
32        }
33
34        // SAFETY: All elements have been initialized by the loop above
35        let array = unsafe { result.map(|item| item.assume_init()) };
36        Ok(Array(array))
37    }
38}