aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDirk Van Haerenborgh <vhdirk@gmail.com>2018-04-19 10:21:14 +0200
committerDirk Van Haerenborgh <vhdirk@gmail.com>2018-10-06 10:05:07 +0200
commitf021813ed443b4ef7224d1934881abecf53977a7 (patch)
treec82d534ec74a2fb2025f5815302e6005de78f566
parent1be0cc6ab7920208b3d6986dbb6076846d758c17 (diff)
downloadmail-f021813ed443b4ef7224d1934881abecf53977a7.tar.gz
cleanup & export db revision uuid
-rw-r--r--README.md4
-rw-r--r--src/database.rs58
-rw-r--r--src/directory.rs16
-rw-r--r--src/ffi.rs2
-rw-r--r--src/filenames.rs40
-rw-r--r--src/lib.rs1
-rw-r--r--src/message.rs17
-rw-r--r--src/messages.rs24
-rw-r--r--src/query.rs19
-rw-r--r--src/tags.rs23
-rw-r--r--src/thread.rs16
-rw-r--r--src/threads.rs21
-rw-r--r--src/utils.rs1
-rw-r--r--tests/main.rs3
14 files changed, 115 insertions, 130 deletions
diff --git a/README.md b/README.md
index a636d6c..4e1d86b 100644
--- a/README.md
+++ b/README.md
@@ -66,9 +66,7 @@ that must outlive any related objects, for instance ```notmuch::Query```. The
```notmuch::Threads``` iterator that you can get from a ```notmuch::Query``` is
always outlived by the parent query.
This means that you can only use these structs accross thread bounds if you
-figure out how to satisfy the lifetime requirements. Up until now, I haven't
-been able to do that (though my knowledge of Rust is still rather basic).
-So, concurrency seems currently limited to scoped threads.
+figure out how to satisfy the lifetime requirements.
## Acknowledgements
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<P: AsRef<path::Path>>(path: &P) -> Result<Self> {
+ pub fn create<P: AsRef<Path>>(path: &P) -> Result<Self> {
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<P: AsRef<path::Path>>(path: &P, mode: DatabaseMode) -> Result<Self> {
+ pub fn open<P: AsRef<Path>>(path: &P, mode: DatabaseMode) -> Result<Self> {
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<P: AsRef<path::Path>, F: FnMut(&str)>(
+ pub fn compact<P: AsRef<Path>, F: FnMut(&str)>(
path: &P, backup_path: Option<&P>,
) -> Result<()> {
let status: Option<F> = None;
Database::_compact(path, backup_path, status)
}
- pub fn compact_with_status<P: AsRef<path::Path>, F: FnMut(&str)>(
+ pub fn compact_with_status<P: AsRef<Path>, F: FnMut(&str)>(
path: &P, backup_path: Option<&P>, status: F,
) -> Result<()> {
Database::_compact(path, backup_path, Some(status))
}
- fn _compact<P: AsRef<path::Path>, F: FnMut(&str)>(
+ fn _compact<P: AsRef<Path>, F: FnMut(&str)>(
path: &P, backup_path: Option<&P>, status: Option<F>,
) -> 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<path::Path>>(&'d self, path: &P) -> Result<Option<Directory<'d>>> {
+ pub fn directory<'d, P: AsRef<Path>>(&'d self, path: &P) -> Result<Option<Directory<'d>>> {
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<Query<'d>> {
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<Self::Item> {
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<Self::Item> {
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<Self::Item> {
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<Self::Item> {
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<T> {
diff --git a/tests/main.rs b/tests/main.rs
index b92bcf7..ca31225 100644
--- a/tests/main.rs
+++ b/tests/main.rs
@@ -7,6 +7,9 @@ fn main() {
match notmuch::Database::open(&mail_path.to_str().unwrap().to_string(), notmuch::DatabaseMode::ReadOnly){
Ok(db) => {
+ let rev = db.revision();
+ println!("db revision: {:?}", rev);
+
let query = db.create_query(&"".to_string()).unwrap();
let mut threads = query.search_threads().unwrap();