diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-04-25 16:58:17 +0200 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-04-25 16:58:17 +0200 |
| commit | 72c2b83748a601d02c82d5bcb4220d3b238281cc (patch) | |
| tree | 38acb17db4a48fc9f340680ea5b86c6cd49d8f2a /notmuch/src/filenames.rs | |
| parent | 51fa75397dda2c280f29760e7b525caefc03642e (diff) | |
| parent | 2231a5cf6cdeb90c1cdb75d161a0063d4a385576 (diff) | |
| download | mail-72c2b83748a601d02c82d5bcb4220d3b238281cc.tar.gz | |
Add 'notmuch/' from commit '2231a5cf6cdeb90c1cdb75d161a0063d4a385576'
git-subtree-dir: notmuch
git-subtree-mainline: 51fa75397dda2c280f29760e7b525caefc03642e
git-subtree-split: 2231a5cf6cdeb90c1cdb75d161a0063d4a385576
Diffstat (limited to 'notmuch/src/filenames.rs')
| -rw-r--r-- | notmuch/src/filenames.rs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/notmuch/src/filenames.rs b/notmuch/src/filenames.rs new file mode 100644 index 0000000..12e5857 --- /dev/null +++ b/notmuch/src/filenames.rs @@ -0,0 +1,68 @@ +use std::ffi::CStr; +use std::iter::Iterator; +use std::ops::Drop; +use std::path::PathBuf; + +use ffi; +use utils::ScopedPhantomcow; + +pub trait FilenamesOwner {} + +#[derive(Debug)] +pub struct Filenames<'o, O> +where + O: FilenamesOwner + 'o, +{ + pub(crate) ptr: *mut ffi::notmuch_filenames_t, + pub(crate) marker: ScopedPhantomcow<'o, O>, +} + +impl<'o, O> Drop for Filenames<'o, O> +where + O: FilenamesOwner + 'o, +{ + fn drop(self: &mut Self) { + unsafe { ffi::notmuch_filenames_destroy(self.ptr) }; + } +} + +impl<'o, O> Filenames<'o, O> +where + O: FilenamesOwner + 'o, +{ + pub(crate) fn from_ptr<P>(ptr: *mut ffi::notmuch_filenames_t, owner: P) -> Filenames<'o, O> + where + P: Into<ScopedPhantomcow<'o, O>>, + { + Filenames { + ptr, + marker: owner.into(), + } + } +} + +impl<'o, O> Iterator for Filenames<'o, O> +where + O: FilenamesOwner + 'o, +{ + type Item = PathBuf; + + fn next(self: &mut Self) -> Option<Self::Item> { + let valid = unsafe { ffi::notmuch_filenames_valid(self.ptr) }; + + if valid == 0 { + return None; + } + + let ctag = unsafe { + let t = ffi::notmuch_filenames_get(self.ptr); + ffi::notmuch_filenames_move_to_next(self.ptr); + CStr::from_ptr(t) + }; + + Some(PathBuf::from(ctag.to_str().unwrap())) + } +} + +unsafe impl<'o, O> Send for Filenames<'o, O> where O: FilenamesOwner + 'o {} +unsafe impl<'o, O> Sync for Filenames<'o, O> where O: FilenamesOwner + 'o {} |
