From f2d09cc51e4a45a5999c80e3ce074c5848be6d22 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Wed, 24 Jun 2020 20:47:39 +0300 Subject: [PATCH] dc_array: simplify and test search_id This also makes it possible to search for location IDs. --- deltachat-ffi/src/dc_array.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/deltachat-ffi/src/dc_array.rs b/deltachat-ffi/src/dc_array.rs index babf814a9..6094f5546 100644 --- a/deltachat-ffi/src/dc_array.rs +++ b/deltachat-ffi/src/dc_array.rs @@ -32,16 +32,7 @@ impl dc_array_t { } pub fn search_id(&self, needle: u32) -> Option { - if let Self::Uint(array) = self { - for (i, &u) in array.iter().enumerate() { - if u == needle { - return Some(i); - } - } - None - } else { - panic!("Attempt to search for id in array of other type"); - } + (0..self.len()).find(|i| self.get_id(*i) == needle) } pub fn as_ptr(&self) -> *const u32 { @@ -82,6 +73,9 @@ mod tests { for i in 0..1000 { assert_eq!(arr.get_id(i), (i + 2) as u32); } + + assert_eq!(arr.search_id(10), Some(8)); + assert_eq!(arr.search_id(1), None); } #[test]