From f021813ed443b4ef7224d1934881abecf53977a7 Mon Sep 17 00:00:00 2001 From: Dirk Van Haerenborgh Date: Thu, 19 Apr 2018 10:21:14 +0200 Subject: cleanup & export db revision uuid --- src/database.rs | 58 ++++++++++++++++++++++++++++---------------------------- src/directory.rs | 16 ++++++---------- src/ffi.rs | 2 +- src/filenames.rs | 40 +++++++++++++++++++------------------- src/lib.rs | 1 + src/message.rs | 17 +++++++---------- src/messages.rs | 24 ++++++++++++++--------- src/query.rs | 19 ++++++++----------- src/tags.rs | 23 +++++++++------------- src/thread.rs | 16 +++++++--------- src/threads.rs | 21 ++++++++------------ src/utils.rs | 1 - 12 files changed, 111 insertions(+), 127 deletions(-) (limited to 'src') diff --git a/src/database.rs b/src/database.rs index 2b95a7a..f1d743b 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,9 +1,7 @@ -use std::{ - ops, - path, - ptr, -}; - +use std::ops::Drop; +use std::iter::Iterator; +use std::ptr; +use std::path::Path; use std::ffi::CString; use libc; @@ -14,9 +12,9 @@ use utils::{ ToStr, }; -use directory::Directory; -use query::Query; -use tags::Tags; +use Directory; +use Query; +use Tags; use ffi; @@ -26,14 +24,17 @@ pub use ffi::DatabaseMode; #[derive(Copy, Clone, Debug)] pub struct Version(libc::c_uint); -#[derive(Copy, Clone, Debug)] -pub struct Revision(libc::c_ulong); +#[derive(Clone, Debug)] +pub struct Revision{ + revision: libc::c_ulong, + uuid: String +} #[derive(Debug)] pub struct Database(*mut ffi::notmuch_database_t); impl Database { - pub fn create>(path: &P) -> Result { + pub fn create>(path: &P) -> Result { let path_str = CString::new(path.as_ref().to_str().unwrap()).unwrap(); let mut db = ptr::null_mut(); @@ -44,7 +45,7 @@ impl Database { Ok(Database(db)) } - pub fn open>(path: &P, mode: DatabaseMode) -> Result { + pub fn open>(path: &P, mode: DatabaseMode) -> Result { let path_str = CString::new(path.as_ref().to_str().unwrap()).unwrap(); let mut db = ptr::null_mut(); @@ -67,20 +68,20 @@ impl Database { Ok(()) } - pub fn compact, F: FnMut(&str)>( + pub fn compact, F: FnMut(&str)>( path: &P, backup_path: Option<&P>, ) -> Result<()> { let status: Option = None; Database::_compact(path, backup_path, status) } - pub fn compact_with_status, F: FnMut(&str)>( + pub fn compact_with_status, F: FnMut(&str)>( path: &P, backup_path: Option<&P>, status: F, ) -> Result<()> { Database::_compact(path, backup_path, Some(status)) } - fn _compact, F: FnMut(&str)>( + fn _compact, F: FnMut(&str)>( path: &P, backup_path: Option<&P>, status: Option, ) -> Result<()> { @@ -112,8 +113,8 @@ impl Database { Ok(()) } - pub fn path(&self) -> &path::Path { - path::Path::new(unsafe { + pub fn path(&self) -> &Path { + Path::new(unsafe { ffi::notmuch_database_get_path(self.0) }.to_str().unwrap()) } @@ -125,10 +126,14 @@ impl Database { } pub fn revision(&self) -> Revision { - let uuid = ptr::null_mut(); - Revision(unsafe { - ffi::notmuch_database_get_revision(self.0, uuid) - }) + let uuid_p: *mut libc::c_char = ptr::null_mut(); + let revision = unsafe { + ffi::notmuch_database_get_revision(self.0, (&uuid_p) as *const _ as *const *mut libc::c_char) + }; + + let uuid = unsafe { CString::from_raw(uuid_p) }; + + Revision{revision, uuid: uuid.to_str().unwrap().to_string()} } pub fn needs_upgrade(&self) -> bool { @@ -171,7 +176,7 @@ impl Database { Ok(()) } - pub fn directory<'d, P: AsRef>(&'d self, path: &P) -> Result>> { + pub fn directory<'d, P: AsRef>(&'d self, path: &P) -> Result>> { let path_str = CString::new(path.as_ref().to_str().unwrap()).unwrap(); let mut dir = ptr::null_mut(); @@ -186,7 +191,6 @@ impl Database { pub fn create_query<'d>(&'d self, query_string: &str) -> Result> { let query_str = CString::new(query_string).unwrap(); - println!("query {:?}", query_str); let query = unsafe { ffi::notmuch_query_create(self.0, query_str.as_ptr()) @@ -203,13 +207,9 @@ impl Database { Ok(Tags::new(tags)) } - - - - } -impl ops::Drop for Database { +impl Drop for Database { fn drop(&mut self) { unsafe { ffi::notmuch_database_destroy(self.0) diff --git a/src/directory.rs b/src/directory.rs index d3097f2..b0bb415 100644 --- a/src/directory.rs +++ b/src/directory.rs @@ -1,11 +1,7 @@ -use std::{ - ops, - marker, -}; +use std::ops::Drop; +use std::marker::PhantomData; -use utils::{ - NewFromPtr, -}; +use utils::NewFromPtr; use Database; use Filenames; @@ -15,7 +11,7 @@ use ffi; #[derive(Debug)] pub struct Directory<'d>( *mut ffi::notmuch_directory_t, - marker::PhantomData<&'d Database>, + PhantomData<&'d Database>, ); impl<'d> Directory<'d>{ @@ -28,11 +24,11 @@ impl<'d> Directory<'d>{ impl<'d> NewFromPtr<*mut ffi::notmuch_directory_t> for Directory<'d> { fn new(ptr: *mut ffi::notmuch_directory_t) -> Directory<'d> { - Directory(ptr, marker::PhantomData) + Directory(ptr, PhantomData) } } -impl<'d> ops::Drop for Directory<'d> { +impl<'d> Drop for Directory<'d> { fn drop(self: &mut Self) { unsafe { ffi::notmuch_directory_destroy(self.0) diff --git a/src/ffi.rs b/src/ffi.rs index 3c281fa..5091b43 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -399,7 +399,7 @@ extern { /// this database. Two revision numbers are only comparable if they /// have the same database UUID. pub fn notmuch_database_get_revision(notmuch: *mut notmuch_database_t, - uuid: *mut *const c_char) + uuid: *const *mut c_char) -> c_ulong; /// Retrieve a directory object from the database for 'path'. diff --git a/src/filenames.rs b/src/filenames.rs index 67c03a5..b698b78 100644 --- a/src/filenames.rs +++ b/src/filenames.rs @@ -1,45 +1,45 @@ -use std::{ - ops, - marker, - iter -}; - -use std::path::{ - PathBuf -}; - -use std::ffi::{ - CStr -}; +use std::ops::Drop; +use std::iter::Iterator; +use std::marker::PhantomData; +use std::path::PathBuf; +use std::ffi::CStr; use utils::{ NewFromPtr, }; -use database; +use Database; +use Directory; +use Message; use ffi; #[derive(Debug)] pub struct Filenames<'d>( *mut ffi::notmuch_filenames_t, - marker::PhantomData<&'d database::Database>, + PhantomData<&'d Database>, ); impl<'d> NewFromPtr<*mut ffi::notmuch_filenames_t> for Filenames<'d> { fn new(ptr: *mut ffi::notmuch_filenames_t) -> Filenames<'d> { - Filenames(ptr, marker::PhantomData) + Filenames(ptr, PhantomData) } } -impl<'d> ops::Drop for Filenames<'d> { +impl<'d> Drop for Filenames<'d> { fn drop(self: &mut Self) { - unsafe { - ffi::notmuch_filenames_destroy(self.0) + let valid = unsafe { + ffi::notmuch_filenames_valid(self.0) }; + + if valid != 0 { + unsafe { + ffi::notmuch_filenames_destroy(self.0) + }; + } } } -impl<'d> iter::Iterator for Filenames<'d> { +impl<'d> Iterator for Filenames<'d> { type Item = PathBuf; fn next(self: &mut Self) -> Option { diff --git a/src/lib.rs b/src/lib.rs index db3c276..8120bd0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,7 @@ mod filenames; pub use error::Error; pub use database::Database; +pub use directory::Directory; pub use query::Query; pub use messages::Messages; pub use message::Message; diff --git a/src/message.rs b/src/message.rs index 60011d1..c3b100f 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1,14 +1,11 @@ -use std::{ - ops, - marker -}; - +use std::ops::Drop; +use std::marker::PhantomData; use std::path::PathBuf; use ffi; use utils::{ - NewFromPtr, - ToStr + ToStr, + NewFromPtr }; use Query; use Messages; @@ -17,12 +14,12 @@ use Filenames; #[derive(Debug)] pub struct Message<'q, 'd:'q>( pub(crate) *mut ffi::notmuch_message_t, - marker::PhantomData<&'q Query<'d>>, + PhantomData<&'q Query<'d>>, ); impl<'q, 'd> NewFromPtr<*mut ffi::notmuch_message_t> for Message<'q, 'd> { fn new(ptr: *mut ffi::notmuch_message_t) -> Message<'q, 'd> { - Message(ptr, marker::PhantomData) + Message(ptr, PhantomData) } } @@ -70,7 +67,7 @@ impl<'q, 'd> Message<'q, 'd>{ } -impl<'q, 'd> ops::Drop for Message<'q, 'd> { +impl<'q, 'd> Drop for Message<'q, 'd> { fn drop(self: &mut Self) { unsafe { ffi::notmuch_message_destroy(self.0) diff --git a/src/messages.rs b/src/messages.rs index 1d3b3c2..139b9a8 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -1,8 +1,6 @@ -use std::{ - ops, - marker, - iter -}; +use std::ops::Drop; +use std::iter::Iterator; +use std::marker::PhantomData; use ffi; use utils::{ @@ -17,12 +15,12 @@ pub struct Messages<'q, 'd:'q>( // TODO: is this lifetime specifier correct? // query may outlive messages. pub(crate) *mut ffi::notmuch_messages_t, - marker::PhantomData<&'q Query<'d>>, + PhantomData<&'q Query<'d>>, ); impl<'q, 'd> NewFromPtr<*mut ffi::notmuch_messages_t> for Messages<'q, 'd> { fn new(ptr: *mut ffi::notmuch_messages_t) -> Messages<'q, 'd> { - Messages(ptr, marker::PhantomData) + Messages(ptr, PhantomData) } } @@ -36,15 +34,23 @@ impl<'q, 'd> Messages<'q, 'd>{ } -impl<'q, 'd> ops::Drop for Messages<'q, 'd> { +impl<'q, 'd> Drop for Messages<'q, 'd> { fn drop(self: &mut Self) { + let valid = unsafe { + ffi::notmuch_messages_valid(self.0) + }; + + if valid == 0{ + return; + } + unsafe { ffi::notmuch_messages_destroy(self.0) }; } } -impl<'q, 'd> iter::Iterator for Messages<'q, 'd> { +impl<'q, 'd> Iterator for Messages<'q, 'd> { type Item = Message<'q, 'd>; fn next(&mut self) -> Option { diff --git a/src/query.rs b/src/query.rs index 125e63d..e0c748e 100644 --- a/src/query.rs +++ b/src/query.rs @@ -1,15 +1,12 @@ -use std::{ - ops, - marker, - ptr, -}; +use std::ops::Drop; +use std::ptr; +use std::marker::PhantomData; use error::Result; use ffi; -use utils::{ - NewFromPtr, -}; +use utils::NewFromPtr; + use Database; use Messages; use Threads; @@ -18,7 +15,7 @@ use ffi::Sort; #[derive(Debug)] pub struct Query<'d>( pub(crate) *mut ffi::notmuch_query_t, - marker::PhantomData<&'d Database>, + PhantomData<&'d Database>, ); @@ -101,12 +98,12 @@ impl<'d> Query<'d> { impl<'d> NewFromPtr<*mut ffi::notmuch_query_t> for Query<'d> { fn new(ptr: *mut ffi::notmuch_query_t) -> Query<'d> { - Query(ptr, marker::PhantomData) + Query(ptr, PhantomData) } } -impl<'d> ops::Drop for Query<'d> { +impl<'d> Drop for Query<'d> { fn drop(&mut self) { unsafe { ffi::notmuch_query_destroy(self.0) diff --git a/src/tags.rs b/src/tags.rs index 02ee767..04c7cd5 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -1,33 +1,28 @@ -use std::{ - ops, - marker, - iter -}; - -use std::ffi::{ - CStr -}; +use std::ops::Drop; +use std::iter::Iterator; +use std::marker::PhantomData; +use std::ffi::CStr; use utils::{ NewFromPtr, }; -use database; +use Database; use ffi; #[derive(Debug)] pub struct Tags<'d>( *mut ffi::notmuch_tags_t, - marker::PhantomData<&'d database::Database>, + PhantomData<&'d Database>, ); impl<'d> NewFromPtr<*mut ffi::notmuch_tags_t> for Tags<'d> { fn new(ptr: *mut ffi::notmuch_tags_t) -> Tags<'d> { - Tags(ptr, marker::PhantomData) + Tags(ptr, PhantomData) } } -impl<'d> ops::Drop for Tags<'d> { +impl<'d> Drop for Tags<'d> { fn drop(&mut self) { unsafe { ffi::notmuch_tags_destroy(self.0) @@ -35,7 +30,7 @@ impl<'d> ops::Drop for Tags<'d> { } } -impl<'d> iter::Iterator for Tags<'d> { +impl<'d> Iterator for Tags<'d> { type Item = String; fn next(&mut self) -> Option { diff --git a/src/thread.rs b/src/thread.rs index 47166d4..dfe0a69 100644 --- a/src/thread.rs +++ b/src/thread.rs @@ -1,8 +1,5 @@ -use std::{ - ops, - marker -}; - +use std::ops::Drop; +use std::marker::PhantomData; use ffi; use utils::{ NewFromPtr, @@ -15,12 +12,12 @@ use Tags; #[derive(Debug)] pub struct Thread<'q, 'd:'q>( pub(crate) *mut ffi::notmuch_thread_t, - marker::PhantomData<&'q Query<'d>>, + PhantomData<&'q Query<'d>>, ); impl<'q, 'd> NewFromPtr<*mut ffi::notmuch_thread_t> for Thread<'q, 'd> { fn new(ptr: *mut ffi::notmuch_thread_t) -> Thread<'q, 'd> { - Thread(ptr, marker::PhantomData) + Thread(ptr, PhantomData) } } @@ -39,7 +36,8 @@ impl<'q, 'd> Thread<'q, 'd>{ ffi::notmuch_thread_get_total_messages(self.0) } } -#[cfg(feature = "0.26")] + + #[cfg(feature = "0.26")] pub fn total_files(self: &Self) -> i32{ unsafe { ffi::notmuch_thread_get_total_files(self.0) @@ -100,7 +98,7 @@ impl<'q, 'd> Thread<'q, 'd>{ } -impl<'q, 'd> ops::Drop for Thread<'q, 'd> { +impl<'q, 'd> Drop for Thread<'q, 'd> { fn drop(&mut self) { unsafe { ffi::notmuch_thread_destroy(self.0) diff --git a/src/threads.rs b/src/threads.rs index 8ec7c95..9a0117f 100644 --- a/src/threads.rs +++ b/src/threads.rs @@ -1,13 +1,8 @@ -use std::{ - ops, - marker, - iter -}; - -use utils::{ - NewFromPtr, -}; +use std::ops::Drop; +use std::iter::Iterator; +use std::marker::PhantomData; +use utils::NewFromPtr; use Query; use Thread; use ffi; @@ -15,16 +10,16 @@ use ffi; #[derive(Debug)] pub struct Threads<'q, 'd:'q>( *mut ffi::notmuch_threads_t, - marker::PhantomData<&'q Query<'d>>, + PhantomData<&'q Query<'d>>, ); impl<'q, 'd> NewFromPtr<*mut ffi::notmuch_threads_t> for Threads<'q, 'd> { fn new(ptr: *mut ffi::notmuch_threads_t) -> Threads<'q, 'd> { - Threads(ptr, marker::PhantomData) + Threads(ptr, PhantomData) } } -impl<'q, 'd> ops::Drop for Threads<'q, 'd> { +impl<'q, 'd> Drop for Threads<'q, 'd> { fn drop(&mut self) { unsafe { ffi::notmuch_threads_destroy(self.0) @@ -32,7 +27,7 @@ impl<'q, 'd> ops::Drop for Threads<'q, 'd> { } } -impl<'q, 'd> iter::Iterator for Threads<'q, 'd> { +impl<'q, 'd> Iterator for Threads<'q, 'd> { type Item = Thread<'q, 'd>; fn next(self: &mut Self) -> Option { diff --git a/src/utils.rs b/src/utils.rs index e1f691c..bdc8c4f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -2,7 +2,6 @@ use std::{ ffi, str, }; - use libc; pub trait NewFromPtr { -- cgit v1.2.1