use hopper_runtime::error::ProgramError;
use hopper_runtime::{AccountView, Address};
use crate::account::{FixedLayout, Pod, VerifiedAccount};
use crate::check;
pub struct ProgramAccount<'a, T: Pod + FixedLayout> {
view: &'a AccountView,
_marker: core::marker::PhantomData<T>,
}
impl<'a, T: Pod + FixedLayout> ProgramAccount<'a, T> {
#[inline]
pub fn from_account(
account: &'a AccountView,
expected_owner: &Address,
) -> Result<Self, ProgramError> {
check::check_owner(account, expected_owner)?;
let data = account.try_borrow()?;
check::check_size(&data, core::mem::size_of::<T>())?;
Ok(Self {
view: account,
_marker: core::marker::PhantomData,
})
}
#[inline]
pub fn from_account_unchecked(account: &'a AccountView) -> Self {
Self {
view: account,
_marker: core::marker::PhantomData,
}
}
#[inline]
pub fn read(&self) -> Result<VerifiedAccount<'a, T>, ProgramError> {
let data = self.view.try_borrow()?;
VerifiedAccount::from_ref(data)
}
#[inline(always)]
pub fn address(&self) -> &Address {
self.view.address()
}
#[inline(always)]
pub unsafe fn owner(&self) -> &Address {
unsafe { self.view.owner() }
}
#[inline(always)]
pub fn to_account_view(&self) -> &'a AccountView {
self.view
}
}