aboutsummaryrefslogtreecommitdiffstats
path: root/notmuch/src/config_list.rs
blob: 2c1a6a4d415586bd0972f3313d8f8960ef4c64f7 (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
use std::ops::Drop;

use ffi;
use Database;
use utils::{ToStr, ScopedPhantomcow};


#[derive(Debug)]
pub struct ConfigList<'d> {
    ptr: *mut ffi::notmuch_config_list_t,
    marker: ScopedPhantomcow<'d, Database>,
}

impl<'d> Drop for ConfigList<'d> {
    fn drop(&mut self) {
        unsafe { ffi::notmuch_config_list_destroy(self.ptr) };
    }
}

impl<'d> ConfigList<'d> {
    pub(crate) fn from_ptr<O>(ptr: *mut ffi::notmuch_config_list_t, owner: O) -> ConfigList<'d>
    where
        O: Into<ScopedPhantomcow<'d, Database>>,
    {
        ConfigList {
            ptr,
            marker: owner.into(),
        }
    }
}


impl<'d> Iterator for ConfigList<'d>
{
    type Item = (String, String);

    fn next(&mut self) -> Option<Self::Item> {
        let valid = unsafe { ffi::notmuch_config_list_valid(self.ptr) };

        if valid == 0 {
            return None;
        }

        let (k, v) = unsafe {
            let key = ffi::notmuch_config_list_key(self.ptr);
            let value = ffi::notmuch_config_list_value(self.ptr);

            ffi::notmuch_config_list_move_to_next(self.ptr);

            (key, value)
        };

        Some((k.to_string_lossy().to_string(), v.to_string_lossy().to_string()))
    }
}

unsafe impl<'d> Send for ConfigList<'d> {}
unsafe impl<'d> Sync for ConfigList<'d> {}