Skip to main content

typhoon_context/
array.rs

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