use solana_program::pubkey::Pubkey;
use crate::account_info::AccountInfo;
#[repr(C)]
#[derive(Debug, Clone)]
pub struct CAccountMeta {
pub pubkey: *const Pubkey,
pub is_writable: bool,
pub is_signer: bool,
}
impl From<&AccountInfo> for CAccountMeta {
fn from(account: &AccountInfo) -> Self {
CAccountMeta {
pubkey: offset(account.raw, 8),
is_writable: account.is_writable(),
is_signer: account.is_signer(),
}
}
}
#[repr(C)]
#[derive(Clone)]
pub struct CAccountInfo {
pub key: *const Pubkey,
pub lamports: *const u64,
pub data_len: u64,
pub data: *const u8,
pub owner: *const Pubkey,
pub rent_epoch: u64,
pub is_signer: bool,
pub is_writable: bool,
pub executable: bool,
}
#[inline(always)]
const fn offset<T, U>(ptr: *const T, offset: usize) -> *const U {
unsafe { (ptr as *const u8).add(offset) as *const U }
}
impl From<&AccountInfo> for CAccountInfo {
fn from(account: &AccountInfo) -> Self {
CAccountInfo {
key: offset(account.raw, 8),
lamports: offset(account.raw, 72),
data_len: account.data_len() as u64,
data: offset(account.raw, 88),
owner: offset(account.raw, 40),
rent_epoch: 0,
is_signer: account.is_signer(),
is_writable: account.is_writable(),
executable: account.executable(),
}
}
}
#[repr(C)]
#[derive(Debug, PartialEq, Clone)]
pub struct CInstruction {
pub program_id: *const Pubkey,
pub accounts: *const CAccountMeta,
pub accounts_len: u64,
pub data: *const u8,
pub data_len: u64,
}