From 21ed66fefcf8a87a228f1e856c315ec908896599 Mon Sep 17 00:00:00 2001 From: Sebastian Jeltsch Date: Tue, 30 Sep 2025 17:27:39 +0200 Subject: [PATCH] Make clippy happy. --- crates/wasm-runtime-host/src/lib.rs | 48 +++++++++++++++-------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/crates/wasm-runtime-host/src/lib.rs b/crates/wasm-runtime-host/src/lib.rs index febd7327..b766f4a9 100644 --- a/crates/wasm-runtime-host/src/lib.rs +++ b/crates/wasm-runtime-host/src/lib.rs @@ -92,10 +92,8 @@ pub enum ExecutorMessage { Run(Box BoxFuture<'static, ()>) + Send>), } -#[derive(Clone)] -struct LockedTransaction(Arc>>); - -unsafe impl Send for LockedTransaction {} +/// NOTE: This is needed due to State needing to be Send. +unsafe impl Send for sqlite::OwnedTx {} struct State { resource_table: ResourceTable, @@ -104,13 +102,13 @@ struct State { kv: WasiKeyValueCtx, shared: Arc, - tx: LockedTransaction, + tx: Arc>>, } impl Drop for State { fn drop(&mut self) { #[cfg(debug_assertions)] - if self.tx.0.lock().is_some() { + if self.tx.lock().is_some() { log::warn!("pending transaction locking the DB"); } } @@ -223,11 +221,11 @@ impl trailbase::runtime::host_endpoint::Host for State { fn tx_begin(&mut self) -> impl Future>> + Send { async fn begin( conn: trailbase_sqlite::Connection, - tx: LockedTransaction, + tx: &Mutex>, ) -> Result<(), TxError> { - assert!(tx.0.lock().is_none()); + assert!(tx.lock().is_none()); - *tx.0.lock() = Some( + *tx.lock() = Some( sqlite::new_tx(conn) .await .map_err(|err| TxError::Other(err.to_string()))?, @@ -237,12 +235,12 @@ impl trailbase::runtime::host_endpoint::Host for State { } let tx = self.tx.clone(); - return async move { Ok(begin(self.shared.conn.clone(), tx).await) }; + return async move { Ok(begin(self.shared.conn.clone(), &tx).await) }; } fn tx_commit(&mut self) -> wasmtime::Result> { - fn commit(tx: LockedTransaction) -> Result<(), TxError> { - let Some(tx) = tx.0.lock().take() else { + fn commit(tx: &Mutex>) -> Result<(), TxError> { + let Some(tx) = tx.lock().take() else { return Err(TxError::Other("no pending tx".to_string())); }; @@ -255,12 +253,12 @@ impl trailbase::runtime::host_endpoint::Host for State { return Ok(()); } - return Ok(commit(self.tx.clone())); + return Ok(commit(&self.tx)); } fn tx_rollback(&mut self) -> wasmtime::Result> { - fn rollback(tx: LockedTransaction) -> Result<(), TxError> { - let Some(tx) = tx.0.lock().take() else { + fn rollback(tx: &Mutex>) -> Result<(), TxError> { + let Some(tx) = tx.lock().take() else { return Err(TxError::Other("no pending tx".to_string())); }; @@ -273,7 +271,7 @@ impl trailbase::runtime::host_endpoint::Host for State { return Ok(()); } - return Ok(rollback(self.tx.clone())); + return Ok(rollback(&self.tx)); } fn tx_execute( @@ -281,10 +279,14 @@ impl trailbase::runtime::host_endpoint::Host for State { query: String, params: Vec, ) -> wasmtime::Result> { - fn execute(tx: LockedTransaction, query: String, params: Vec) -> Result { + fn execute( + tx: &Mutex>, + query: String, + params: Vec, + ) -> Result { let params: Vec<_> = params.into_iter().map(to_sqlite_value).collect(); - let Some(ref tx) = *tx.0.lock() else { + let Some(ref tx) = *tx.lock() else { return Err(TxError::Other("No open transaction".to_string())); }; @@ -304,7 +306,7 @@ impl trailbase::runtime::host_endpoint::Host for State { ); } - return Ok(execute(self.tx.clone(), query, params)); + return Ok(execute(&self.tx, query, params)); } fn tx_query( @@ -313,13 +315,13 @@ impl trailbase::runtime::host_endpoint::Host for State { params: Vec, ) -> wasmtime::Result>, TxError>> { fn query_fn( - tx: LockedTransaction, + tx: &Mutex>, query: String, params: Vec, ) -> Result>, TxError> { let params: Vec<_> = params.into_iter().map(to_sqlite_value).collect(); - let Some(ref tx) = *tx.0.lock() else { + let Some(ref tx) = *tx.lock() else { return Err(TxError::Other("No open transaction".to_string())); }; @@ -345,7 +347,7 @@ impl trailbase::runtime::host_endpoint::Host for State { return Ok(values); } - return Ok(query_fn(self.tx.clone(), query, params)); + return Ok(query_fn(&self.tx, query, params)); } } @@ -649,7 +651,7 @@ impl RuntimeInstance { http: WasiHttpCtx::new(), kv: WasiKeyValueCtx::new(self.shared.kv_store.clone()), shared: self.shared.clone(), - tx: LockedTransaction(Arc::new(Mutex::new(None))), + tx: Arc::new(Mutex::new(None)), }, )); }