chunk_loader/
state.rs

1use anchor_lang::prelude::*;
2
3#[account(discriminator = [1])]
4#[derive(Debug, InitSpace)]
5pub struct ChunkHolder {
6    pub owner: Pubkey,
7    #[max_len(0)]
8    pub chunks: Vec<Chunk>,
9}
10
11impl ChunkHolder {
12    pub fn join_chunks(&mut self) -> Vec<u8> {
13        self.chunks.sort_unstable_by(|a, b| b.index.cmp(&a.index));
14        let mut res = Vec::with_capacity(self.chunks.iter().map(|x| x.data.len()).sum());
15        for chunk in self.chunks.iter().rev() {
16            res.extend(&chunk.data);
17        }
18        res
19    }
20}
21
22#[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug, InitSpace)]
23pub struct Chunk {
24    pub index: u8,
25    #[max_len(0)]
26    pub data: Vec<u8>,
27}
28
29impl Chunk {
30    pub fn self_space(&self) -> usize {
31        Self::INIT_SPACE + self.data.len()
32    }
33}