From b1c1bb5b0b50831be64b2d8892726bac165f0d04 Mon Sep 17 00:00:00 2001 From: Dan Burkert Date: Sun, 21 Dec 2014 13:20:17 -0800 Subject: [PATCH] simplify cursor struct hierarchy --- src/cursor.rs | 74 +++++++++++++++++++++++---------------------------- src/lib.rs | 7 ++++- 2 files changed, 39 insertions(+), 42 deletions(-) diff --git a/src/cursor.rs b/src/cursor.rs index ea70af7..682743f 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -16,7 +16,7 @@ pub trait Cursor<'txn> { fn cursor(&self) -> *mut MDB_cursor; } -pub trait ReadCursor<'txn> : Cursor<'txn> { +pub trait CursorExt<'txn> : Cursor<'txn> { /// Retrieves a key/data pair from the cursor. Depending on the cursor op, the current key is /// returned. @@ -45,41 +45,7 @@ pub trait ReadCursor<'txn> : Cursor<'txn> { } } -pub trait WriteCursor<'txn> : ReadCursor<'txn> { - - /// Puts a key/data pair into the database. The cursor will be positioned at the new data item, - /// or on failure usually near it. - fn put(&self, - key: &[u8], - data: &[u8], - flags: WriteFlags) - -> LmdbResult<()> { - - let mut key_val: ffi::MDB_val = ffi::MDB_val { mv_size: key.len() as size_t, - mv_data: key.as_ptr() as *mut c_void }; - let mut data_val: ffi::MDB_val = ffi::MDB_val { mv_size: data.len() as size_t, - mv_data: data.as_ptr() as *mut c_void }; - - unsafe { - lmdb_result(ffi::mdb_cursor_put(self.cursor(), - &mut key_val, - &mut data_val, - flags.bits())) - } - } - - /// Deletes the current key/data pair. - /// - /// ### Flags - /// - /// `MDB_NODUPDATA` may be used to delete all data items for the current key, if the database - /// was opened with `MDB_DUPSORT`. - fn del(&self, flags: WriteFlags) -> LmdbResult<()> { - unsafe { - lmdb_result(ffi::mdb_cursor_del(self.cursor(), flags.bits())) - } - } -} +impl<'txn, T> CursorExt<'txn> for T where T: Cursor<'txn> {} /// A read-only cursor for navigating items within a database. pub struct RoCursor<'txn> { @@ -95,8 +61,6 @@ impl <'txn> Cursor<'txn> for RoCursor<'txn> { } } -impl <'txn> ReadCursor<'txn> for RoCursor<'txn> { } - #[unsafe_destructor] impl <'txn> Drop for RoCursor<'txn> { fn drop(&mut self) { @@ -135,9 +99,6 @@ impl <'txn> Cursor<'txn> for RwCursor<'txn> { } } -impl <'txn> ReadCursor<'txn> for RwCursor<'txn> { } -impl <'txn> WriteCursor<'txn> for RwCursor<'txn> { } - #[unsafe_destructor] impl <'txn> Drop for RwCursor<'txn> { fn drop(&mut self) { @@ -160,6 +121,37 @@ impl <'txn> RwCursor<'txn> { _contravariant: marker::ContravariantLifetime::<'txn>, }) } + + /// Puts a key/data pair into the database. The cursor will be positioned at the new data item, + /// or on failure usually near it. + pub fn put(&self, + key: &[u8], + data: &[u8], + flags: WriteFlags) + -> LmdbResult<()> { + let mut key_val: ffi::MDB_val = ffi::MDB_val { mv_size: key.len() as size_t, + mv_data: key.as_ptr() as *mut c_void }; + let mut data_val: ffi::MDB_val = ffi::MDB_val { mv_size: data.len() as size_t, + mv_data: data.as_ptr() as *mut c_void }; + unsafe { + lmdb_result(ffi::mdb_cursor_put(self.cursor(), + &mut key_val, + &mut data_val, + flags.bits())) + } + } + + /// Deletes the current key/data pair. + /// + /// ### Flags + /// + /// `MDB_NODUPDATA` may be used to delete all data items for the current key, if the database + /// was opened with `MDB_DUPSORT`. + pub fn del(&self, flags: WriteFlags) -> LmdbResult<()> { + unsafe { + lmdb_result(ffi::mdb_cursor_del(self.cursor(), flags.bits())) + } + } } unsafe fn slice_to_val(slice: Option<&[u8]>) -> MDB_val { diff --git a/src/lib.rs b/src/lib.rs index d7732b2..e552262 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,12 @@ extern crate "lmdb-sys" as ffi; extern crate test; extern crate collections; -pub use cursor::Cursor; +pub use cursor::{ + Cursor, + CursorExt, + RoCursor, + RwCursor +}; pub use database::Database; pub use environment::{Environment, EnvironmentBuilder}; pub use error::{LmdbResult, LmdbError};