aboutsummaryrefslogtreecommitdiffstats
path: root/src/threads.rs
blob: 9a0117f6e1f73d7f83ca44955b73ef7eb2d82d8a (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
use std::ops::Drop;
use std::iter::Iterator;
use std::marker::PhantomData;

use utils::NewFromPtr;
use Query;
use Thread;
use ffi;

#[derive(Debug)]
pub struct Threads<'q, 'd:'q>(
    *mut ffi::notmuch_threads_t,
    PhantomData<&'q Query<'d>>,
);

impl<'q, 'd> NewFromPtr<*mut ffi::notmuch_threads_t> for Threads<'q, 'd> {
    fn new(ptr: *mut ffi::notmuch_threads_t) -> Threads<'q, 'd> {
        Threads(ptr, PhantomData)
    }
}

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

impl<'q, 'd> Iterator for Threads<'q, 'd> {
    type Item = Thread<'q, 'd>;

    fn next(self: &mut Self) -> Option<Self::Item> {

        let valid = unsafe {
            ffi::notmuch_threads_valid(self.0)
        };

        if valid == 0{
            return None;
        }

        let cthread = unsafe {
            let t = ffi::notmuch_threads_get(self.0);
            ffi::notmuch_threads_move_to_next(self.0);
            t
        };

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

unsafe impl<'q, 'd> Send for Threads<'q, 'd> {}
unsafe impl<'q, 'd> Sync for Threads<'q, 'd> {}