solana-primitives 0.2.6

Fundamental data structures and tools needed to construct and submit Solana transactions.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use crate::error::{Result, SolanaError};
use borsh::{BorshDeserialize, BorshSerialize};

/// Helper function to convert a compact array to bytes
pub fn compact_array_to_bytes<T: BorshSerialize>(items: &[T]) -> Result<Vec<u8>> {
    let mut bytes = Vec::new();
    items
        .serialize(&mut &mut bytes)
        .map_err(|e| SolanaError::SerializationError(e.to_string()))?;
    Ok(bytes)
}

/// Helper function to convert bytes to a compact array
pub fn bytes_to_compact_array<T: BorshDeserialize>(bytes: &[u8]) -> Result<Vec<T>> {
    let mut bytes_mut = bytes; // Borsh deserialize expects a mutable slice for some reason
    Vec::<T>::deserialize(&mut bytes_mut)
        .map_err(|e| SolanaError::SerializationError(e.to_string()))
}