From 02af16bb080e833a87ccb663b8959a2a80869706 Mon Sep 17 00:00:00 2001 From: BlackDex Date: Sun, 21 Sep 2025 10:34:21 +0200 Subject: [PATCH] Remove macro, replaced with an function Signed-off-by: BlackDex --- Cargo.lock | 12 ++++++------ Cargo.toml | 2 +- src/db/mod.rs | 50 +++++++++++++++++++++++++++----------------------- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f984b713..2a9402d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4726,9 +4726,9 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.225" +version = "1.0.226" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd6c24dee235d0da097043389623fb913daddf92c76e9f5a1db88607a0bcbd1d" +checksum = "0dca6411025b24b60bfa7ec1fe1f8e710ac09782dca409ee8237ba74b51295fd" dependencies = [ "serde_core", "serde_derive", @@ -4756,18 +4756,18 @@ dependencies = [ [[package]] name = "serde_core" -version = "1.0.225" +version = "1.0.226" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "659356f9a0cb1e529b24c01e43ad2bdf520ec4ceaf83047b83ddcc2251f96383" +checksum = "ba2ba63999edb9dac981fb34b3e5c0d111a69b0924e253ed29d83f7c99e966a4" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.225" +version = "1.0.226" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ea936adf78b1f766949a4977b91d2f5595825bd6ec079aa9543ad2685fc4516" +checksum = "8db53ae22f34573731bafa1db20f04027b2d25e02d8205921b569171699cdb33" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index c17bb81f..d1f36aa1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -80,7 +80,7 @@ tokio = { version = "1.47.1", features = ["rt-multi-thread", "fs", "io-util", "p tokio-util = { version = "0.7.16", features = ["compat"]} # A generic serialization/deserialization framework -serde = { version = "1.0.225", features = ["derive"] } +serde = { version = "1.0.226", features = ["derive"] } serde_json = "1.0.145" # A safe, extensible ORM and Query builder diff --git a/src/db/mod.rs b/src/db/mod.rs index 5758bdac..7d87877d 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -247,34 +247,38 @@ impl DbConnType { } } -#[macro_export] -macro_rules! db_run_base { - ( $conn:ident ) => { - let conn = std::sync::Arc::clone(&$conn.conn); +impl DbConn { + pub async fn run(&self, f: F) -> R + where + F: FnOnce(&mut DbConnInner) -> R + Send, + R: Send + 'static, + { + let conn = Arc::clone(&self.conn); let mut conn = conn.lock_owned().await; - let $conn = conn.as_mut().expect("internal invariant broken: self.conn is Some"); - }; + let conn = conn.as_mut().expect("Internal invariant broken: self.conn is Some"); + + // Run blocking can't be used due to the 'static limitation, use block_in_place instead + tokio::task::block_in_place(move || f(conn)) + } } #[macro_export] macro_rules! db_run { - ( $conn:ident: $body:block ) => {{ - db_run_base!($conn); - // Run blocking can't be used due to the 'static limitation, use block_in_place instead - tokio::task::block_in_place(move || $body ) - }}; - - ( $conn:ident: $( $($db:ident),+ $body:block )+ ) => {{ - db_run_base!($conn); - match std::ops::DerefMut::deref_mut($conn) { - $($( - #[cfg($db)] - pastey::paste!(&mut $crate::db::DbConnInner::[<$db:camel>](ref mut $conn)) => { - // Run blocking can't be used due to the 'static limitation, use block_in_place instead - tokio::task::block_in_place(move || $body ) - }, - )+)+} - }}; + ( $conn:ident: $body:block ) => { + $conn.run(move |$conn| $body).await + }; + + ( $conn:ident: $( $($db:ident),+ $body:block )+ ) => { + $conn.run(move |$conn| { + match $conn { + $($( + #[cfg($db)] + pastey::paste!(&mut $crate::db::DbConnInner::[<$db:camel>](ref mut $conn)) => { + $body + }, + )+)+} + }).await + }; } pub mod schema;