mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-24 07:26:52 +00:00
cleanup utils imports
This commit is contained in:
69
utils/src/macros.rs
Normal file
69
utils/src/macros.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
#[macro_export]
|
||||
macro_rules! impl_to_bytes {
|
||||
($struct_name:ident) => {
|
||||
impl $struct_name {
|
||||
pub fn to_bytes(&self) -> &[u8] {
|
||||
bytemuck::bytes_of(self)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! impl_account_from_bytes {
|
||||
($struct_name:ident) => {
|
||||
impl $crate::AccountDeserialize for $struct_name {
|
||||
fn try_from_bytes(
|
||||
data: &[u8],
|
||||
) -> Result<&Self, solana_program::program_error::ProgramError> {
|
||||
if Self::discriminator().ne(&data[0]) {
|
||||
return Err(solana_program::program_error::ProgramError::InvalidAccountData);
|
||||
}
|
||||
bytemuck::try_from_bytes::<Self>(&data[8..]).or(Err(
|
||||
solana_program::program_error::ProgramError::InvalidAccountData,
|
||||
))
|
||||
}
|
||||
fn try_from_bytes_mut(
|
||||
data: &mut [u8],
|
||||
) -> Result<&mut Self, solana_program::program_error::ProgramError> {
|
||||
if Self::discriminator().ne(&data[0]) {
|
||||
return Err(solana_program::program_error::ProgramError::InvalidAccountData);
|
||||
}
|
||||
bytemuck::try_from_bytes_mut::<Self>(&mut data[8..]).or(Err(
|
||||
solana_program::program_error::ProgramError::InvalidAccountData,
|
||||
))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! impl_instruction_from_bytes {
|
||||
($struct_name:ident) => {
|
||||
impl $struct_name {
|
||||
pub fn try_from_bytes(
|
||||
data: &[u8],
|
||||
) -> Result<&Self, solana_program::program_error::ProgramError> {
|
||||
bytemuck::try_from_bytes::<Self>(data).or(Err(
|
||||
solana_program::program_error::ProgramError::InvalidInstructionData,
|
||||
))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! account {
|
||||
($struct_name:ident) => {
|
||||
$crate::impl_to_bytes!($struct_name);
|
||||
$crate::impl_account_from_bytes!($struct_name);
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! instruction {
|
||||
($struct_name:ident) => {
|
||||
$crate::impl_to_bytes!($struct_name);
|
||||
$crate::impl_instruction_from_bytes!($struct_name);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user