From 942740b143c8f07f402da8e20338c8f769fe5447 Mon Sep 17 00:00:00 2001 From: Dirk Van Haerenborgh Date: Mon, 5 Nov 2018 08:15:27 +0100 Subject: implement 'Ext' traits for all types exept iterators --- src/database.rs | 56 +++++++++++++++++++++++++++++++++++++++----------------- src/directory.rs | 36 ++++++++++-------------------------- src/message.rs | 31 +++++++++++++++++-------------- src/messages.rs | 9 +++++++++ src/query.rs | 20 ++++++++++++++------ src/tags.rs | 4 ++++ src/thread.rs | 46 +++++++++++++++++++++++++++++++++------------- src/threads.rs | 4 ++++ 8 files changed, 130 insertions(+), 76 deletions(-) (limited to 'src') 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>(&'d self, path: &P) -> Result>> { + ::directory(self, path) + } + + pub fn create_query<'d>(&'d self, query_string: &str) -> Result> { + ::create_query(self, query_string) + } + + pub fn all_tags<'d>(&'d self) -> Result> { + ::all_tags(self) + } +} + +pub trait DatabaseExt{ + fn create_query<'d, D: Into>>(database: D, query_string: &str) -> Result> { + 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>>(database: D) -> Result> { + 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>, P: AsRef>(database: D, path: &P) -> Result>> { + 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> { - self.handle.create_query(query_string).map(move |handle|{ - Query::from_handle(handle, self) - }) - } - - pub fn all_tags<'d>(&'d self) -> Result> { - 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>, - P: AsRef>(owner: O, path: &P) -> Result>> { - 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 { + ::child_directories(self) } +} - - pub fn child_directories(&self) -> Filenames { +pub trait DirectoryExt<'d>{ + fn child_directories<'s, S: Into>>>(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 { - Messages::from_ptr( - unsafe { ffi::notmuch_message_get_replies(self.handle.ptr) }, - self, - ) + >::replies(self) } #[cfg(feature = "v0_26")] @@ -71,10 +68,7 @@ impl<'o, Owner: MessageOwner + 'o> Message<'o, Owner> { } pub fn filenames(self: &Self) -> Filenames { - Filenames::from_ptr( - unsafe { ffi::notmuch_message_get_filenames(self.handle.ptr) }, - self, - ) + >::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 { - Tags::from_ptr( - unsafe { ffi::notmuch_message_get_tags(self.handle.ptr) }, - self, - ) + >::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>>>(message: M) -> Messages<'s, Message<'o, Owner>> { + fn tags<'s, S: Into>>>(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>>>(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>>>(message: M) -> Filenames<'s, Message<'o, Owner>> { + fn filenames<'s, S: Into>>>(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>>( + ptr: *mut ffi::notmuch_query_t, + owner: O, + ) -> Query<'d> { + Query { + handle: QueryPtr{ptr}, + marker: owner.into(), + } + } + pub(crate) fn from_handle>>( handle: QueryPtr, owner: O, @@ -80,7 +91,6 @@ impl<'d> Query<'d> { pub fn search_threads<'q>(self: &'d Self) -> Result> { ::search_threads(self) - } pub fn count_threads(self: &Self) -> Result { @@ -92,11 +102,7 @@ impl<'d> Query<'d> { } pub trait QueryExt<'d>{ - fn search_threads<'q, Q: Into>>>(query: Q) -> Result>>; - fn search_messages<'q, Q: Into>>>(query: Q) -> Result>>; -} - -impl<'d> QueryExt<'d> for Query<'d>{ + fn search_threads<'q, Q: Into>>>(query: Q) -> Result>>{ 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 { - Messages::from_ptr( - unsafe { ffi::notmuch_thread_get_toplevel_messages(self.handle.ptr) }, - self, - ) + >::toplevel_messages(self) } /// Get a `Messages` iterator for all messages in 'thread' in /// oldest-first order. pub fn messages(self: &Self) -> Messages { - Messages::from_ptr( - unsafe { ffi::notmuch_thread_get_messages(self.handle.ptr) }, - self, - ) + >::messages(self) } pub fn tags(&self) -> Tags { - Tags::from_ptr( - unsafe { ffi::notmuch_thread_get_tags(self.handle.ptr) }, - self, - ) + >::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>>>(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>>>(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>>>(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> {} -- cgit v1.2.1