Skip to main content

dlp_api/state/utils/
to_bytes.rs

1#[macro_export]
2macro_rules! impl_to_bytes_with_discriminator_zero_copy {
3    ($struct_name:ident) => {
4        impl $struct_name {
5            pub fn to_bytes_with_discriminator(
6                &self,
7                data: &mut [u8],
8            ) -> Result<(), ::solana_program::program_error::ProgramError> {
9                let expected_len = 8 + ::std::mem::size_of::<Self>();
10                if data.len() != expected_len {
11                    return Err(
12                        $crate::error::DlpError::InvalidDataLength.into()
13                    );
14                }
15                data[..8].copy_from_slice(&Self::discriminator().to_bytes());
16                data[8..].copy_from_slice(bytemuck::bytes_of(self));
17                Ok(())
18            }
19        }
20    };
21}
22
23#[macro_export]
24macro_rules! impl_to_bytes_with_discriminator_borsh {
25    ($struct_name:ident) => {
26        impl $struct_name {
27            pub fn to_bytes_with_discriminator<W: std::io::Write>(
28                &self,
29                writer: &mut W,
30            ) -> Result<(), ::solana_program::program_error::ProgramError> {
31                writer.write_all(&Self::discriminator().to_bytes())?;
32                self.serialize(writer)?;
33                Ok(())
34            }
35        }
36    };
37}