aboutsummaryrefslogtreecommitdiffstats
path: root/src/query.rs
diff options
context:
space:
mode:
authorDirk Van Haerenborgh <vhdirk@gmail.com>2018-11-01 21:55:00 +0100
committerDirk Van Haerenborgh <vhdirk@gmail.com>2018-11-01 21:55:00 +0100
commit7d2be237297c16628cb3d58774e808cac9c92fc1 (patch)
tree55cb240828bac9968f94166e740b567b66928e3b /src/query.rs
parent2932d67d87fa2ff41fcdf46ce269ba5b49294930 (diff)
downloadmail-7d2be237297c16628cb3d58774e808cac9c92fc1.tar.gz
improve lifetime management with supercow
Diffstat (limited to 'src/query.rs')
-rw-r--r--src/query.rs105
1 files changed, 36 insertions, 69 deletions
diff --git a/src/query.rs b/src/query.rs
index a95c9c2..a90a0fe 100644
--- a/src/query.rs
+++ b/src/query.rs
@@ -1,128 +1,95 @@
use std::ops::Drop;
use std::ptr;
-use std::marker::PhantomData;
+use supercow::Phantomcow;
use error::Result;
-
use ffi;
-use utils::FromPtr;
-
+use ffi::Sort;
use Database;
use Messages;
+use MessagesOwner;
use Threads;
-use ffi::Sort;
-use threads::ThreadsOwner;
-use messages::MessagesOwner;
+use ThreadsOwner;
#[derive(Debug)]
pub(crate) struct QueryPtr {
- pub ptr: *mut ffi::notmuch_query_t
+ pub ptr: *mut ffi::notmuch_query_t,
}
impl Drop for QueryPtr {
fn drop(&mut self) {
- unsafe {
- ffi::notmuch_query_destroy(self.ptr)
- };
+ unsafe { ffi::notmuch_query_destroy(self.ptr) };
}
}
#[derive(Debug)]
-pub struct Query<'d>{
+pub struct Query<'d> {
pub(crate) handle: QueryPtr,
- phantom: PhantomData<&'d Database>,
+ marker: Phantomcow<'d, Database>,
}
-impl<'d> ThreadsOwner for Query<'d>{}
-impl<'d> MessagesOwner for Query<'d>{}
-
+impl<'d> ThreadsOwner for Query<'d> {}
+impl<'d> MessagesOwner for Query<'d> {}
-impl<'d> FromPtr<*mut ffi::notmuch_query_t> for Query<'d> {
- fn from_ptr(ptr: *mut ffi::notmuch_query_t) -> Query<'d> {
- Query{
- handle: QueryPtr{ptr},
- phantom: PhantomData
+impl<'d> Query<'d> {
+ pub fn from_ptr<O: Into<Phantomcow<'d, Database>>>(
+ ptr: *mut ffi::notmuch_query_t,
+ owner: O,
+ ) -> Query<'d> {
+ Query {
+ handle: QueryPtr { ptr },
+ marker: owner.into(),
}
}
-}
-impl<'d> Query<'d> {
pub fn create(db: &'d Database, query_string: &str) -> Result<Self> {
db.create_query(query_string)
}
/// Specify the sorting desired for this query.
- pub fn set_sort(self: &Self, sort: Sort)
- {
- unsafe {
- ffi::notmuch_query_set_sort(
- self.handle.ptr, sort.into(),
- )
- }
+ pub fn set_sort(self: &Self, sort: Sort) {
+ unsafe { ffi::notmuch_query_set_sort(self.handle.ptr, sort.into()) }
}
/// Return the sort specified for this query. See
/// `set_sort`.
- pub fn sort(self: &Self) -> Sort
- {
- unsafe {
- ffi::notmuch_query_get_sort(
- self.handle.ptr,
- )
- }.into()
+ pub fn sort(self: &Self) -> Sort {
+ unsafe { ffi::notmuch_query_get_sort(self.handle.ptr) }.into()
}
-
/// Filter messages according to the query and return
- pub fn search_messages<'q>(self: &'d Self) -> Result<Messages<'q, Self>>
- {
+ pub fn search_messages<'q>(self: &'d Self) -> Result<Messages<'q, Self>> {
let mut msgs = ptr::null_mut();
- try!(unsafe {
- ffi::notmuch_query_search_messages(
- self.handle.ptr, &mut msgs,
- )
- }.as_result());
+ try!(
+ unsafe { ffi::notmuch_query_search_messages(self.handle.ptr, &mut msgs,) }.as_result()
+ );
- Ok(Messages::from_ptr(msgs))
+ Ok(Messages::from_ptr(msgs, self))
}
- pub fn count_messages(self: &Self) -> Result<u32>
- {
+ pub fn count_messages(self: &Self) -> Result<u32> {
let mut cnt = 0;
- try!(unsafe {
- ffi::notmuch_query_count_messages(
- self.handle.ptr, &mut cnt,
- )
- }.as_result());
+ try!(unsafe { ffi::notmuch_query_count_messages(self.handle.ptr, &mut cnt,) }.as_result());
Ok(cnt)
}
- pub fn search_threads<'q>(self: &'d Self) -> Result<Threads<'q, Self>>
- {
+ pub fn search_threads<'q>(self: &'d Self) -> Result<Threads<'q, Self>> {
let mut thrds = ptr::null_mut();
- try!(unsafe {
- ffi::notmuch_query_search_threads(
- self.handle.ptr, &mut thrds,
- )
- }.as_result());
+ try!(
+ unsafe { ffi::notmuch_query_search_threads(self.handle.ptr, &mut thrds,) }.as_result()
+ );
- Ok(Threads::from_ptr(thrds))
+ Ok(Threads::from_ptr(thrds, self))
}
- pub fn count_threads(self: &Self) -> Result<u32>
- {
+ pub fn count_threads(self: &Self) -> Result<u32> {
let mut cnt = 0;
- try!(unsafe {
- ffi::notmuch_query_count_threads(
- self.handle.ptr, &mut cnt,
- )
- }.as_result());
+ try!(unsafe { ffi::notmuch_query_count_threads(self.handle.ptr, &mut cnt,) }.as_result());
Ok(cnt)
}
}
-
unsafe impl<'d> Send for Query<'d> {}
unsafe impl<'d> Sync for Query<'d> {}