From 3991873070d0baf9d55d8e413b2fedb5e2964b70 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 6 Jan 2024 11:58:18 +0000 Subject: [PATCH] Make FileContents store the item ids. This allows it to also search for the index of a specific item. --- src/file.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/file.rs b/src/file.rs index 2df8251..22680b3 100644 --- a/src/file.rs +++ b/src/file.rs @@ -224,7 +224,7 @@ struct FileContents { header: FileHeader, extender: Option, origin: isize, - items: Vec, + items: Vec<(String, Type::Item)>, } impl FileContents { @@ -242,7 +242,7 @@ impl FileContents { 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 FileContents { } } else { let index = self.phys_index(index); - &self.items[index] + &self.items[index].1 } } + + fn index_of_id(&self, id: &str) -> Option { + // 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)] -- 2.30.2