diff options
| author | Dirk Van Haerenborgh <vhdirk@gmail.com> | 2018-03-23 04:02:26 +0100 |
|---|---|---|
| committer | Dirk Van Haerenborgh <vhdirk@gmail.com> | 2018-03-23 04:02:26 +0100 |
| commit | 4513eafb2017b93850153fa978497335df9845be (patch) | |
| tree | 039da7d9b8d4232b7f578bead971f65e1646d3bd | |
| parent | bc968e0da07e8e80e19eec1e8efbdb347746d6e2 (diff) | |
| download | mail-4513eafb2017b93850153fa978497335df9845be.tar.gz | |
message and thread iterators
| -rw-r--r-- | src/directory.rs | 4 | ||||
| -rw-r--r-- | src/lib.rs | 4 | ||||
| -rw-r--r-- | src/message.rs | 35 | ||||
| -rw-r--r-- | src/messages.rs | 39 | ||||
| -rw-r--r-- | src/tags.rs | 2 | ||||
| -rw-r--r-- | src/thread.rs | 34 | ||||
| -rw-r--r-- | src/threads.rs | 29 |
7 files changed, 131 insertions, 16 deletions
diff --git a/src/directory.rs b/src/directory.rs index 1d75f35..7b00d70 100644 --- a/src/directory.rs +++ b/src/directory.rs @@ -7,14 +7,14 @@ use utils::{ NewFromPtr, }; -use database; +use Database; use ffi; #[derive(Debug)] pub struct Directory<'d>( *mut ffi::notmuch_directory_t, - marker::PhantomData<&'d mut database::Database>, + marker::PhantomData<&'d mut Database>, ); impl<'d> NewFromPtr<*mut ffi::notmuch_directory_t> for Directory<'d> { @@ -11,13 +11,17 @@ pub mod database; pub mod directory; pub mod query; pub mod messages; +pub mod message; pub mod tags; pub mod threads; +pub mod thread; pub use database::Database; pub use query::Query; pub use messages::Messages; +pub use message::Message; pub use tags::Tags; pub use threads::Threads; +pub use thread::Thread; pub use ffi::DatabaseMode; diff --git a/src/message.rs b/src/message.rs index e69de29..d774043 100644 --- a/src/message.rs +++ b/src/message.rs @@ -0,0 +1,35 @@ +use std; +use std::{ + ops, + marker, + ptr, +}; + +use error::Result; + +use ffi; +use utils::{ + NewFromPtr, +}; +use Database; + +#[derive(Debug)] +pub struct Message<'d>( + pub(crate) *mut ffi::notmuch_message_t, + marker::PhantomData<&'d mut Database>, +); + +impl<'d> NewFromPtr<*mut ffi::notmuch_message_t> for Message<'d> { + fn new(ptr: *mut ffi::notmuch_message_t) -> Message<'d> { + Message(ptr, marker::PhantomData) + } +} + + +impl<'d> ops::Drop for Message<'d> { + fn drop(&mut self) { + unsafe { + ffi::notmuch_message_destroy(self.0) + }; + } +} diff --git a/src/messages.rs b/src/messages.rs index c21c701..706cfda 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -1,6 +1,7 @@ use std::{ ops, - marker + marker, + iter }; use error::Result; @@ -10,28 +11,48 @@ use utils::{ NewFromPtr, }; use Database; -use Query; +use Message; #[derive(Debug)] -pub struct Messages<'q, 'd:'q>( +pub struct Messages<'d>( // TODO: is this lifetime specifier correct? // query may outlive messages. pub(crate) *mut ffi::notmuch_messages_t, - marker::PhantomData<&'q Query<'d>> + marker::PhantomData<&'d mut Database>, ); -impl<'q, 'd:'q> NewFromPtr<*mut ffi::notmuch_messages_t> for Messages<'q, 'd> { - fn new(ptr: *mut ffi::notmuch_messages_t) -> Messages<'q, 'd> { +impl<'d> NewFromPtr<*mut ffi::notmuch_messages_t> for Messages<'d> { + fn new(ptr: *mut ffi::notmuch_messages_t) -> Messages<'d> { Messages(ptr, marker::PhantomData) } } - - -impl<'q, 'd:'q> ops::Drop for Messages<'q, 'd> { +impl<'d> ops::Drop for Messages<'d> { fn drop(&mut self) { unsafe { ffi::notmuch_messages_destroy(self.0) }; } } + +impl<'d> iter::Iterator for Messages<'d> { + type Item = Message<'d>; + + fn next(&mut self) -> Option<Self::Item> { + + let valid = unsafe { + ffi::notmuch_messages_valid(self.0) + }; + + if valid == 0{ + return None + } + + let cmsg = unsafe { + ffi::notmuch_messages_move_to_next(self.0); + ffi::notmuch_messages_get(self.0) + }; + + Some(Message::new(cmsg)) + } +} diff --git a/src/tags.rs b/src/tags.rs index 9eb2779..0ffb7ae 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -56,6 +56,4 @@ impl<'d> iter::Iterator for Tags<'d> { Some(ctag.to_str().unwrap().to_string()) } - - } diff --git a/src/thread.rs b/src/thread.rs index e69de29..980ff39 100644 --- a/src/thread.rs +++ b/src/thread.rs @@ -0,0 +1,34 @@ +use std; +use std::{ + ops, + marker, + ptr, +}; + +use error::Result; + +use ffi; +use utils::{ + NewFromPtr, +}; +use Database; + +#[derive(Debug)] +pub struct Thread<'d>( + pub(crate) *mut ffi::notmuch_thread_t, + marker::PhantomData<&'d mut Database>, +); + +impl<'d> NewFromPtr<*mut ffi::notmuch_thread_t> for Thread<'d> { + fn new(ptr: *mut ffi::notmuch_thread_t) -> Thread<'d> { + Thread(ptr, marker::PhantomData) + } +} + +impl<'d> ops::Drop for Thread<'d> { + fn drop(&mut self) { + unsafe { + ffi::notmuch_thread_destroy(self.0) + }; + } +} diff --git a/src/threads.rs b/src/threads.rs index 75f4549..5f62a8d 100644 --- a/src/threads.rs +++ b/src/threads.rs @@ -1,20 +1,21 @@ use std::{ ops, marker, + iter }; use utils::{ NewFromPtr, }; -use database; - +use Database; +use Thread; use ffi; #[derive(Debug)] pub struct Threads<'d>( *mut ffi::notmuch_threads_t, - marker::PhantomData<&'d mut database::Database>, + marker::PhantomData<&'d mut Database>, ); impl<'d> NewFromPtr<*mut ffi::notmuch_threads_t> for Threads<'d> { @@ -30,3 +31,25 @@ impl<'d> ops::Drop for Threads<'d> { }; } } + +impl<'d> iter::Iterator for Threads<'d> { + type Item = Thread<'d>; + + fn next(&mut self) -> Option<Self::Item> { + + let valid = unsafe { + ffi::notmuch_threads_valid(self.0) + }; + + if valid == 0{ + return None + } + + let cthread = unsafe { + ffi::notmuch_threads_move_to_next(self.0); + ffi::notmuch_threads_get(self.0) + }; + + Some(Thread::new(cthread)) + } +} |
