diff options
| author | Dirk Van Haerenborgh <vhdirk@gmail.com> | 2018-10-16 21:01:58 +0200 |
|---|---|---|
| committer | Dirk Van Haerenborgh <vhdirk@gmail.com> | 2018-10-24 07:12:55 +0200 |
| commit | a4ffe47c51d1617fc0e728c7bbd7e9b3738878cb (patch) | |
| tree | 017d0717ac8e2d5e0f6a46e47ea9e731ce63697b /src/messages.rs | |
| parent | b93d4cb749714699b44b2566e50a7086c2854ac7 (diff) | |
| download | mail-a4ffe47c51d1617fc0e728c7bbd7e9b3738878cb.tar.gz | |
some tries towards better lifetimes
Diffstat (limited to 'src/messages.rs')
| -rw-r--r-- | src/messages.rs | 80 |
1 files changed, 51 insertions, 29 deletions
diff --git a/src/messages.rs b/src/messages.rs index 65e1248..4bc90e9 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -4,40 +4,22 @@ use std::marker::PhantomData; use ffi; use utils::{ - NewFromPtr, + FromPtr, }; use Query; use Message; use Tags; -#[derive(Debug)] -pub struct Messages<'d:'q, 'q>( - // TODO: is this lifetime specifier correct? - // query may outlive messages. - pub(crate) *mut ffi::notmuch_messages_t, - PhantomData<&'q Query<'d>>, -); - -impl<'d, 'q> NewFromPtr<*mut ffi::notmuch_messages_t> for Messages<'d, 'q> { - fn new(ptr: *mut ffi::notmuch_messages_t) -> Messages<'d, 'q> { - Messages(ptr, PhantomData) - } -} - -impl<'d, 'q> Messages<'d, 'q>{ - - pub fn collect_tags(self: &'d Self) -> Tags<'d>{ - Tags::new(unsafe { - ffi::notmuch_messages_collect_tags(self.0) - }) - } +#[derive(Debug)] +pub(crate) struct MessagesPtr { + pub ptr: *mut ffi::notmuch_messages_t } -impl<'d, 'q> Drop for Messages<'d, 'q> { +impl Drop for MessagesPtr { fn drop(self: &mut Self) { let valid = unsafe { - ffi::notmuch_messages_valid(self.0) + ffi::notmuch_messages_valid(self.ptr) }; if valid == 0{ @@ -45,18 +27,58 @@ impl<'d, 'q> Drop for Messages<'d, 'q> { } unsafe { - ffi::notmuch_messages_destroy(self.0) + ffi::notmuch_messages_destroy(self.ptr) }; } } + +#[derive(Debug)] +pub struct Messages<'d:'q, 'q>{ + pub(crate) handle: MessagesPtr, + phantom: PhantomData<&'q Query<'d>>, +} + +impl<'d, 'q> FromPtr<*mut ffi::notmuch_messages_t> for Messages<'d, 'q> { + fn from_ptr(ptr: *mut ffi::notmuch_messages_t) -> Messages<'d, 'q> { + Messages{ + handle: MessagesPtr{ptr}, + phantom: PhantomData + } + } +} + +impl<'d, 'q> Messages<'d, 'q>{ + + /** + * Return a list of tags from all messages. + * + * The resulting list is guaranteed not to contain duplicated tags. + * + * WARNING: You can no longer iterate over messages after calling this + * function, because the iterator will point at the end of the list. + * We do not have a function to reset the iterator yet and the only + * way how you can iterate over the list again is to recreate the + * message list. + * + * The function returns NULL on error. + */ + pub fn collect_tags(self: &'d Self) -> Tags{ + Tags::from_ptr(unsafe { + ffi::notmuch_messages_collect_tags(self.handle.ptr) + }) + } +} + + + impl<'d, 'q> Iterator for Messages<'d, 'q> { type Item = Message<'d, 'q>; fn next(&mut self) -> Option<Self::Item> { let valid = unsafe { - ffi::notmuch_messages_valid(self.0) + ffi::notmuch_messages_valid(self.handle.ptr) }; if valid == 0{ @@ -64,12 +86,12 @@ impl<'d, 'q> Iterator for Messages<'d, 'q> { } let cmsg = unsafe { - let msg = ffi::notmuch_messages_get(self.0); - ffi::notmuch_messages_move_to_next(self.0); + let msg = ffi::notmuch_messages_get(self.handle.ptr); + ffi::notmuch_messages_move_to_next(self.handle.ptr); msg }; - Some(Self::Item::new(cmsg)) + Some(Self::Item::from_ptr(cmsg)) } } |
