From 401224f3e992343c3ce695814fb3035077ea734a Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 21 Mar 2024 10:41:55 +1100 Subject: [PATCH] Get Transaction to deref to ReadTransaction --- src/handle.rs | 4 ++-- src/reader.rs | 2 +- src/tx.rs | 28 ++++++++++------------------ 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/handle.rs b/src/handle.rs index 2861670..8189513 100644 --- a/src/handle.rs +++ b/src/handle.rs @@ -448,7 +448,7 @@ impl Handle { pub fn move_prefix(&self, from: &[u8], to: &[u8]) -> Result<()> { let mut tx = self.start_deferred_transaction()?; - let items = tx.read().list_items(from)?; + let items = tx.list_items(from)?; let mut to_vec = to.to_vec(); for item in items { to_vec.truncate(to.len()); @@ -461,7 +461,7 @@ impl Handle { pub fn delete_prefix(&self, prefix: &[u8]) -> PubResult<()> { let mut tx = self.start_deferred_transaction()?; - for item in tx.read().list_items(prefix)? { + for item in tx.list_items(prefix)? { tx.delete_key(&item.key)?; } tx.commit(())?.complete(); diff --git a/src/reader.rs b/src/reader.rs index 96fa5fe..1c6318d 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -68,7 +68,7 @@ impl<'a> Reader<'a> { } pub fn list_items(&self, prefix: &[u8]) -> PubResult> { - self.owned_tx.read().list_items(prefix) + self.owned_tx.list_items(prefix) } fn get_file_clone( diff --git a/src/tx.rs b/src/tx.rs index 2b2c241..ce63cf2 100644 --- a/src/tx.rs +++ b/src/tx.rs @@ -170,28 +170,21 @@ pub(crate) struct Transaction<'h> { altered_files: HashSet, } -// TODO: Implement this, so the .read method doesn't need to exist: writable transactions should -// passthrough to read methods automatically. +// TODO: Try doing this with a read trait that just requires a rusqlite::Transaction be available. -// impl<'h> Deref for Transaction<'h> { -// type Target = ReadTransaction<&'h rusqlite::Transaction<'h>>; -// -// fn deref(&self) -> &Self::Target { -// unsafe { -// &ReadTransaction { -// tx: ReadOnlyRusqliteTransaction { conn: &self.tx }, -// } -// } -// } -// } +impl<'h> Deref for Transaction<'h> { + type Target = ReadTransaction>; -impl<'h> Transaction<'h> { - pub fn read(&self) -> ReadTransaction<&rusqlite::Transaction> { - ReadTransaction { - tx: ReadOnlyRusqliteTransaction { conn: &self.tx }, + fn deref(&self) -> &Self::Target { + unsafe { + std::mem::transmute::<&rusqlite::Transaction, &ReadTransaction>( + &self.tx, + ) } } +} +impl<'h> Transaction<'h> { pub fn new(tx: rusqlite::Transaction<'h>, handle: &'h Handle) -> Self { Self { tx, @@ -353,7 +346,6 @@ impl<'h> Transaction<'h> { if let Some(max) = self.handle.instance_limits.max_value_length_sum { loop { let actual = self - .read() .sum_value_length() .context("reading value_length sum")?; if actual <= max {