solana_program_pack/
lib.rs

1//! The [`Pack`] serialization trait
2//! This is a specific serialization API that is used by many older programs in
3//! the [Solana Program Library][spl] to manage account state. It is not generally
4//! recommended for new code since it does not define a language-independent
5//! serialization format.
6//!
7//! [spl]: https://github.com/solana-labs/solana-program-library
8
9use solana_program_error::ProgramError;
10
11/// Check if a program account state is initialized
12pub trait IsInitialized {
13    /// Is initialized
14    fn is_initialized(&self) -> bool;
15}
16
17/// Implementors must have a known size
18pub trait Sealed: Sized {}
19
20/// Safely and efficiently (de)serialize account state
21pub trait Pack: Sealed {
22    /// The length, in bytes, of the packed representation
23    const LEN: usize;
24    #[doc(hidden)]
25    fn pack_into_slice(&self, dst: &mut [u8]);
26    #[doc(hidden)]
27    fn unpack_from_slice(src: &[u8]) -> Result<Self, ProgramError>;
28
29    /// Get the packed length
30    fn get_packed_len() -> usize {
31        Self::LEN
32    }
33
34    /// Unpack from slice and check if initialized
35    fn unpack(input: &[u8]) -> Result<Self, ProgramError>
36    where
37        Self: IsInitialized,
38    {
39        let value = Self::unpack_unchecked(input)?;
40        if value.is_initialized() {
41            Ok(value)
42        } else {
43            Err(ProgramError::UninitializedAccount)
44        }
45    }
46
47    /// Unpack from slice without checking if initialized
48    fn unpack_unchecked(input: &[u8]) -> Result<Self, ProgramError> {
49        if input.len() != Self::LEN {
50            return Err(ProgramError::InvalidAccountData);
51        }
52        Self::unpack_from_slice(input)
53    }
54
55    /// Pack into slice
56    fn pack(src: Self, dst: &mut [u8]) -> Result<(), ProgramError> {
57        if dst.len() != Self::LEN {
58            return Err(ProgramError::InvalidAccountData);
59        }
60        src.pack_into_slice(dst);
61        Ok(())
62    }
63}