typhoon_context/
args.rs

1use {
2    crate::HandlerContext,
3    bytemuck::{try_from_bytes, AnyBitPattern},
4    pinocchio::{account_info::AccountInfo, program_error::ProgramError},
5    std::ops::Deref,
6};
7
8#[derive(Debug)]
9pub struct Args<'a, T>(&'a T);
10
11impl<'a, T> Args<'a, T> {
12    pub fn new(arg: &'a T) -> Self {
13        Args(arg)
14    }
15}
16
17impl<T> Deref for Args<'_, T> {
18    type Target = T;
19
20    fn deref(&self) -> &Self::Target {
21        self.0
22    }
23}
24
25impl<'a, T> HandlerContext<'a> for Args<'a, T>
26where
27    T: AnyBitPattern,
28{
29    fn from_entrypoint(
30        _accounts: &mut &'a [AccountInfo],
31        instruction_data: &mut &'a [u8],
32    ) -> Result<Self, ProgramError> {
33        let arg: &T = try_from_bytes(&instruction_data[..std::mem::size_of::<T>()])
34            .map_err(|_| ProgramError::InvalidInstructionData)?;
35
36        let (_, remaining) = instruction_data.split_at(std::mem::size_of::<T>());
37
38        *instruction_data = remaining;
39
40        Ok(Args::new(arg))
41    }
42}