Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ solana-program.workspace = true
spl-token = { workspace = true, optional = true }
spl-associated-token-account = { workspace = true, optional = true }
thiserror.workspace = true

53 changes: 42 additions & 11 deletions lib/src/loaders.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use bytemuck::Pod;
#[cfg(feature = "spl")]
use solana_program::program_pack::Pack;
use solana_program::{account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey};

use crate::{AccountDeserialize, AccountInfoValidation, AsAccount, Discriminator};
use crate::{
AccountDeserialize, AccountInfoValidation, AsAccount, Discriminator, FromHeader, FromHeaderMut,
};
#[cfg(feature = "spl")]
use crate::{AccountValidation, AsSplToken};

Expand Down Expand Up @@ -76,32 +77,62 @@ impl AccountInfoValidation for AccountInfo<'_> {
}
}

impl AsAccount for AccountInfo<'_> {
fn as_account<T>(&self, program_id: &Pubkey) -> Result<&T, ProgramError>
where
T: AccountDeserialize + Discriminator + Pod,
{
impl<'info> AsAccount for AccountInfo<'info> {
fn as_account<T: AccountDeserialize>(&self, program_id: &Pubkey) -> Result<&T, ProgramError> {
self.has_owner(program_id)?;
unsafe {
self.has_owner(program_id)?;
T::try_from_bytes(std::slice::from_raw_parts(
self.try_borrow_data()?.as_ptr(),
8 + std::mem::size_of::<T>(),
))
}
}

fn as_account_mut<T>(&self, program_id: &Pubkey) -> Result<&mut T, ProgramError>
fn as_account_with_header<'a, H, T>(&'a self, program_id: &Pubkey) -> Result<T, ProgramError>
where
T: AccountDeserialize + Discriminator + Pod,
'info: 'a,
H: AccountDeserialize + 'a,
T: FromHeader<'a, H>,
{
self.has_owner(program_id)?;
unsafe {
let borrowed = self.try_borrow_data()?;
let len = borrowed.len();
let data = std::slice::from_raw_parts(borrowed.as_ptr(), len);
let (header_bytes, remainder) = data.split_at(8 + std::mem::size_of::<H>());
let header = AccountDeserialize::try_from_bytes(header_bytes)?;
T::from_header_and_remainder(header, remainder)
}
}

fn as_account_mut<T: AccountDeserialize>(
&self,
program_id: &Pubkey,
) -> Result<&mut T, ProgramError> {
self.has_owner(program_id)?;
unsafe {
self.has_owner(program_id)?;
T::try_from_bytes_mut(std::slice::from_raw_parts_mut(
self.try_borrow_mut_data()?.as_mut_ptr(),
8 + std::mem::size_of::<T>(),
))
}
}

fn as_account_mut_with_header<'a, H, T>(&self, program_id: &Pubkey) -> Result<T, ProgramError>
where
H: AccountDeserialize + 'a,
T: FromHeaderMut<'a, H>,
{
self.has_owner(program_id)?;
unsafe {
let mut borrowed = self.try_borrow_mut_data()?;
let len = borrowed.len();
let data = std::slice::from_raw_parts_mut(borrowed.as_mut_ptr(), len);
let (header_bytes, remainder) = data.split_at_mut(8 + std::mem::size_of::<H>());
let header: &mut H = AccountDeserialize::try_from_bytes_mut(header_bytes)?;
T::from_header_and_remainder_mut(header, remainder)
}
}
}

#[cfg(feature = "spl")]
Expand Down
Loading