Skip to content

Commit 401224f

Browse files
committed
Get Transaction to deref to ReadTransaction
1 parent c91d19b commit 401224f

File tree

3 files changed

+13
-21
lines changed

3 files changed

+13
-21
lines changed

src/handle.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ impl Handle {
448448

449449
pub fn move_prefix(&self, from: &[u8], to: &[u8]) -> Result<()> {
450450
let mut tx = self.start_deferred_transaction()?;
451-
let items = tx.read().list_items(from)?;
451+
let items = tx.list_items(from)?;
452452
let mut to_vec = to.to_vec();
453453
for item in items {
454454
to_vec.truncate(to.len());
@@ -461,7 +461,7 @@ impl Handle {
461461

462462
pub fn delete_prefix(&self, prefix: &[u8]) -> PubResult<()> {
463463
let mut tx = self.start_deferred_transaction()?;
464-
for item in tx.read().list_items(prefix)? {
464+
for item in tx.list_items(prefix)? {
465465
tx.delete_key(&item.key)?;
466466
}
467467
tx.commit(())?.complete();

src/reader.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl<'a> Reader<'a> {
6868
}
6969

7070
pub fn list_items(&self, prefix: &[u8]) -> PubResult<Vec<Item>> {
71-
self.owned_tx.read().list_items(prefix)
71+
self.owned_tx.list_items(prefix)
7272
}
7373

7474
fn get_file_clone(

src/tx.rs

+10-18
Original file line numberDiff line numberDiff line change
@@ -170,28 +170,21 @@ pub(crate) struct Transaction<'h> {
170170
altered_files: HashSet<FileId>,
171171
}
172172

173-
// TODO: Implement this, so the .read method doesn't need to exist: writable transactions should
174-
// passthrough to read methods automatically.
173+
// TODO: Try doing this with a read trait that just requires a rusqlite::Transaction be available.
175174

176-
// impl<'h> Deref for Transaction<'h> {
177-
// type Target = ReadTransaction<&'h rusqlite::Transaction<'h>>;
178-
//
179-
// fn deref(&self) -> &Self::Target {
180-
// unsafe {
181-
// &ReadTransaction {
182-
// tx: ReadOnlyRusqliteTransaction { conn: &self.tx },
183-
// }
184-
// }
185-
// }
186-
// }
175+
impl<'h> Deref for Transaction<'h> {
176+
type Target = ReadTransaction<rusqlite::Transaction<'h>>;
187177

188-
impl<'h> Transaction<'h> {
189-
pub fn read(&self) -> ReadTransaction<&rusqlite::Transaction> {
190-
ReadTransaction {
191-
tx: ReadOnlyRusqliteTransaction { conn: &self.tx },
178+
fn deref(&self) -> &Self::Target {
179+
unsafe {
180+
std::mem::transmute::<&rusqlite::Transaction, &ReadTransaction<rusqlite::Transaction>>(
181+
&self.tx,
182+
)
192183
}
193184
}
185+
}
194186

187+
impl<'h> Transaction<'h> {
195188
pub fn new(tx: rusqlite::Transaction<'h>, handle: &'h Handle) -> Self {
196189
Self {
197190
tx,
@@ -353,7 +346,6 @@ impl<'h> Transaction<'h> {
353346
if let Some(max) = self.handle.instance_limits.max_value_length_sum {
354347
loop {
355348
let actual = self
356-
.read()
357349
.sum_value_length()
358350
.context("reading value_length sum")?;
359351
if actual <= max {

0 commit comments

Comments
 (0)