changeset 623:2390a37c0086

Merge pull request #40 from Velnbur/master Make `AsyncDB` clonable
author Lewin Bormann <lbo@spheniscida.de>
date Fri, 08 Sep 2023 12:49:45 +0200
parents dbf8977e966a (current diff) 8fec6e2bd06c (diff)
children bf64ad2d36d1
files
diffstat 1 files changed, 7 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/asyncdb.rs	Thu Aug 31 08:01:36 2023 +0200
+++ b/src/asyncdb.rs	Fri Sep 08 12:49:45 2023 +0200
@@ -1,5 +1,6 @@
 use std::collections::hash_map::HashMap;
 use std::path::Path;
+use std::sync::Arc;
 
 use crate::{Options, Result, Status, StatusCode, WriteBatch, DB};
 
@@ -45,8 +46,9 @@
 ///
 /// TODO: Make it work in other runtimes as well. This is a matter of adapting the blocking thread
 /// mechanism as well as the channel types.
+#[derive(Clone)]
 pub struct AsyncDB {
-    jh: JoinHandle<()>,
+    jh: Arc<JoinHandle<()>>,
     send: mpsc::Sender<Message>,
 }
 
@@ -56,7 +58,10 @@
         let db = DB::open(name, opts)?;
         let (send, recv) = mpsc::channel(CHANNEL_BUFFER_SIZE);
         let jh = spawn_blocking(move || AsyncDB::run_server(db, recv));
-        Ok(AsyncDB { jh, send })
+        Ok(AsyncDB {
+            jh: Arc::new(jh),
+            send,
+        })
     }
 
     pub async fn close(&self) -> Result<()> {