@@ -115,10 +115,14 @@ macro_rules! unwrap_or_err {
115
115
///
116
116
/// # Example
117
117
///
118
- /// ```
119
- /// let one = 1;
120
- /// let two = 2;
118
+ /// ```should_panic
119
+ /// # use anchor_lang::prelude::*;
120
+ /// # #[macro_use] extern crate vipers; fn main() -> ProgramResult {
121
+ /// let one = 1_u64;
122
+ /// let two = 2_u64;
121
123
/// let my_value = unwrap_int!(one.checked_sub(2)); // returns an error
124
+ /// Ok(())
125
+ /// # }
122
126
/// ```
123
127
#[ macro_export]
124
128
macro_rules! unwrap_int {
@@ -131,8 +135,17 @@ macro_rules! unwrap_int {
131
135
///
132
136
/// # Example
133
137
///
134
- /// ```
138
+ /// ```should_panic
139
+ /// # use anchor_lang::prelude::*;
140
+ /// # impl From<ErrorCode> for ProgramError { fn from(code: ErrorCode) -> Self { ProgramError::Custom(10) } }
141
+ /// # pub enum ErrorCode { MyError }
142
+ /// # #[macro_use] extern crate vipers; fn main() -> ProgramResult {
143
+ /// fn function_returning_result() -> Result<u64, u64> {
144
+ /// Err(123)
145
+ /// }
146
+ ///
135
147
/// let my_value = try_or_err!(function_returning_result(), MyError);
148
+ /// # Ok(()) }
136
149
/// ```
137
150
#[ macro_export]
138
151
macro_rules! try_or_err {
@@ -146,10 +159,16 @@ macro_rules! try_or_err {
146
159
/// # Example
147
160
///
148
161
/// ```
162
+ /// # use anchor_lang::prelude::*;
163
+ /// # impl From<ErrorCode> for ProgramError { fn from(code: ErrorCode) -> Self { ProgramError::Custom(10) } }
164
+ /// # pub enum ErrorCode { MyError }
165
+ /// # #[macro_use] extern crate vipers; fn main() -> ProgramResult {
166
+ /// let fail = false;
149
167
/// if fail {
150
168
/// return program_err!(MyError);
151
169
/// }
152
170
/// Ok(())
171
+ /// # }
153
172
/// ```
154
173
#[ macro_export]
155
174
macro_rules! program_err {
@@ -158,32 +177,15 @@ macro_rules! program_err {
158
177
} ;
159
178
}
160
179
161
- /// Require or return a [solana_program::program_error::ProgramError], logging the string representation to the program log.
162
- ///
163
- /// # Example
164
- ///
165
- /// ```
166
- /// if fail {
167
- /// return prog_require!(ProgramError::CustomError(10));
168
- /// }
169
- /// Ok(())
170
- /// ```
171
- #[ macro_export]
172
- macro_rules! prog_require {
173
- ( $invariant: expr, $err: expr $( , ) ?) => {
174
- if !( $invariant) {
175
- msg!( "Invariant failed: {:?}" , $err) ;
176
- return Err ( $err. into( ) ) ;
177
- }
178
- } ;
179
- }
180
-
181
180
/// Asserts that an invariant holds, otherwise logs the given message.
182
181
///
183
182
/// # Example
184
183
///
185
- /// ```
184
+ /// ```should_panic
185
+ /// # use anchor_lang::prelude::*;
186
+ /// # #[macro_use] extern crate vipers; fn main() -> ProgramResult {
186
187
/// invariant!(1 == 2, "incorrect");
188
+ /// # Ok(()) }
187
189
/// ```
188
190
#[ macro_export]
189
191
macro_rules! invariant {
@@ -199,17 +201,20 @@ macro_rules! invariant {
199
201
///
200
202
/// # Example
201
203
///
202
- /// ```
203
- /// let one = 1;
204
- /// let two = 2;
204
+ /// ```should_panic
205
+ /// # use anchor_lang::prelude::*;
206
+ /// # #[macro_use] extern crate vipers; fn main() -> ProgramResult {
207
+ /// let one = 1_u64;
208
+ /// let two = 2_u64;
205
209
/// let my_value = unwrap_opt!(one.checked_sub(2), "cannot do this"); // returns an error
210
+ /// # Ok(()) }
206
211
/// ```
207
212
#[ macro_export]
208
- macro_rules! unwrap_opt{
213
+ macro_rules! unwrap_opt {
209
214
( $option: expr, $err: expr $( , ) ?) => {
210
- $option. ok_or_else( || -> {
215
+ $option. ok_or_else( || -> ProgramError {
211
216
msg!( "Option unwrap failed: {:?}" , $err) ;
212
- ProgramError { $crate:: VipersError :: OptionUnwrapFailed . into( ) }
217
+ $crate:: VipersError :: OptionUnwrapFailed . into( )
213
218
} ) ?
214
219
} ;
215
220
}
@@ -239,6 +244,7 @@ mod tests {
239
244
240
245
let weird_math: Option < i32 > = ( 1_i32 ) . checked_add ( 2 ) ;
241
246
let _result = unwrap_int ! ( weird_math) ;
247
+ unwrap_opt ! ( weird_math, "aaa" ) ;
242
248
243
249
Ok ( ( ) )
244
250
}
0 commit comments