light_compressed_account/instruction_data/
cpi_context.rs

1use light_zero_copy::ZeroCopyMut;
2
3use crate::instruction_data::{
4    zero_copy::ZCompressedCpiContext, zero_copy_set::CompressedCpiContextTrait,
5};
6
7#[repr(C)]
8#[cfg_attr(
9    all(feature = "std", feature = "anchor"),
10    derive(anchor_lang::AnchorDeserialize, anchor_lang::AnchorSerialize)
11)]
12#[cfg_attr(
13    not(feature = "anchor"),
14    derive(borsh::BorshDeserialize, borsh::BorshSerialize)
15)]
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, ZeroCopyMut)]
17pub struct CompressedCpiContext {
18    /// Is set by the program that is invoking the CPI to signal that is should
19    /// set the cpi context.
20    pub set_context: bool,
21    /// Is set to clear the cpi context since someone could have set it before
22    /// with unrelated data.
23    pub first_set_context: bool,
24    /// Index of cpi context account in remaining accounts.
25    pub cpi_context_account_index: u8,
26}
27
28impl CompressedCpiContext {
29    pub fn first() -> Self {
30        Self {
31            set_context: false,
32            first_set_context: true,
33            cpi_context_account_index: 0,
34        }
35    }
36
37    pub fn set() -> Self {
38        Self {
39            set_context: true,
40            first_set_context: false,
41            cpi_context_account_index: 0,
42        }
43    }
44}
45
46impl CompressedCpiContextTrait for ZCompressedCpiContext {
47    fn first_set_context(&self) -> u8 {
48        self.first_set_context() as u8
49    }
50
51    fn set_context(&self) -> u8 {
52        self.set_context() as u8
53    }
54}
55
56impl CompressedCpiContextTrait for CompressedCpiContext {
57    fn first_set_context(&self) -> u8 {
58        self.first_set_context as u8
59    }
60
61    fn set_context(&self) -> u8 {
62        self.set_context as u8
63    }
64}