summary refs log tree commit diff stats
path: root/src/list.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/list.rs')
-rw-r--r--src/list.rs25
1 files changed, 18 insertions, 7 deletions
diff --git a/src/list.rs b/src/list.rs
index 2f1f93c..5d40e5a 100644
--- a/src/list.rs
+++ b/src/list.rs
@@ -56,7 +56,7 @@ pub struct Entries<'a, 'ph, T> {
     pub(crate) context: &'a crate::PluginHandle<'ph>,
     pub(crate) list: *mut crate::internals::HexchatList,
     pub(crate) t: &'a T,
-    pub(crate) valid: Rc<Cell<usize>>,
+    pub(crate) valid: Option<Rc<Cell<usize>>>,
 }
 
 /// Fields.
@@ -99,7 +99,9 @@ impl List for Users {
 impl<'a, 'ph, T> Drop for Entries<'a, 'ph, T> {
     fn drop(&mut self) {
         let ph = &self.context;
-        self.valid.set(usize::MAX);
+        if let Some(ref valid) = self.valid {
+            valid.set(usize::MAX);
+        }
         unsafe {
             ph_call!(hexchat_list_free(ph, self.list));
         }
@@ -111,21 +113,30 @@ impl<'a, 'ph, T> Iterator for Entries<'a, 'ph, T> {
 
     fn next(&mut self) -> Option<Self::Item> {
         let ph = &self.context;
-        let id = self.valid.get();
         if unsafe {
             ph_call!(hexchat_list_next(ph, self.list))
         } != 0 {
-            debug_assert!(self.valid.get() < usize::MAX);
-            self.valid.set(id + 1);
+            let valid = if let Some(ref valid) = self.valid {
+                debug_assert!(valid.get() < usize::MAX);
+                valid.set(valid.get() + 1);
+                Rc::clone(valid)
+            } else {
+                let valid: Rc<Cell<usize>> = Default::default();
+                self.valid = Some(Rc::clone(&valid));
+                valid
+            };
+            let id = valid.get();
             Some(Fields {
                 context: self.context,
                 list: self.list,
                 _t: self.t,
-                valid: Rc::clone(&self.valid),
+                valid: valid,
                 id: id,
             })
         } else {
-            self.valid.set(usize::MAX);
+            if let Some(ref valid) = self.valid {
+                valid.set(usize::MAX);
+            }
             None
         }
     }