1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/// Returns a raw slice of the account's bytes.
#[macro_export]
macro_rules! impl_to_bytes {
($struct_name:ident) => {
impl $struct_name {
#[inline]
pub fn to_bytes(&self) -> &[u8] {
// SAFETY:
// 1. `self` lives as long as the returned slice,
// 2. pointer is aligned to `align_of::<Self>()`,
// 3. length is exactly `size_of::<Self>()`.
unsafe {
core::slice::from_raw_parts(
self as *const _ as *const u8,
core::mem::size_of::<Self>(),
)
}
}
}
};
}
/// Returns a mutable raw slice of the account's bytes.
#[macro_export]
macro_rules! impl_to_bytes_mut {
($struct_name:ident) => {
impl $struct_name {
#[inline]
pub fn to_bytes_mut(&mut self) -> &mut [u8] {
// SAFETY:
// 1. `self` lives as long as the returned slice,
// 2. pointer is aligned to `align_of::<Self>()`,
// 3. length is exactly `size_of::<Self>()`.
unsafe {
core::slice::from_raw_parts_mut(
self as *mut _ as *mut u8,
core::mem::size_of::<Self>(),
)
}
}
}
};
}
#[macro_export]
macro_rules! account {
($discriminator_name:ident, $struct_name:ident) => {
$crate::impl_to_bytes!($struct_name);
impl $crate::Account for $struct_name {}
impl $crate::Discriminator for $struct_name {
#[inline(always)]
fn discriminator() -> u8 {
$discriminator_name::$struct_name as u8
}
}
impl $crate::AccountValidation for $struct_name {
#[track_caller]
fn assert<F>(
&self,
condition: F,
) -> Result<&Self, pinocchio::program_error::ProgramError>
where
F: Fn(&Self) -> bool,
{
if !condition(self) {
return Err($crate::trace(
"Account data is invalid",
pinocchio::program_error::ProgramError::InvalidAccountData,
));
}
Ok(self)
}
#[track_caller]
fn assert_err<F, E>(
&self,
condition: F,
err: E,
) -> Result<&Self, pinocchio::program_error::ProgramError>
where
F: Fn(&Self) -> bool,
E: Into<pinocchio::program_error::ProgramError>,
{
if !condition(self) {
return Err(err.into());
}
Ok(self)
}
#[track_caller]
fn assert_mut<F>(
&mut self,
condition: F,
) -> Result<&mut Self, pinocchio::program_error::ProgramError>
where
F: Fn(&Self) -> bool,
{
if !condition(self) {
return Err($crate::trace(
"Account data is invalid",
pinocchio::program_error::ProgramError::InvalidAccountData,
));
}
Ok(self)
}
#[track_caller]
fn assert_mut_err<F, E>(
&mut self,
condition: F,
err: E,
) -> Result<&mut Self, pinocchio::program_error::ProgramError>
where
F: Fn(&Self) -> bool,
E: Into<pinocchio::program_error::ProgramError>,
{
if !condition(self) {
return Err(err.into());
}
Ok(self)
}
}
};
}
#[macro_export]
macro_rules! error {
($struct_name:ident) => {
impl From<$struct_name> for pinocchio::program_error::ProgramError {
fn from(e: $struct_name) -> Self {
pinocchio_log::log!(
"Error Number: {}. Error Message: {}.",
e as u32,
e.message()
);
pinocchio::program_error::ProgramError::Custom(e as u32)
}
}
};
}
/// Declare a log-gable event struct.
#[macro_export]
macro_rules! event {
($struct_name:ident) => {
$crate::impl_to_bytes!($struct_name);
impl $crate::Loggable for $struct_name {
fn log(&self) {
pinocchio::log::sol_log_data(&[&self.to_bytes()]);
}
fn log_return(&self) {
pinocchio::program::set_return_data(&self.to_bytes());
}
}
};
}
#[macro_export]
macro_rules! instruction {
($discriminator_name:ident, $struct_name:ident) => {
impl $crate::Instruction for $struct_name {}
impl $crate::Discriminator for $struct_name {
#[inline(always)]
fn discriminator() -> u8 {
$discriminator_name::$struct_name as u8
}
}
// Compared to a standard "to_bytes" impl add a header with discriminator
impl $struct_name {
pub fn to_bytes(&self) -> $crate::alloc::vec::Vec<u8> {
unsafe {
[
[$discriminator_name::$struct_name as u8].to_vec(),
core::slice::from_raw_parts(
self as *const _ as *const u8,
core::mem::size_of::<Self>(),
)
.to_vec(),
]
.concat()
}
}
}
};
}