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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::ops::Drop;
use ffi;
use utils::ScopedPhantomcow;
use MessageOwner;
use Message;
use Tags;
use TagsOwner;
#[derive(Debug)]
pub struct Messages<'o, O>
where
O: MessageOwner + 'o,
{
pub(crate) ptr: *mut ffi::notmuch_messages_t,
marker: ScopedPhantomcow<'o, O>,
}
impl<'o, O> Drop for Messages<'o, O>
where
O: MessageOwner + 'o,
{
fn drop(self: &mut Self) {
unsafe { ffi::notmuch_messages_destroy(self.ptr) };
}
}
impl<'o, O> Messages<'o, O>
where
O: MessageOwner + 'o,
{
pub(crate) fn from_ptr<P>(ptr: *mut ffi::notmuch_messages_t, owner: P) -> Messages<'o, O>
where
P: Into<ScopedPhantomcow<'o, O>>,
{
Messages {
ptr,
marker: owner.into(),
}
}
}
impl<'o, O> MessageOwner for Messages<'o, O> where O: MessageOwner + 'o {}
impl<'o, O> TagsOwner for Messages<'o, O> where O: MessageOwner + 'o {}
impl<'o, O> Messages<'o, O>
where
O: MessageOwner + 'o,
{
pub fn collect_tags<'m>(self: &'o Self) -> Tags<'m, Self> {
Tags::from_ptr(
unsafe { ffi::notmuch_messages_collect_tags(self.ptr) },
self,
)
}
}
impl<'o, O> Iterator for Messages<'o, O>
where
O: MessageOwner + 'o,
{
type Item = Message<'o, O>;
fn next(&mut self) -> Option<Self::Item> {
let valid = unsafe { ffi::notmuch_messages_valid(self.ptr) };
if valid == 0 {
return None;
}
let cthrd = unsafe {
let thrd = ffi::notmuch_messages_get(self.ptr);
ffi::notmuch_messages_move_to_next(self.ptr);
thrd
};
Some(Message::from_ptr(cthrd, ScopedPhantomcow::<'o, O>::share(&mut self.marker)))
}
}
pub trait MessagesExt<'o, O>
where
O: MessageOwner + 'o,
{
}
impl<'o, O> MessagesExt<'o, O> for Messages<'o, O> where O: MessageOwner + 'o {}
unsafe impl<'o, O> Send for Messages<'o, O> where O: MessageOwner + 'o {}
unsafe impl<'o, O> Sync for Messages<'o, O> where O: MessageOwner + 'o {}
#[cfg(test)]
mod tests {
fn descend<'o, O: 'o + super::MessageOwner, T: Iterator<Item=super::Message<'o, O>>>(iter: T)
-> usize {
iter.map(|msg| descend(msg.replies()) ).count()
}
use query::Query;
use database;
#[test]
#[should_panic] fn recurse() -> () {
match database::Database::open(
&String::new(),
database::DatabaseMode::ReadOnly,
) {
Ok(db) => {
let q = Query::create(db, &String::new()).unwrap();
descend::<Query, super::Messages<Query>>(q.search_messages().unwrap());
}
Err(err) => {
panic!("Got error while trying to open db: {:?}", err);
}
}
}
}