on master help sign in
Improve iterator usage
commit7956485e37c8fc48506df1484f34811104eb882b authorMarcin Puc <marcin.e.puc@gmail.com> dateFri 10 Sep 2021, 21:56 (5 years ago) parent372e42f
5 files changed+10−13
src/assets.rs+1−1 src/bin/bat/main.rs+2−2 src/controller.rs+2−2 src/printer.rs+3−4 src/syntax_mapping.rs+2−4
src/assets.rs+1−1
@@ -351,7 +351,7 @@ impl HighlightingAssets {
351351 let file_path = Path::new(file_name);
352352 let mut syntax = None;
353353 if let Some(file_str) = file_path.to_str() {
354− for suffix in IGNORED_SUFFIXES.iter() {
354+ for suffix in &IGNORED_SUFFIXES {
355355 if let Some(stripped_filename) = file_str.strip_suffix(suffix) {
356356 syntax = self.get_extension_syntax(OsStr::new(stripped_filename))?;
357357 break;
src/bin/bat/main.rs+2−2
@@ -91,7 +91,7 @@ pub fn get_languages(config: &Config) -> Result<String> {
9191 .collect::<Vec<_>>();
9292
9393 // Handling of file-extension conflicts, see issue #1076
94− for lang in languages.iter_mut() {
94+ for lang in &mut languages {
9595 let lang_name = lang.name.clone();
9696 lang.file_extensions.retain(|extension| {
9797 // The 'extension' variable is not certainly a real extension.
@@ -115,7 +115,7 @@ pub fn get_languages(config: &Config) -> Result<String> {
115115
116116 let configured_languages = get_syntax_mapping_to_paths(config.syntax_mapping.mappings());
117117
118− for lang in languages.iter_mut() {
118+ for lang in &mut languages {
119119 if let Some(additional_paths) = configured_languages.get(lang.name.as_str()) {
120120 lang.file_extensions
121121 .extend(additional_paths.iter().cloned());
src/controller.rs+2−2
@@ -175,8 +175,8 @@ impl<'b> Controller<'b> {
175175 let mut line_ranges: Vec<LineRange> = vec![];
176176
177177 if let Some(line_changes) = line_changes {
178− for line in line_changes.keys() {
179− let line = *line as usize;
178+ for &line in line_changes.keys() {
179+ let line = line as usize;
180180 line_ranges.push(LineRange::new(line - context, line + context));
181181 }
182182 }
src/printer.rs+3−4
@@ -420,8 +420,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
420420 let decorations = self
421421 .decorations
422422 .iter()
423− .map(|d| d.generate(line_number, false, self))
424− .collect::<Vec<_>>();
423+ .map(|d| d.generate(line_number, false, self));
425424
426425 for deco in decorations {
427426 write!(handle, "{} ", deco.text)?;
@@ -435,7 +434,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
435434 let colored_output = self.config.colored_output;
436435 let italics = self.config.use_italic_text;
437436
438− for &(style, region) in regions.iter() {
437+ for &(style, region) in &regions {
439438 let text = &*self.preprocess(region, &mut cursor_total);
440439 let text_trimmed = text.trim_end_matches(|c| c == '\r' || c == '\n');
441440 write!(
@@ -472,7 +471,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
472471 writeln!(handle)?;
473472 }
474473 } else {
475− for &(style, region) in regions.iter() {
474+ for &(style, region) in &regions {
476475 let ansi_iterator = AnsiCodeIterator::new(region);
477476 let mut ansi_prefix: String = String::new();
478477 for chunk in ansi_iterator {
src/syntax_mapping.rs+2−4
@@ -81,7 +81,7 @@ impl<'a> SyntaxMapping<'a> {
8181 .unwrap();
8282 }
8383
84− for glob in [
84+ for glob in &[
8585 "**/systemd/**/*.conf",
8686 "**/systemd/**/*.example",
8787 "*.automount",
@@ -100,9 +100,7 @@ impl<'a> SyntaxMapping<'a> {
100100 "*.swap",
101101 "*.target",
102102 "*.timer",
103− ]
104− .iter()
105− {
103+ ] {
106104 mapping.insert(glob, MappingTarget::MapTo("INI")).unwrap();
107105 }
108106