1
1
mirror of https://github.com/wez/wezterm.git synced 2024-10-26 23:58:28 +03:00

fix(clippy): Use faster methods on Iterators

- Use `find` instead of `position(..).next()`
- Use `any` instead of `position(..).next().is_some()/.is_none()`
- Use `first/next` instead of `get(0)/nth(0)`
- Prefer `for` loops over `while let` loops on iterators

May improve performance.
This commit is contained in:
Jalil David Salamé Messina 2023-03-18 00:05:39 -03:00 committed by Wez Furlong
parent 1ccc6c9ce0
commit 191aacc7d7
7 changed files with 9 additions and 13 deletions

View File

@ -103,8 +103,8 @@ impl BidiRun {
impl<'a> Iterator for Iter<'a> { impl<'a> Iterator for Iter<'a> {
type Item = usize; type Item = usize;
fn next(&mut self) -> Option<usize> { fn next(&mut self) -> Option<usize> {
while let Some(idx) = self.range.next() { for idx in self.range.by_ref() {
if self.removed_by_x9.iter().position(|&i| i == idx).is_some() { if self.removed_by_x9.iter().any(|&i| i == idx) {
// Skip it // Skip it
continue; continue;
} }

View File

@ -566,7 +566,7 @@ impl TextStyle {
// Insert our bundled default JetBrainsMono as a fallback // Insert our bundled default JetBrainsMono as a fallback
// in case their preference doesn't match anything. // in case their preference doesn't match anything.
// But don't add it if it is already their preference. // But don't add it if it is already their preference.
if font.iter().position(|f| *f == default_font).is_none() { if !font.iter().any(|f| *f == default_font) {
default_font.is_fallback = true; default_font.is_fallback = true;
font.push(default_font); font.push(default_font);
} }

View File

@ -71,7 +71,7 @@ impl LocalProcessInfo {
Some(LinuxStat { Some(LinuxStat {
pid, pid,
name: name.to_string(), name: name.to_string(),
status: fields.get(0)?.to_string(), status: fields.first()?.to_string(),
ppid: fields.get(1)?.parse().ok()?, ppid: fields.get(1)?.parse().ok()?,
starttime: fields.get(20)?.parse().ok()?, starttime: fields.get(20)?.parse().ok()?,
}) })

View File

@ -1905,7 +1905,7 @@ impl TerminalState {
break; break;
} }
let ch = cell.str().chars().nth(0).unwrap() as u32; let ch = cell.str().chars().next().unwrap() as u32;
// debug!("y={} col={} ch={:x} cell={:?}", y + y_origin, col, ch, cell); // debug!("y={} col={} ch={:x} cell={:?}", y + y_origin, col, ch, cell);
checksum += u16::from(ch as u8); checksum += u16::from(ch as u8);

View File

@ -552,8 +552,7 @@ impl ParsedFont {
}; };
let style = *styles let style = *styles
.iter() .iter()
.filter(|&&style| candidates.iter().any(|&idx| fonts[idx].style == style)) .find(|&&style| candidates.iter().any(|&idx| fonts[idx].style == style))?;
.next()?;
// Reduce to matching italics // Reduce to matching italics
candidates.retain(|&idx| fonts[idx].style == style); candidates.retain(|&idx| fonts[idx].style == style);

View File

@ -466,8 +466,8 @@ impl HarfbuzzShaper {
cluster_resolver.build(hb_infos, s, &range); cluster_resolver.build(hb_infos, s, &range);
log::debug!("cluster_resolver: {cluster_resolver:#?}"); log::debug!("cluster_resolver: {cluster_resolver:#?}");
let mut info_iter = hb_infos.iter().zip(positions.iter()).peekable(); let info_iter = hb_infos.iter().zip(positions.iter()).peekable();
while let Some((info, pos)) = info_iter.next() { for (info, pos) in info_iter {
let cluster_info = match cluster_resolver.get_mut(info.cluster as usize) { let cluster_info = match cluster_resolver.get_mut(info.cluster as usize) {
Some(i) => i, Some(i) => i,
None => panic!( None => panic!(

View File

@ -61,10 +61,7 @@ pub struct GlConnection {
impl GlConnection { impl GlConnection {
#[allow(dead_code)] #[allow(dead_code)]
pub fn has_extension(&self, wanted: &str) -> bool { pub fn has_extension(&self, wanted: &str) -> bool {
self.extensions self.extensions.split(' ').any(|ext| ext == wanted)
.split(' ')
.find(|&ext| ext == wanted)
.is_some()
} }
} }