Reworked thread safe connection be threadsafer,,,, again

Co-Authored-By: kay@zed.dev
This commit is contained in:
Mikayla Maki 2022-12-01 18:31:05 -08:00
parent 189a820113
commit 5e240f98f0
12 changed files with 741 additions and 584 deletions

View file

@ -1,4 +1,5 @@
use std::{
cell::RefCell,
ffi::{CStr, CString},
marker::PhantomData,
path::Path,
@ -11,7 +12,7 @@ use libsqlite3_sys::*;
pub struct Connection {
pub(crate) sqlite3: *mut sqlite3,
persistent: bool,
pub(crate) write: bool,
pub(crate) write: RefCell<bool>,
_sqlite: PhantomData<sqlite3>,
}
unsafe impl Send for Connection {}
@ -21,7 +22,7 @@ impl Connection {
let mut connection = Self {
sqlite3: 0 as *mut _,
persistent,
write: true,
write: RefCell::new(true),
_sqlite: PhantomData,
};
@ -64,7 +65,7 @@ impl Connection {
}
pub fn can_write(&self) -> bool {
self.write
*self.write.borrow()
}
pub fn backup_main(&self, destination: &Connection) -> Result<()> {
@ -152,6 +153,13 @@ impl Connection {
))
}
}
pub(crate) fn with_write<T>(&self, callback: impl FnOnce(&Connection) -> T) -> T {
*self.write.borrow_mut() = true;
let result = callback(self);
*self.write.borrow_mut() = false;
result
}
}
impl Drop for Connection {