diff options
| author | Dirk Van Haerenborgh <vhdirk@gmail.com> | 2018-11-05 08:15:27 +0100 |
|---|---|---|
| committer | Dirk Van Haerenborgh <vhdirk@gmail.com> | 2018-11-05 08:15:27 +0100 |
| commit | 942740b143c8f07f402da8e20338c8f769fe5447 (patch) | |
| tree | 738254b10ded304175c8e5b94f25c28caca303aa | |
| parent | 228e7665bc4dc20929ea2a8cb52600da3d4dd839 (diff) | |
| download | mail-942740b143c8f07f402da8e20338c8f769fe5447.tar.gz | |
implement 'Ext' traits for all types exept iterators
| -rw-r--r-- | src/database.rs | 56 | ||||
| -rw-r--r-- | src/directory.rs | 36 | ||||
| -rw-r--r-- | src/message.rs | 31 | ||||
| -rw-r--r-- | src/messages.rs | 9 | ||||
| -rw-r--r-- | src/query.rs | 20 | ||||
| -rw-r--r-- | src/tags.rs | 4 | ||||
| -rw-r--r-- | src/thread.rs | 46 | ||||
| -rw-r--r-- | src/threads.rs | 4 |
8 files changed, 130 insertions, 76 deletions
diff --git a/src/database.rs b/src/database.rs index 8a125b4..b73c5c2 100644 --- a/src/database.rs +++ b/src/database.rs @@ -3,6 +3,8 @@ use std::ops::Drop; use std::path::Path; use std::ptr; +use supercow::Supercow; + use libc; use error::Result; @@ -208,12 +210,46 @@ impl Database { } pub fn directory<'d, P: AsRef<Path>>(&'d self, path: &P) -> Result<Option<Directory<'d>>> { + <Self as DatabaseExt>::directory(self, path) + } + + pub fn create_query<'d>(&'d self, query_string: &str) -> Result<Query<'d>> { + <Self as DatabaseExt>::create_query(self, query_string) + } + + pub fn all_tags<'d>(&'d self) -> Result<Tags<'d, Self>> { + <Self as DatabaseExt>::all_tags(self) + } +} + +pub trait DatabaseExt{ + fn create_query<'d, D: Into<Supercow<'d, Database>>>(database: D, query_string: &str) -> Result<Query<'d>> { + let dbref = database.into(); + let query_str = CString::new(query_string).unwrap(); + + let query = unsafe { ffi::notmuch_query_create(dbref.handle.ptr, query_str.as_ptr()) }; + + Ok(Query::from_ptr(query, Supercow::phantom(dbref))) + } + + fn all_tags<'d, D: Into<Supercow<'d, Database>>>(database: D) -> Result<Tags<'d, Database>> { + let dbref = database.into(); + + let tags = unsafe { ffi::notmuch_database_get_all_tags(dbref.handle.ptr) }; + + Ok(Tags::from_ptr(tags, Supercow::phantom(dbref))) + } + + + fn directory<'d, D: Into<Supercow<'d, Database>>, P: AsRef<Path>>(database: D, path: &P) -> Result<Option<Directory<'d>>> { + let dbref = database.into(); + let path_str = CString::new(path.as_ref().to_str().unwrap()).unwrap(); let mut dir = ptr::null_mut(); try!( unsafe { - ffi::notmuch_database_get_directory(self.handle.ptr, path_str.as_ptr(), &mut dir) + ffi::notmuch_database_get_directory(dbref.handle.ptr, path_str.as_ptr(), &mut dir) } .as_result() ); @@ -221,26 +257,12 @@ impl Database { if dir.is_null() { Ok(None) } else { - Ok(Some(Directory::from_ptr(dir, self))) + Ok(Some(Directory::from_ptr(dir, Supercow::phantom(dbref)))) } } - - pub fn create_query<'d>(&'d self, query_string: &str) -> Result<Query<'d>> { - self.handle.create_query(query_string).map(move |handle|{ - Query::from_handle(handle, self) - }) - } - - pub fn all_tags<'d>(&'d self) -> Result<Tags<'d, Self>> { - let tags = unsafe { ffi::notmuch_database_get_all_tags(self.handle.ptr) }; - - Ok(Tags::from_ptr(tags, self)) - } } -pub trait DatabaseExt{ - -} +impl DatabaseExt for Database{} unsafe impl Send for Database {} diff --git a/src/directory.rs b/src/directory.rs index 8ddc044..0aa5b80 100644 --- a/src/directory.rs +++ b/src/directory.rs @@ -40,41 +40,25 @@ impl<'d> Directory<'d> { } } - pub fn new<O: Into<Supercow<'d, Database>>, - P: AsRef<Path>>(owner: O, path: &P) -> Result<Option<Directory<'d>>> { - let db = owner.into(); - let path_str = CString::new(path.as_ref().to_str().unwrap()).unwrap(); - - let mut dir = ptr::null_mut(); - try!( - unsafe { - ffi::notmuch_database_get_directory(db.handle.ptr, path_str.as_ptr(), &mut dir) - } - .as_result() - ); - - if dir.is_null() { - Ok(None) - } else { - Ok(Some(Directory { - handle: DirectoryPtr { ptr: dir }, - marker: Supercow::phantom(db), - })) - } + pub fn child_directories(&self) -> Filenames<Self> { + <Self as DirectoryExt>::child_directories(self) } +} - - pub fn child_directories(&self) -> Filenames<Self> { +pub trait DirectoryExt<'d>{ + fn child_directories<'s, S: Into<Supercow<'s, Directory<'d>>>>(directory: S) -> Filenames<'s, Directory<'d>> { + let dir = directory.into(); Filenames::from_ptr( - unsafe { ffi::notmuch_directory_get_child_directories(self.handle.ptr) }, - self, + unsafe { ffi::notmuch_directory_get_child_directories(dir.handle.ptr) }, + Supercow::phantom(dir), ) } } -pub trait DirectoryExt<'d>{ +impl<'d> DirectoryExt<'d> for Directory<'d>{ } + unsafe impl<'d> Send for Directory<'d> {} unsafe impl<'d> Sync for Directory<'d> {} diff --git a/src/message.rs b/src/message.rs index f825e03..4c35544 100644 --- a/src/message.rs +++ b/src/message.rs @@ -59,10 +59,7 @@ impl<'o, Owner: MessageOwner + 'o> Message<'o, Owner> { } pub fn replies(self: &Self) -> Messages<Self> { - Messages::from_ptr( - unsafe { ffi::notmuch_message_get_replies(self.handle.ptr) }, - self, - ) + <Self as MessageExt<'o, Owner>>::replies(self) } #[cfg(feature = "v0_26")] @@ -71,10 +68,7 @@ impl<'o, Owner: MessageOwner + 'o> Message<'o, Owner> { } pub fn filenames(self: &Self) -> Filenames<Self> { - Filenames::from_ptr( - unsafe { ffi::notmuch_message_get_filenames(self.handle.ptr) }, - self, - ) + <Self as MessageExt<'o, Owner>>::filenames(self) } pub fn filename(self: &Self) -> PathBuf { @@ -104,10 +98,7 @@ impl<'o, Owner: MessageOwner + 'o> Message<'o, Owner> { } pub fn tags(&self) -> Tags<Self> { - Tags::from_ptr( - unsafe { ffi::notmuch_message_get_tags(self.handle.ptr) }, - self, - ) + <Self as MessageExt<'o, Owner>>::tags(self) } pub fn add_tag(self: &Self, tag: &str) -> Status { @@ -134,7 +125,15 @@ impl<'o, Owner: MessageOwner + 'o> Message<'o, Owner> { pub trait MessageExt<'o, Owner: MessageOwner + 'o>{ - fn replies<'s, M: Into<Supercow<'s, Message<'o, Owner>>>>(message: M) -> Messages<'s, Message<'o, Owner>> { + fn tags<'s, S: Into<Supercow<'s, Message<'o, Owner>>>>(message: S) -> Tags<'s, Message<'o, Owner>> { + let messageref = message.into(); + Tags::from_ptr( + unsafe { ffi::notmuch_message_get_tags(messageref.handle.ptr) }, + Supercow::phantom(messageref) + ) + } + + fn replies<'s, S: Into<Supercow<'s, Message<'o, Owner>>>>(message: S) -> Messages<'s, Message<'o, Owner>> { let messageref = message.into(); Messages::from_ptr( unsafe { ffi::notmuch_message_get_replies(messageref.handle.ptr) }, @@ -142,7 +141,7 @@ pub trait MessageExt<'o, Owner: MessageOwner + 'o>{ ) } - fn filenames<'s, M: Into<Supercow<'s, Message<'o, Owner>>>>(message: M) -> Filenames<'s, Message<'o, Owner>> { + fn filenames<'s, S: Into<Supercow<'s, Message<'o, Owner>>>>(message: S) -> Filenames<'s, Message<'o, Owner>> { let messageref = message.into(); Filenames::from_ptr( unsafe { ffi::notmuch_message_get_filenames(messageref.handle.ptr) }, @@ -151,5 +150,9 @@ pub trait MessageExt<'o, Owner: MessageOwner + 'o>{ } } +impl<'o, Owner: MessageOwner + 'o> MessageExt<'o, Owner> for Message<'o, Owner>{ + +} + unsafe impl<'o, Owner: MessageOwner + 'o> Send for Message<'o, Owner> {} unsafe impl<'o, Owner: MessageOwner + 'o> Sync for Message<'o, Owner> {} diff --git a/src/messages.rs b/src/messages.rs index 8863bef..7aa1c45 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -56,6 +56,15 @@ impl<'o, Owner: MessagesOwner + 'o> Messages<'o, Owner> { } } + +pub trait MessagesExt<'o, Owner: MessagesOwner + 'o>{ + +} + +impl<'o, Owner: MessagesOwner + 'o> MessagesExt<'o, Owner> for Messages<'o, Owner>{ + +} + impl<'o, Owner: MessagesOwner + 'o> MessageOwner for Messages<'o, Owner> {} impl<'o, Owner: MessagesOwner + 'o> TagsOwner for Messages<'o, Owner> {} diff --git a/src/query.rs b/src/query.rs index 4839f3b..b502542 100644 --- a/src/query.rs +++ b/src/query.rs @@ -36,6 +36,17 @@ impl<'d> ThreadsOwner for Query<'d> {} impl<'d> MessagesOwner for Query<'d> {} impl<'d> Query<'d> { + + pub(crate) fn from_ptr<O: Into<Phantomcow<'d, Database>>>( + ptr: *mut ffi::notmuch_query_t, + owner: O, + ) -> Query<'d> { + Query { + handle: QueryPtr{ptr}, + marker: owner.into(), + } + } + pub(crate) fn from_handle<O: Into<Phantomcow<'d, Database>>>( handle: QueryPtr, owner: O, @@ -80,7 +91,6 @@ impl<'d> Query<'d> { pub fn search_threads<'q>(self: &'d Self) -> Result<Threads<'q, Self>> { <Query as QueryExt>::search_threads(self) - } pub fn count_threads(self: &Self) -> Result<u32> { @@ -92,11 +102,7 @@ impl<'d> Query<'d> { } pub trait QueryExt<'d>{ - fn search_threads<'q, Q: Into<Supercow<'q, Query<'d>>>>(query: Q) -> Result<Threads<'q, Query<'d>>>; - fn search_messages<'q, Q: Into<Supercow<'q, Query<'d>>>>(query: Q) -> Result<Messages<'q, Query<'d>>>; -} - -impl<'d> QueryExt<'d> for Query<'d>{ + fn search_threads<'q, Q: Into<Supercow<'q, Query<'d>>>>(query: Q) -> Result<Threads<'q, Query<'d>>>{ let queryref = query.into(); @@ -121,6 +127,8 @@ impl<'d> QueryExt<'d> for Query<'d>{ } +impl<'d> QueryExt<'d> for Query<'d>{} + unsafe impl<'d> Send for Query<'d> {} unsafe impl<'d> Sync for Query<'d> {} diff --git a/src/tags.rs b/src/tags.rs index 243cfbc..dcf205a 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -63,5 +63,9 @@ pub trait TagsExt<'o, Owner: TagsOwner + 'o>{ } +impl<'o, Owner: TagsOwner + 'o> TagsExt<'o, Owner> for Tags<'o, Owner>{ + +} + unsafe impl<'o, Owner: TagsOwner + 'o> Send for Tags<'o, Owner> {} unsafe impl<'o, Owner: TagsOwner + 'o> Sync for Tags<'o, Owner> {} diff --git a/src/thread.rs b/src/thread.rs index 36da638..e9ecdad 100644 --- a/src/thread.rs +++ b/src/thread.rs @@ -1,5 +1,5 @@ use std::ops::Drop; -use supercow::Phantomcow; +use supercow::{Supercow, Phantomcow}; use ffi; use utils::ToStr; @@ -56,26 +56,17 @@ impl<'o, Owner: ThreadOwner + 'o> Thread<'o, Owner> { } pub fn toplevel_messages(self: &Self) -> Messages<Self> { - Messages::from_ptr( - unsafe { ffi::notmuch_thread_get_toplevel_messages(self.handle.ptr) }, - self, - ) + <Self as ThreadExt<'o, Owner>>::toplevel_messages(self) } /// Get a `Messages` iterator for all messages in 'thread' in /// oldest-first order. pub fn messages(self: &Self) -> Messages<Self> { - Messages::from_ptr( - unsafe { ffi::notmuch_thread_get_messages(self.handle.ptr) }, - self, - ) + <Self as ThreadExt<'o, Owner>>::messages(self) } pub fn tags(&self) -> Tags<Self> { - Tags::from_ptr( - unsafe { ffi::notmuch_thread_get_tags(self.handle.ptr) }, - self, - ) + <Self as ThreadExt<'o, Owner>>::tags(self) } pub fn subject(self: &Self) -> String { @@ -107,7 +98,36 @@ impl<'o, Owner: ThreadOwner + 'o> Thread<'o, Owner> { } pub trait ThreadExt<'o, Owner: ThreadOwner + 'o>{ + fn tags<'s, S: Into<Supercow<'s, Thread<'o, Owner>>>>(thread: S) -> Tags<'s, Thread<'o, Owner>> { + let threadref = thread.into(); + Tags::from_ptr( + unsafe { ffi::notmuch_thread_get_tags(threadref.handle.ptr) }, + Supercow::phantom(threadref) + ) + } + + fn toplevel_messages<'s, S: Into<Supercow<'s, Thread<'o, Owner>>>>(thread: S) -> Messages<'s, Thread<'o, Owner>> { + let threadref = thread.into(); + Messages::from_ptr( + unsafe { ffi::notmuch_thread_get_toplevel_messages(threadref.handle.ptr) }, + Supercow::phantom(threadref) + ) + } + + /// Get a `Messages` iterator for all messages in 'thread' in + /// oldest-first order. + fn messages<'s, S: Into<Supercow<'s, Thread<'o, Owner>>>>(thread: S) -> Messages<'s, Thread<'o, Owner>> { + let threadref = thread.into(); + Messages::from_ptr( + unsafe { ffi::notmuch_thread_get_messages(threadref.handle.ptr) }, + Supercow::phantom(threadref) + ) + } + +} +impl<'o, Owner: ThreadOwner + 'o> ThreadExt<'o, Owner> for Thread<'o, Owner>{ + } unsafe impl<'o, Owner: ThreadOwner + 'o> Send for Thread<'o, Owner> {} diff --git a/src/threads.rs b/src/threads.rs index bf869ab..54b49a9 100644 --- a/src/threads.rs +++ b/src/threads.rs @@ -73,5 +73,9 @@ pub trait ThreadsExt<'o, Owner: ThreadsOwner + 'o>{ } +impl<'o, Owner: ThreadsOwner + 'o> ThreadsExt<'o, Owner> for Threads<'o, Owner>{ + +} + unsafe impl<'o, Owner: ThreadsOwner + 'o> Send for Threads<'o, Owner> {} unsafe impl<'o, Owner: ThreadsOwner + 'o> Sync for Threads<'o, Owner> {} |
