on master help sign in
Improve iterator usage
7956485Marcin Puc
src/assets.rs+1−1
@@ -351,7 +351,7 @@ impl HighlightingAssets {
351 let file_path = Path::new(file_name);
351 let file_path = Path::new(file_name);
352 let mut syntax = None;
352 let mut syntax = None;
353 if let Some(file_str) = file_path.to_str() {
353 if let Some(file_str) = file_path.to_str() {
354− for suffix in IGNORED_SUFFIXES.iter() {
354+ for suffix in &IGNORED_SUFFIXES {
355 if let Some(stripped_filename) = file_str.strip_suffix(suffix) {
355 if let Some(stripped_filename) = file_str.strip_suffix(suffix) {
356 syntax = self.get_extension_syntax(OsStr::new(stripped_filename))?;
356 syntax = self.get_extension_syntax(OsStr::new(stripped_filename))?;
357 break;
357 break;
src/bin/bat/main.rs+2−2
@@ -91,7 +91,7 @@ pub fn get_languages(config: &Config) -> Result<String> {
91 .collect::<Vec<_>>();
91 .collect::<Vec<_>>();
92
92
93 // Handling of file-extension conflicts, see issue #1076
93 // Handling of file-extension conflicts, see issue #1076
94− for lang in languages.iter_mut() {
94+ for lang in &mut languages {
95 let lang_name = lang.name.clone();
95 let lang_name = lang.name.clone();
96 lang.file_extensions.retain(|extension| {
96 lang.file_extensions.retain(|extension| {
97 // The 'extension' variable is not certainly a real extension.
97 // The 'extension' variable is not certainly a real extension.
@@ -115,7 +115,7 @@ pub fn get_languages(config: &Config) -> Result<String> {
115
115
116 let configured_languages = get_syntax_mapping_to_paths(config.syntax_mapping.mappings());
116 let configured_languages = get_syntax_mapping_to_paths(config.syntax_mapping.mappings());
117
117
118− for lang in languages.iter_mut() {
118+ for lang in &mut languages {
119 if let Some(additional_paths) = configured_languages.get(lang.name.as_str()) {
119 if let Some(additional_paths) = configured_languages.get(lang.name.as_str()) {
120 lang.file_extensions
120 lang.file_extensions
121 .extend(additional_paths.iter().cloned());
121 .extend(additional_paths.iter().cloned());
src/controller.rs+2−2
@@ -175,8 +175,8 @@ impl<'b> Controller<'b> {
175 let mut line_ranges: Vec<LineRange> = vec![];
175 let mut line_ranges: Vec<LineRange> = vec![];
176
176
177 if let Some(line_changes) = line_changes {
177 if let Some(line_changes) = line_changes {
178− for line in line_changes.keys() {
178+ for &line in line_changes.keys() {
179− let line = *line as usize;
179+ let line = line as usize;
180 line_ranges.push(LineRange::new(line - context, line + context));
180 line_ranges.push(LineRange::new(line - context, line + context));
181 }
181 }
182 }
182 }
src/printer.rs+3−4
@@ -420,8 +420,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
420 let decorations = self
420 let decorations = self
421 .decorations
421 .decorations
422 .iter()
422 .iter()
423− .map(|d| d.generate(line_number, false, self))
423+ .map(|d| d.generate(line_number, false, self));
424− .collect::<Vec<_>>();
425
424
426 for deco in decorations {
425 for deco in decorations {
427 write!(handle, "{} ", deco.text)?;
426 write!(handle, "{} ", deco.text)?;
@@ -435,7 +434,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
435 let colored_output = self.config.colored_output;
434 let colored_output = self.config.colored_output;
436 let italics = self.config.use_italic_text;
435 let italics = self.config.use_italic_text;
437
436
438− for &(style, region) in regions.iter() {
437+ for &(style, region) in &regions {
439 let text = &*self.preprocess(region, &mut cursor_total);
438 let text = &*self.preprocess(region, &mut cursor_total);
440 let text_trimmed = text.trim_end_matches(|c| c == '\r' || c == '\n');
439 let text_trimmed = text.trim_end_matches(|c| c == '\r' || c == '\n');
441 write!(
440 write!(
@@ -472,7 +471,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
472 writeln!(handle)?;
471 writeln!(handle)?;
473 }
472 }
474 } else {
473 } else {
475− for &(style, region) in regions.iter() {
474+ for &(style, region) in &regions {
476 let ansi_iterator = AnsiCodeIterator::new(region);
475 let ansi_iterator = AnsiCodeIterator::new(region);
477 let mut ansi_prefix: String = String::new();
476 let mut ansi_prefix: String = String::new();
478 for chunk in ansi_iterator {
477 for chunk in ansi_iterator {
src/syntax_mapping.rs+2−4
@@ -81,7 +81,7 @@ impl<'a> SyntaxMapping<'a> {
81 .unwrap();
81 .unwrap();
82 }
82 }
83
83
84− for glob in [
84+ for glob in &[
85 "**/systemd/**/*.conf",
85 "**/systemd/**/*.conf",
86 "**/systemd/**/*.example",
86 "**/systemd/**/*.example",
87 "*.automount",
87 "*.automount",
@@ -100,9 +100,7 @@ impl<'a> SyntaxMapping<'a> {
100 "*.swap",
100 "*.swap",
101 "*.target",
101 "*.target",
102 "*.timer",
102 "*.timer",
103− ]
103+ ] {
104− .iter()
105− {
106 mapping.insert(glob, MappingTarget::MapTo("INI")).unwrap();
104 mapping.insert(glob, MappingTarget::MapTo("INI")).unwrap();
107 }
105 }
108
106