chiark / gitweb /
Make FileContents store the item ids.
authorSimon Tatham <anakin@pobox.com>
Sat, 6 Jan 2024 11:58:18 +0000 (11:58 +0000)
committerSimon Tatham <anakin@pobox.com>
Sat, 6 Jan 2024 13:36:17 +0000 (13:36 +0000)
This allows it to also search for the index of a specific item.

src/file.rs

index 2df82515972efab55bd3f5e8273a8e5a8059e204..22680b357642dccfe42fcf1367646e2776cc87ce 100644 (file)
@@ -224,7 +224,7 @@ struct FileContents<Type: FileType, Source: FileDataSource> {
     header: FileHeader,
     extender: Option<ExtendableIndicator>,
     origin: isize,
-    items: Vec<Type::Item>,
+    items: Vec<(String, Type::Item)>,
 }
 
 impl<Type: FileType, Source: FileDataSource> FileContents<Type,Source> {
@@ -242,7 +242,7 @@ impl<Type: FileType, Source: FileDataSource> FileContents<Type,Source> {
         for id in ids {
             let item = Type::get_from_client(&id, client)
                 .expect("Any id stored in a Feed should also be cached");
-            self.items.push(item);
+            self.items.push((id.to_owned(), item));
         }
     }
 
@@ -274,9 +274,19 @@ impl<Type: FileType, Source: FileDataSource> FileContents<Type,Source> {
             }
         } else {
             let index = self.phys_index(index);
-            &self.items[index]
+            &self.items[index].1
         }
     }
+
+    fn index_of_id(&self, id: &str) -> Option<isize> {
+        // We can't do anything efficient like binary search, because
+        // our ids might not be in any sensible order. (If they're,
+        // say, a list of users that took an action, the ids' natural
+        // order would be user creation date, but here they'd be
+        // ordered by when each user did the thing.)
+        self.items.iter().position(|item| item.0 == id)
+            .map(|u| (u as isize) + self.origin)
+    }
 }
 
 #[derive(Debug, PartialEq, Eq, Clone, Copy)]