aboutsummaryrefslogtreecommitdiffstats
path: root/src/messages.rs
blob: 16cf74155992261eba9ae529022a87b39636e875 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use std::{
    ops,
    marker,
    iter
};

use error::Result;

use ffi;
use utils::{
    NewFromPtr,
};
use Query;
use Message;
use Tags;

#[derive(Debug)]
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 mut 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)
    }
}

impl<'q, 'd> Messages<'q, 'd>{

    pub fn collect_tags(self: &Self) -> Tags{
        Tags::new(unsafe {
            ffi::notmuch_messages_collect_tags(self.0)
        })
    }

}

impl<'q, 'd> ops::Drop for Messages<'q, 'd> {
    fn drop(&mut self) {
        unsafe {
            ffi::notmuch_messages_destroy(self.0)
        };
    }
}

impl<'q, 'd> iter::Iterator for Messages<'q, 'd> {
    type Item = Message<'q, '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 {
            let msg = ffi::notmuch_messages_get(self.0);
            ffi::notmuch_messages_move_to_next(self.0);
            msg
        };

        Some(Self::Item::new(cmsg))
    }
}