Use map_while instead of take_while + map

This commit is contained in:
Yuki Okushi 2021-06-22 06:27:22 +09:00
parent cb3b3cf6ab
commit 8d00be9980
No known key found for this signature in database
GPG Key ID: DABA5B072961C18A
2 changed files with 6 additions and 4 deletions

View File

@ -27,6 +27,8 @@
#![feature(min_type_alias_impl_trait)]
#![allow(rustc::default_hash_types)]
#![deny(unaligned_references)]
#![feature(iter_map_while)]
#![feature(bool_to_option)]
#[macro_use]
extern crate tracing;

View File

@ -86,10 +86,10 @@ pub fn get_by_key(&'a self, key: K) -> impl 'a + Iterator<Item = &'a V> {
/// insertion order.
pub fn get_by_key_enumerated(&'a self, key: K) -> impl '_ + Iterator<Item = (I, &V)> {
let lower_bound = self.idx_sorted_by_item_key.partition_point(|&i| self.items[i].0 < key);
self.idx_sorted_by_item_key[lower_bound..]
.iter()
.take_while(move |&&i| self.items[i].0.eq(&key))
.map(move |&idx| (idx, &self.items[idx].1))
self.idx_sorted_by_item_key[lower_bound..].iter().map_while(move |&i| {
let (k, v) = &self.items[i];
(k == &key).then_some((i, v))
})
}
}