From 1cae751c873303648670d556f06d15e662f59426 Mon Sep 17 00:00:00 2001 From: Oscar Beaumont Date: Sun, 23 Jun 2024 16:20:29 +0800 Subject: [PATCH 1/3] Implement `Connection::from` --- src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index fbf89ee..7fa2f08 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -174,6 +174,11 @@ pub struct Connection { } impl Connection { + /// Convert an existing `rusqlite::Connection` into a `Connection`. + pub async fn from(conn: rusqlite::Connection) -> Self { + start(move || Ok(conn)).await.expect(BUG_TEXT) + } + /// Open a new connection to a SQLite database. /// /// `Connection::open(path)` is equivalent to From 94039c30884177620d70cc15d692efbb4cf36c0b Mon Sep 17 00:00:00 2001 From: Oscar Beaumont Date: Sun, 23 Jun 2024 16:29:53 +0800 Subject: [PATCH 2/3] Use `std::from::From` instead --- src/lib.rs | 55 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7fa2f08..659dc32 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -98,7 +98,7 @@ #[cfg(test)] mod tests; -use crossbeam_channel::Sender; +use crossbeam_channel::{Receiver, Sender}; use std::{ fmt::{self, Debug, Display}, path::Path, @@ -366,6 +366,15 @@ impl Debug for Connection { } } +impl From for Connection { + fn from(conn: rusqlite::Connection) -> Self { + let (sender, receiver) = crossbeam_channel::unbounded::(); + thread::spawn(move || event_loop(conn, receiver)); + + Self { sender } + } +} + async fn start(open: F) -> rusqlite::Result where F: FnOnce() -> rusqlite::Result + Send + 'static, @@ -374,7 +383,7 @@ where let (result_sender, result_receiver) = oneshot::channel(); thread::spawn(move || { - let mut conn = match open() { + let conn = match open() { Ok(c) => c, Err(e) => { let _ = result_sender.send(Err(e)); @@ -386,25 +395,7 @@ where return; } - while let Ok(message) = receiver.recv() { - match message { - Message::Execute(f) => f(&mut conn), - Message::Close(s) => { - let result = conn.close(); - - match result { - Ok(v) => { - s.send(Ok(v)).expect(BUG_TEXT); - break; - } - Err((c, e)) => { - conn = c; - s.send(Err(e)).expect(BUG_TEXT); - } - } - } - } - } + event_loop(conn, receiver); }); result_receiver @@ -412,3 +403,25 @@ where .expect(BUG_TEXT) .map(|_| Connection { sender }) } + +fn event_loop(mut conn: rusqlite::Connection, receiver: Receiver) { + while let Ok(message) = receiver.recv() { + match message { + Message::Execute(f) => f(&mut conn), + Message::Close(s) => { + let result = conn.close(); + + match result { + Ok(v) => { + s.send(Ok(v)).expect(BUG_TEXT); + break; + } + Err((c, e)) => { + conn = c; + s.send(Err(e)).expect(BUG_TEXT); + } + } + } + } + } +} From 1e12d973e727d385c4bc3a7a0b3c5f046a87352f Mon Sep 17 00:00:00 2001 From: Oscar Beaumont Date: Sun, 23 Jun 2024 16:33:39 +0800 Subject: [PATCH 3/3] Remove legacy impl --- src/lib.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 659dc32..2c52100 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -174,11 +174,6 @@ pub struct Connection { } impl Connection { - /// Convert an existing `rusqlite::Connection` into a `Connection`. - pub async fn from(conn: rusqlite::Connection) -> Self { - start(move || Ok(conn)).await.expect(BUG_TEXT) - } - /// Open a new connection to a SQLite database. /// /// `Connection::open(path)` is equivalent to