From b4d4b5795ef9229f24f838592c4c915a7f4086bc Mon Sep 17 00:00:00 2001 From: topecongiro Date: Mon, 25 Mar 2019 07:54:52 +0900 Subject: [PATCH] Use BTreeMap to guarantee consistent ordering --- src/cargo-fmt/main.rs | 44 +++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/src/cargo-fmt/main.rs b/src/cargo-fmt/main.rs index 25ce43d47e24..a686e91b6947 100644 --- a/src/cargo-fmt/main.rs +++ b/src/cargo-fmt/main.rs @@ -6,7 +6,8 @@ use cargo_metadata; use getopts; -use std::collections::{HashMap, HashSet}; +use std::cmp::Ordering; +use std::collections::{BTreeMap, BTreeSet}; use std::env; use std::fs; use std::hash::{Hash, Hasher}; @@ -122,7 +123,7 @@ fn handle_command_status(status: Result, opts: &getopts::Options } fn get_version(verbosity: Verbosity) -> Result { - run_rustfmt(&HashSet::new(), &[String::from("--version")], verbosity) + run_rustfmt(&BTreeSet::new(), &[String::from("--version")], verbosity) } fn format_crate(verbosity: Verbosity, strategy: &CargoFmtStrategy) -> Result { @@ -131,7 +132,7 @@ fn format_crate(verbosity: Verbosity, strategy: &CargoFmtStrategy) -> Result Option { + Some(self.path.cmp(&other.path)) + } +} + +impl Ord for Target { + fn cmp(&self, other: &Target) -> Ordering { + self.path.cmp(&other.path) + } +} + impl Eq for Target {} impl Hash for Target { @@ -204,12 +217,12 @@ impl CargoFmtStrategy { } /// Based on the specified `CargoFmtStrategy`, returns a set of main source files. -fn get_targets(strategy: &CargoFmtStrategy) -> Result, io::Error> { - let mut targets = HashSet::new(); +fn get_targets(strategy: &CargoFmtStrategy) -> Result, io::Error> { + let mut targets = BTreeSet::new(); match *strategy { CargoFmtStrategy::Root => get_targets_root_only(&mut targets)?, - CargoFmtStrategy::All => get_targets_recursive(None, &mut targets, &mut HashSet::new())?, + CargoFmtStrategy::All => get_targets_recursive(None, &mut targets, &mut BTreeSet::new())?, CargoFmtStrategy::Some(ref hitlist) => get_targets_with_hitlist(hitlist, &mut targets)?, } @@ -223,7 +236,7 @@ fn get_targets(strategy: &CargoFmtStrategy) -> Result, io::Error } } -fn get_targets_root_only(targets: &mut HashSet) -> Result<(), io::Error> { +fn get_targets_root_only(targets: &mut BTreeSet) -> Result<(), io::Error> { let metadata = get_cargo_metadata(None)?; let current_dir = env::current_dir()?.canonicalize()?; let current_dir_manifest = current_dir.join("Cargo.toml"); @@ -243,8 +256,8 @@ fn get_targets_root_only(targets: &mut HashSet) -> Result<(), io::Error> fn get_targets_recursive( manifest_path: Option<&Path>, - mut targets: &mut HashSet, - visited: &mut HashSet, + mut targets: &mut BTreeSet, + visited: &mut BTreeSet, ) -> Result<(), io::Error> { let metadata = get_cargo_metadata(manifest_path)?; @@ -275,11 +288,11 @@ fn get_targets_recursive( fn get_targets_with_hitlist( hitlist: &[String], - targets: &mut HashSet, + targets: &mut BTreeSet, ) -> Result<(), io::Error> { let metadata = get_cargo_metadata(None)?; - let mut workspace_hitlist: HashSet<&String> = HashSet::from_iter(hitlist); + let mut workspace_hitlist: BTreeSet<&String> = BTreeSet::from_iter(hitlist); for package in metadata.packages { if workspace_hitlist.remove(&package.name) { @@ -300,14 +313,14 @@ fn get_targets_with_hitlist( } } -fn add_targets(target_paths: &[cargo_metadata::Target], targets: &mut HashSet) { +fn add_targets(target_paths: &[cargo_metadata::Target], targets: &mut BTreeSet) { for target in target_paths { targets.insert(Target::from_target(target)); } } fn run_rustfmt( - targets: &HashSet, + targets: &BTreeSet, fmt_args: &[String], verbosity: Verbosity, ) -> Result { @@ -318,9 +331,8 @@ fn run_rustfmt( println!("[{} ({})] {:?}", t.kind, t.edition, t.path) } }) - .map(|t| (&t.edition, &t.path)) - .fold(HashMap::new(), |mut h, t| { - h.entry(t.0).or_insert_with(Vec::new).push(t.1); + .fold(BTreeMap::new(), |mut h, t| { + h.entry(&t.edition).or_insert_with(Vec::new).push(&t.path); h });