spherenet_program_whitelist_interface/state/
mod.rs1pub mod account;
2pub mod whitelist_entry;
3
4use {bytemuck::Pod, core::mem, pinocchio::program_error::ProgramError, shank::ShankType};
5
6pub trait SizeOf {
8 const SIZE_OF: usize;
10}
11
12impl<T: Pod> SizeOf for T {
14 const SIZE_OF: usize = mem::size_of::<T>();
15}
16
17pub trait Initializable {
19 fn is_initialized(&self) -> bool;
21}
22
23#[repr(u8)]
25#[derive(Clone, Copy, Debug, PartialEq, ShankType)]
26pub enum AccountState {
27 Uninitialized,
29 Initialized,
31}
32
33#[inline(always)]
35pub fn load<T: Pod + Initializable>(input: &[u8]) -> Result<&T, ProgramError> {
36 if input.len() != T::SIZE_OF {
37 return Err(ProgramError::InvalidAccountData);
38 }
39
40 let account = bytemuck::from_bytes::<T>(input);
41
42 if !account.is_initialized() {
43 return Err(ProgramError::UninitializedAccount);
44 }
45
46 Ok(account)
47}
48
49#[inline(always)]
51pub fn load_mut<T: Pod + Initializable>(
52 input: &mut [u8],
53) -> Result<&mut T, ProgramError> {
54 if input.len() != T::SIZE_OF {
55 return Err(ProgramError::InvalidAccountData);
56 }
57
58 let account = bytemuck::from_bytes_mut::<T>(input);
59
60 if !account.is_initialized() {
61 return Err(ProgramError::UninitializedAccount);
62 }
63
64 Ok(account)
65}
66
67#[inline(always)]
69pub fn load_unchecked<T: Pod>(input: &[u8]) -> Result<&T, ProgramError> {
70 if input.len() != T::SIZE_OF {
71 return Err(ProgramError::InvalidAccountData);
72 }
73
74 Ok(bytemuck::from_bytes::<T>(input))
75}
76
77#[inline(always)]
79pub fn load_mut_unchecked<T: Pod>(
80 input: &mut [u8],
81) -> Result<&mut T, ProgramError> {
82 if input.len() != T::SIZE_OF {
83 return Err(ProgramError::InvalidAccountData);
84 }
85
86 Ok(bytemuck::from_bytes_mut::<T>(input))
87}