diff options
Diffstat (limited to 'src/config_list.rs')
| -rw-r--r-- | src/config_list.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/config_list.rs b/src/config_list.rs new file mode 100644 index 0000000..632603c --- /dev/null +++ b/src/config_list.rs @@ -0,0 +1,62 @@ +use std::ops::Drop; +use std::ffi::{CStr, CString}; +use supercow::Supercow; + +use ffi; +use Database; +use Filenames; +use FilenamesOwner; +use utils::{ScopedSupercow, 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 = CStr::from_ptr(ffi::notmuch_config_list_key(self.ptr)); + let value = CStr::from_ptr(ffi::notmuch_config_list_value(self.ptr)); + + ffi::notmuch_config_list_move_to_next(self.ptr); + + (key, value) + }; + + Some((k.to_str().unwrap().to_string(), v.to_str().unwrap().to_string())) + } +} + +unsafe impl<'d> Send for ConfigList<'d> {} +unsafe impl<'d> Sync for ConfigList<'d> {} |
