From 731a2bea850e760aae0b1c3802ae5d6ecd0dfbf9 Mon Sep 17 00:00:00 2001 From: Ben C Date: Wed, 27 Dec 2023 01:47:51 -0500 Subject: [PATCH] Linting --- core/src/bootstrap.rs | 12 ++++++------ core/src/parser.rs | 4 ++-- macros/src/lib.rs | 10 +++++----- utils/src/grid.rs | 25 +++++++++++-------------- utils/src/pos.rs | 41 +++++++++++++++++++---------------------- utils/src/range.rs | 10 +++++----- 6 files changed, 48 insertions(+), 54 deletions(-) diff --git a/core/src/bootstrap.rs b/core/src/bootstrap.rs index 6c67ad3..f881778 100644 --- a/core/src/bootstrap.rs +++ b/core/src/bootstrap.rs @@ -86,7 +86,7 @@ fn make_examples(folder: &Path) { fn make_lib(folder: &Path, year: &str) { let lib_path = folder.join("lib.rs"); - let contents = YEAR_TEMPLATE.replace("{year}", &year); + let contents = YEAR_TEMPLATE.replace("{year}", year); std::fs::write(lib_path, contents).unwrap(); } @@ -94,7 +94,7 @@ fn make_lib(folder: &Path, year: &str) { fn make_main(folder: &Path, year: &str) { let main_path = folder.join("main.rs"); - let contents = RUNNER_TEMPLATE.replace("{year}", &year); + let contents = RUNNER_TEMPLATE.replace("{year}", year); std::fs::write(main_path, contents).unwrap(); } @@ -106,14 +106,14 @@ fn make_src(folder: &Path, year: &str) { make_days(&src_path); make_examples(&src_path); - make_lib(&src_path, &year); - make_main(&src_path, &year); + make_lib(&src_path, year); + make_main(&src_path, year); } fn make_cargo(folder: &Path, year: &str) { let cargo_path = folder.join("Cargo.toml"); - let contents = CARGO_TEMPLATE.replace("{year}", &year); + let contents = CARGO_TEMPLATE.replace("{year}", year); std::fs::write(cargo_path, contents).unwrap(); } @@ -131,7 +131,7 @@ fn replace_year_list(new_year: &str) { .get(1) .unwrap() .as_str() - .split(",") + .split(',') .map(|s| s.parse::().unwrap()) .collect::>(); diff --git a/core/src/parser.rs b/core/src/parser.rs index 992e34d..9c0b915 100644 --- a/core/src/parser.rs +++ b/core/src/parser.rs @@ -68,7 +68,7 @@ impl YDP { pub fn get_dp_and_input() -> (DP, Option) { let mut args = args().skip(1); - let dp = args.next().map(|s| DP::parse(&s.trim())).unwrap_or(DP_ALL); + let dp = args.next().map(|s| DP::parse(s.trim())).unwrap_or(DP_ALL); let input = args.next().map(|s| s.trim().to_string()).map(|i| { if i == "-" { @@ -88,7 +88,7 @@ pub fn get_dp_and_input() -> (DP, Option) { pub fn get_ydp_and_input(args: Vec) -> (YDP, Option) { let mut args = args.into_iter(); - let ydp = args.next().map(|s| YDP::parse(&s.trim())).unwrap_or(YDP { + let ydp = args.next().map(|s| YDP::parse(s.trim())).unwrap_or(YDP { year: Selection::All, day: Selection::All, part: Selection::All, diff --git a/macros/src/lib.rs b/macros/src/lib.rs index efcc7a8..c435530 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -150,15 +150,15 @@ pub fn year_runner(item: TokenStream) -> TokenStream { .unwrap() } -fn make_year_match(years: &Vec<&str>, inner: &str) -> String { +fn make_year_match(years: &[&str], inner: &str) -> String { years .iter() - .map(|year| format!("{year} => {},", inner.replace("{year}", &year.to_string()))) + .map(|year| format!("{year} => {},", inner.replace("{year}", year.as_ref()))) .collect::>() .join("\n") } -fn make_year_uses(years: &Vec<&str>) -> String { +fn make_year_uses(years: &[&str]) -> String { years .iter() .map(|year| format!("use y_{year}::Year{year};", year = year)) @@ -166,7 +166,7 @@ fn make_year_uses(years: &Vec<&str>) -> String { .join("\n") } -fn make_run_all_years(years: &Vec<&str>) -> String { +fn make_run_all_years(years: &[&str]) -> String { years .iter() .map(|year| { @@ -179,7 +179,7 @@ fn make_run_all_years(years: &Vec<&str>) -> String { .join("\n") } -fn make_run_year(years: &Vec<&str>) -> String { +fn make_run_year(years: &[&str]) -> String { let inner = make_year_match(years, "Year{year}::run_dp(input.as_deref(), dp)"); format!( " diff --git a/utils/src/grid.rs b/utils/src/grid.rs index 1badb76..ee208c1 100644 --- a/utils/src/grid.rs +++ b/utils/src/grid.rs @@ -442,36 +442,33 @@ impl Grid { } /// Like [Grid::relatives] but with `kernels` set to the four cardinal directions. - pub fn adjacent<'a>( - &'a self, - pos: Position, - ) -> impl Iterator + 'a { + pub fn adjacent(&self, pos: Position) -> impl Iterator { self.relatives(pos, &CARDINALS) } /// Like [Grid::relatives_wrapped] but with `kernels` set to the four cardinal directions. - pub fn adjacent_wrapped<'a>( - &'a self, + pub fn adjacent_wrapped( + &self, pos: Position, - ) -> impl Iterator + 'a { + ) -> impl Iterator { self.relatives_wrapped(pos, &CARDINALS) } /// Like [Grid::relatives_expand_by] but with `kernels` set to the four cardinal directions. - pub fn adjacent_expand_by<'a>( - &'a self, + pub fn adjacent_expand_by( + &self, pos: Position, expand: usize, - ) -> impl Iterator + 'a { + ) -> impl Iterator { self.relatives_expand_by(pos, &CARDINALS, expand) } /// Like [Grid::relatives_expand_by_wrapped] but with `kernels` set to the four cardinal directions. - pub fn adjacent_expand_by_wrapped<'a>( - &'a self, + pub fn adjacent_expand_by_wrapped( + &self, pos: Position, expand: usize, - ) -> impl Iterator + 'a { + ) -> impl Iterator { self.relatives_expand_by_wrapped(pos, &CARDINALS, expand) } } @@ -739,7 +736,7 @@ pub mod cursors { } /// Move the cursor forward one step in the direction it is facing and get the value at the new position. - pub fn next(&mut self) -> Option<&T> { + pub fn advance_get(&mut self) -> Option<&T> { self.move_forward(); self.get() } diff --git a/utils/src/pos.rs b/utils/src/pos.rs index cc0baef..7ae215b 100644 --- a/utils/src/pos.rs +++ b/utils/src/pos.rs @@ -518,11 +518,8 @@ impl Position { /// assert_eq!(relatives, vec![(Position::new(0, -1), Direction::North), (Position::new(1, 0), Direction::East)]); /// ``` /// - pub fn relatives<'a, T: Movement>( - self, - kernels: &'a [T], - ) -> impl Iterator + 'a { - kernels.into_iter().map(move |k| (self.move_dir(*k), *k)) + pub fn relatives(self, kernels: &[T]) -> impl Iterator + '_ { + kernels.iter().map(move |k| (self.move_dir(*k), *k)) } /// Get all positions relative to this position by a list of directions, @@ -537,11 +534,11 @@ impl Position { /// assert_eq!(relatives, vec![(Position::new(1, 0), Direction::East)]); /// ``` /// - pub fn relatives_checked<'a, T: Movement>( + pub fn relatives_checked( self, - kernels: &'a [T], + kernels: &[T], bounds: PositiveType, - ) -> impl Iterator + 'a { + ) -> impl Iterator + '_ { kernels .iter() .filter_map(move |k| self.move_dir_checked(*k, bounds).map(|p| (p, *k))) @@ -566,13 +563,13 @@ impl Position { /// assert_eq!(relatives, expected); /// ``` /// - pub fn relatives_expand_by<'a, T: Movement>( + pub fn relatives_expand_by( self, - kernels: &'a [T], + kernels: &[T], times: usize, - ) -> impl Iterator + 'a { + ) -> impl Iterator + '_ { kernels - .into_iter() + .iter() .flat_map(move |k| (1..=times).map(move |t| ((*k, t), self.move_times(*k, t)))) } @@ -594,13 +591,13 @@ impl Position { /// assert_eq!(relatives, expected); /// ``` /// - pub fn relatives_expand_by_checked<'a, T: Movement>( + pub fn relatives_expand_by_checked( self, - kernels: &'a [T], + kernels: &[T], times: usize, bounds: PositiveType, - ) -> impl Iterator + 'a { - kernels.into_iter().flat_map(move |k| { + ) -> impl Iterator + '_ { + kernels.iter().flat_map(move |k| { (1..=times) .filter_map(move |t| self.move_times_checked(*k, t, bounds).map(|p| ((*k, t), p))) }) @@ -747,9 +744,9 @@ impl From<(CompType, CompType)> for Position { } } -impl Into<(CompType, CompType)> for Position { - fn into(self) -> (CompType, CompType) { - (self.x, self.y) +impl From for (CompType, CompType) { + fn from(val: Position) -> Self { + (val.x, val.y) } } @@ -762,9 +759,9 @@ impl From<(usize, usize)> for Position { } } -impl Into<(usize, usize)> for Position { - fn into(self) -> (usize, usize) { - (self.x as usize, self.y as usize) +impl From for (usize, usize) { + fn from(val: Position) -> Self { + (val.x as usize, val.y as usize) } } diff --git a/utils/src/range.rs b/utils/src/range.rs index 14d7815..0e842fe 100644 --- a/utils/src/range.rs +++ b/utils/src/range.rs @@ -115,15 +115,15 @@ impl BetterRange { } } -impl Into> for BetterRange { - fn into(self) -> Range { - self.start..self.end +impl From> for Range { + fn from(val: BetterRange) -> Self { + val.start..val.end } } impl From> for BetterRange { - fn from(range: Range) -> Self { - Self::new(range.start, range.end) + fn from(val: Range) -> Self { + BetterRange::new(val.start, val.end) } } -- 2.51.2