From 3a425364c246189b2bb7b10b16f31f285e4c09ab Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Sun, 25 Aug 2024 15:01:00 +0000 Subject: [PATCH] from bytes macro --- api/src/event.rs | 4 ++-- utils/src/macros.rs | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/api/src/event.rs b/api/src/event.rs index abea145..9f41f25 100644 --- a/api/src/event.rs +++ b/api/src/event.rs @@ -1,5 +1,5 @@ use bytemuck::{Pod, Zeroable}; -use ore_utils::impl_to_bytes; +use ore_utils::*; #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)] @@ -9,4 +9,4 @@ pub struct MineEvent { pub timing: i64, } -impl_to_bytes!(MineEvent); +event!(MineEvent); diff --git a/utils/src/macros.rs b/utils/src/macros.rs index 23bbc75..53d7fd5 100644 --- a/utils/src/macros.rs +++ b/utils/src/macros.rs @@ -9,6 +9,17 @@ macro_rules! impl_to_bytes { }; } +#[macro_export] +macro_rules! impl_from_bytes { + ($struct_name:ident) => { + impl $struct_name { + pub fn from_bytes(data: &[u8]) -> &Self { + bytemuck::from_bytes::(data) + } + } + }; +} + #[macro_export] macro_rules! impl_account_from_bytes { ($struct_name:ident) => { @@ -73,3 +84,11 @@ macro_rules! instruction { $crate::impl_instruction_from_bytes!($struct_name); }; } + +#[macro_export] +macro_rules! event { + ($struct_name:ident) => { + $crate::impl_to_bytes!($struct_name); + $crate::impl_from_bytes!($struct_name); + }; +}