account_compression/state/
queue.rs

1use std::mem;
2
3use aligned_sized::aligned_sized;
4use anchor_lang::prelude::*;
5use light_hash_set::{zero_copy::HashSetZeroCopy, HashSet};
6use light_merkle_tree_metadata::{
7    access::AccessMetadata, queue::QueueMetadata, rollover::RolloverMetadata, QueueType,
8};
9
10use crate::utils::check_signer_is_registered_or_authority::GroupAccess;
11
12#[account(zero_copy)]
13#[derive(AnchorDeserialize, Debug, PartialEq)]
14#[aligned_sized(anchor)]
15pub struct QueueAccount {
16    pub metadata: QueueMetadata,
17}
18
19impl QueueAccount {
20    pub fn init(
21        &mut self,
22        access_metadata: AccessMetadata,
23        rollover_meta_data: RolloverMetadata,
24        associated_merkle_tree: Pubkey,
25        queue_type: QueueType,
26    ) {
27        self.metadata.init(
28            access_metadata,
29            rollover_meta_data,
30            associated_merkle_tree.into(),
31            queue_type,
32        )
33    }
34}
35
36impl GroupAccess for QueueAccount {
37    fn get_owner(&self) -> Pubkey {
38        self.metadata.access_metadata.owner.into()
39    }
40
41    fn get_program_owner(&self) -> Pubkey {
42        self.metadata
43            .access_metadata
44            .program_owner
45            .to_bytes()
46            .into()
47    }
48}
49
50impl QueueAccount {
51    pub fn size(capacity: usize) -> Result<usize> {
52        Ok(8 + mem::size_of::<Self>() + HashSet::size_in_account(capacity))
53    }
54}
55
56/// Creates a copy of `HashSet` from the given account data.
57///
58/// # Safety
59///
60/// This operation is unsafe. It's the caller's responsibility to ensure that
61/// the provided account data have correct size and alignment.
62pub unsafe fn queue_from_bytes_copy(data: &mut [u8]) -> Result<HashSet> {
63    let data = &mut data[8 + mem::size_of::<QueueAccount>()..];
64    let queue = HashSet::from_bytes_copy(data).map_err(ProgramError::from)?;
65    Ok(queue)
66}
67
68/// Casts the given account data to an `HashSetZeroCopy` instance.
69///
70/// # Safety
71///
72/// This operation is unsafe. It's the caller's responsibility to ensure that
73/// the provided account data have correct size and alignment.
74pub unsafe fn queue_from_bytes_zero_copy_mut(data: &mut [u8]) -> Result<HashSetZeroCopy> {
75    let data = &mut data[8 + mem::size_of::<QueueAccount>()..];
76    let queue = HashSetZeroCopy::from_bytes_zero_copy_mut(data).map_err(ProgramError::from)?;
77    Ok(queue)
78}
79
80/// Casts the given account data to an `HashSetZeroCopy` instance.
81///
82/// # Safety
83///
84/// This operation is unsafe. It's the caller's responsibility to ensure that
85/// the provided account data have correct size and alignment.
86pub unsafe fn queue_from_bytes_zero_copy_init(
87    data: &mut [u8],
88    capacity: usize,
89    sequence_threshold: usize,
90) -> Result<HashSetZeroCopy> {
91    let data = &mut data[8 + mem::size_of::<QueueAccount>()..];
92    let queue = HashSetZeroCopy::from_bytes_zero_copy_init(data, capacity, sequence_threshold)
93        .map_err(ProgramError::from)?;
94    Ok(queue)
95}