Use Iterator::min_by instead of fold in hit_test

This commit is contained in:
Héctor Ramón Jiménez 2021-09-15 15:08:35 +07:00
parent 643500bbdf
commit c914b2a05b
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
2 changed files with 22 additions and 26 deletions

View file

@ -187,20 +187,18 @@ impl Pipeline {
}
}
let (idx, nearest) = bounds.fold(
(None, iced_native::Point::ORIGIN),
|best, (idx, bounds)| {
let center = bounds.center();
let nearest = bounds
.map(|(index, bounds)| (index, bounds.center()))
.min_by(|(_, center_a), (_, center_b)| {
center_a
.distance(point)
.partial_cmp(&center_b.distance(point))
.unwrap_or(std::cmp::Ordering::Greater)
});
if center.distance(point) < best.1.distance(point) {
(Some(idx), center)
} else {
best
}
},
);
idx.map(|idx| Hit::NearestCharOffset(char_index(idx), point - nearest))
nearest.map(|(idx, center)| {
Hit::NearestCharOffset(char_index(idx), point - center)
})
}
pub fn trim_measurement_cache(&mut self) {

View file

@ -195,20 +195,18 @@ impl Pipeline {
}
}
let (idx, nearest) = bounds.fold(
(None, iced_native::Point::ORIGIN),
|best, (idx, bounds)| {
let center = bounds.center();
let nearest = bounds
.map(|(index, bounds)| (index, bounds.center()))
.min_by(|(_, center_a), (_, center_b)| {
center_a
.distance(point)
.partial_cmp(&center_b.distance(point))
.unwrap_or(std::cmp::Ordering::Greater)
});
if center.distance(point) < best.1.distance(point) {
(Some(idx), center)
} else {
best
}
},
);
idx.map(|idx| Hit::NearestCharOffset(char_index(idx), point - nearest))
nearest.map(|(idx, center)| {
Hit::NearestCharOffset(char_index(idx), point - center)
})
}
pub fn trim_measurement_cache(&mut self) {