aboutsummaryrefslogtreecommitdiffstats
path: root/src/query.rs
diff options
context:
space:
mode:
authorDirk Van Haerenborgh <vhdirk@gmail.com>2018-10-16 21:01:58 +0200
committerDirk Van Haerenborgh <vhdirk@gmail.com>2018-10-24 07:12:55 +0200
commita4ffe47c51d1617fc0e728c7bbd7e9b3738878cb (patch)
tree017d0717ac8e2d5e0f6a46e47ea9e731ce63697b /src/query.rs
parentb93d4cb749714699b44b2566e50a7086c2854ac7 (diff)
downloadmail-a4ffe47c51d1617fc0e728c7bbd7e9b3738878cb.tar.gz
some tries towards better lifetimes
Diffstat (limited to 'src/query.rs')
-rw-r--r--src/query.rs61
1 files changed, 34 insertions, 27 deletions
diff --git a/src/query.rs b/src/query.rs
index e0c748e..b4cd9a2 100644
--- a/src/query.rs
+++ b/src/query.rs
@@ -5,7 +5,7 @@ use std::marker::PhantomData;
use error::Result;
use ffi;
-use utils::NewFromPtr;
+use utils::FromPtr;
use Database;
use Messages;
@@ -13,11 +13,32 @@ use Threads;
use ffi::Sort;
#[derive(Debug)]
-pub struct Query<'d>(
- pub(crate) *mut ffi::notmuch_query_t,
- PhantomData<&'d Database>,
-);
+pub(crate) struct QueryPtr {
+ pub ptr: *mut ffi::notmuch_query_t
+}
+impl Drop for QueryPtr {
+ fn drop(&mut self) {
+ unsafe {
+ ffi::notmuch_query_destroy(self.ptr)
+ };
+ }
+}
+
+#[derive(Debug)]
+pub struct Query<'d>{
+ pub(crate) handle: QueryPtr,
+ phantom: PhantomData<&'d Database>,
+}
+
+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 create(db: &'d Database, query_string: &str) -> Result<Self> {
@@ -29,7 +50,7 @@ impl<'d> Query<'d> {
{
unsafe {
ffi::notmuch_query_set_sort(
- self.0, sort.into(),
+ self.handle.ptr, sort.into(),
)
}
}
@@ -40,7 +61,7 @@ impl<'d> Query<'d> {
{
unsafe {
ffi::notmuch_query_get_sort(
- self.0,
+ self.handle.ptr,
)
}.into()
}
@@ -52,11 +73,11 @@ impl<'d> Query<'d> {
let mut msgs = ptr::null_mut();
try!(unsafe {
ffi::notmuch_query_search_messages(
- self.0, &mut msgs,
+ self.handle.ptr, &mut msgs,
)
}.as_result());
- Ok(Messages::new(msgs))
+ Ok(Messages::from_ptr(msgs))
}
pub fn count_messages(self: &Self) -> Result<u32>
@@ -64,7 +85,7 @@ impl<'d> Query<'d> {
let mut cnt = 0;
try!(unsafe {
ffi::notmuch_query_count_messages(
- self.0, &mut cnt,
+ self.handle.ptr, &mut cnt,
)
}.as_result());
@@ -76,11 +97,11 @@ impl<'d> Query<'d> {
let mut thrds = ptr::null_mut();
try!(unsafe {
ffi::notmuch_query_search_threads(
- self.0, &mut thrds,
+ self.handle.ptr, &mut thrds,
)
}.as_result());
- Ok(Threads::new(thrds))
+ Ok(Threads::from_ptr(thrds))
}
pub fn count_threads(self: &Self) -> Result<u32>
@@ -88,7 +109,7 @@ impl<'d> Query<'d> {
let mut cnt = 0;
try!(unsafe {
ffi::notmuch_query_count_threads(
- self.0, &mut cnt,
+ self.handle.ptr, &mut cnt,
)
}.as_result());
@@ -96,20 +117,6 @@ impl<'d> Query<'d> {
}
}
-impl<'d> NewFromPtr<*mut ffi::notmuch_query_t> for Query<'d> {
- fn new(ptr: *mut ffi::notmuch_query_t) -> Query<'d> {
- Query(ptr, PhantomData)
- }
-}
-
-
-impl<'d> Drop for Query<'d> {
- fn drop(&mut self) {
- unsafe {
- ffi::notmuch_query_destroy(self.0)
- };
- }
-}
unsafe impl<'d> Send for Query<'d> {}
unsafe impl<'d> Sync for Query<'d> {}