sqlx_model/
executer_option.rs1use sqlx::Pool;
2use sqlx::{Database, Transaction};
3use std::borrow::BorrowMut;
4
5pub trait ExecutorOptionTransaction {
6 fn as_copy(&mut self) -> &mut Self;
7}
8
9impl<'t, DB> ExecutorOptionTransaction for Transaction<'t, DB>
10where
11 DB: Database,
12{
13 fn as_copy(&mut self) -> &mut Self {
14 self.borrow_mut()
15 }
16}
17
18pub trait ExecutorOptionPool {
19 fn as_copy(&self) -> &Self;
20}
21
22impl<DB> ExecutorOptionPool for Pool<DB>
23where
24 DB: Database,
25{
26 fn as_copy(&self) -> &Self {
27 self
28 }
29}
30
31#[macro_export]
32macro_rules! executor_option {
38 ($block:block,$transaction:expr,$poll:expr,$execute:tt) => {
39 match $transaction {
40 Some($execute) => {
41 #[allow(unused_imports)]
42 use $crate::ExecutorOptionTransaction;
43 $block
44 }
45 None => {
46 #[allow(unused_imports)]
47 use $crate::ExecutorOptionPool;
48 let $execute = $poll;
49 $block
50 }
51 }
52 };
53}
54
55#[test]
56fn test_executor_option() {
57 let va: Option<i32> = None;
58 let vb = 1;
59 let a = executor_option!({ aa }, va, vb, aa);
60 assert!(a == 1);
61
62 let va: Option<i32> = Some(2);
63 let vb = 1;
64 let a = executor_option!({ aa }, va, vb, aa);
65 assert!(a == 2);
66}