account.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. use anchor_lang::prelude::*;
  2. macro_rules! size {
  3. ($name: ident, $size:expr) => {
  4. impl $name {
  5. pub const LEN: usize = $size;
  6. }
  7. };
  8. }
  9. pub const MAX_SIZE: usize = 10;
  10. pub const MAX_SIZE_U8: u8 = 11;
  11. #[account]
  12. pub struct Data {
  13. pub udata: u128, // 16
  14. pub idata: i128, // 16
  15. }
  16. size!(Data, 32);
  17. #[account]
  18. pub struct DataU16 {
  19. pub data: u16, // 2
  20. }
  21. size!(DataU16, 32);
  22. #[account]
  23. pub struct DataI8 {
  24. pub data: i8, // 1
  25. }
  26. size!(DataI8, 1);
  27. #[account]
  28. pub struct DataI16 {
  29. pub data: i16, // 2
  30. }
  31. size!(DataI16, 2);
  32. #[account]
  33. pub struct DataEnum {
  34. pub data: TestEnum, // 1 + 16
  35. }
  36. size!(DataEnum, 17);
  37. #[account(zero_copy)]
  38. pub struct DataZeroCopy {
  39. pub data: u16, // 2
  40. pub _padding: u8, // 1
  41. pub bump: u8, // 1
  42. }
  43. size!(DataZeroCopy, 4);
  44. #[account]
  45. pub struct DataWithFilter {
  46. pub authority: Pubkey, // 32
  47. pub filterable: Pubkey, // 32
  48. }
  49. size!(DataWithFilter, 64);
  50. #[account]
  51. pub struct DataMultidimensionalArray {
  52. pub data: [[u8; 10]; 10], // 100
  53. }
  54. size!(DataMultidimensionalArray, 100);
  55. #[account]
  56. pub struct DataConstArraySize {
  57. pub data: [u8; MAX_SIZE], // 10
  58. }
  59. size!(DataConstArraySize, MAX_SIZE);
  60. #[account]
  61. pub struct DataConstCastArraySize {
  62. pub data_one: [u8; MAX_SIZE as usize],
  63. pub data_two: [u8; MAX_SIZE_U8 as usize],
  64. }
  65. #[account]
  66. pub struct DataMultidimensionalArrayConstSizes {
  67. pub data: [[u8; MAX_SIZE_U8 as usize]; MAX_SIZE],
  68. }
  69. #[derive(Debug, Clone, Copy, AnchorSerialize, AnchorDeserialize, PartialEq, Eq)]
  70. pub enum TestEnum {
  71. First,
  72. Second { x: u64, y: u64 },
  73. TupleTest(u8, u8, u16, u16),
  74. TupleStructTest(TestStruct),
  75. }
  76. #[derive(Debug, Clone, Copy, AnchorSerialize, AnchorDeserialize, PartialEq, Eq)]
  77. pub struct TestStruct {
  78. pub data1: u8,
  79. pub data2: u16,
  80. pub data3: u32,
  81. pub data4: u64,
  82. }