From 93734ea820891115d104989e023b2b421def1907 Mon Sep 17 00:00:00 2001
From: Nick Cameron <nrc@ncameron.org>
Date: Sun, 27 Nov 2022 11:23:02 +0000
Subject: [PATCH] Refactoring: move hex and hasher modules from util module to
 util crate

Signed-off-by: Nick Cameron <nrc@ncameron.org>
---
 .../util => crates/cargo-util/src}/hasher.rs  |  0
 .../util => crates/cargo-util/src}/hex.rs     |  0
 crates/cargo-util/src/lib.rs                  |  4 ++
 src/cargo/core/compiler/compile_kind.rs       |  3 +-
 .../compiler/context/compilation_files.rs     |  5 ++-
 src/cargo/core/compiler/fingerprint.rs        | 37 ++++++++-----------
 src/cargo/core/compiler/job_queue.rs          |  4 +-
 src/cargo/core/compiler/unit.rs               |  2 +-
 src/cargo/core/manifest.rs                    |  3 +-
 src/cargo/core/source/source_id.rs            |  2 +-
 src/cargo/ops/cargo_compile/mod.rs            |  3 +-
 src/cargo/ops/cargo_package.rs                | 10 ++---
 src/cargo/sources/git/source.rs               |  2 +-
 src/cargo/sources/registry/mod.rs             |  2 +-
 src/cargo/util/mod.rs                         |  4 --
 src/cargo/util/rustc.rs                       |  4 +-
 16 files changed, 41 insertions(+), 44 deletions(-)
 rename {src/cargo/util => crates/cargo-util/src}/hasher.rs (100%)
 rename {src/cargo/util => crates/cargo-util/src}/hex.rs (100%)

diff --git a/src/cargo/util/hasher.rs b/crates/cargo-util/src/hasher.rs
similarity index 100%
rename from src/cargo/util/hasher.rs
rename to crates/cargo-util/src/hasher.rs
diff --git a/src/cargo/util/hex.rs b/crates/cargo-util/src/hex.rs
similarity index 100%
rename from src/cargo/util/hex.rs
rename to crates/cargo-util/src/hex.rs
diff --git a/crates/cargo-util/src/lib.rs b/crates/cargo-util/src/lib.rs
index 0cbc920ecf3b..4d4f588c8aa2 100644
--- a/crates/cargo-util/src/lib.rs
+++ b/crates/cargo-util/src/lib.rs
@@ -1,10 +1,14 @@
 //! Miscellaneous support code used by Cargo.
 
+pub use self::hasher::StableHasher;
+pub use self::hex::{hash_u64, short_hash, to_hex};
 pub use self::read2::read2;
 pub use process_builder::ProcessBuilder;
 pub use process_error::{exit_status_to_string, is_simple_exit_code, ProcessError};
 pub use sha256::Sha256;
 
+mod hasher;
+pub mod hex;
 pub mod paths;
 mod process_builder;
 mod process_error;
diff --git a/src/cargo/core/compiler/compile_kind.rs b/src/cargo/core/compiler/compile_kind.rs
index 7ab19ae1ccf2..99cbbc4fac18 100644
--- a/src/cargo/core/compiler/compile_kind.rs
+++ b/src/cargo/core/compiler/compile_kind.rs
@@ -1,8 +1,9 @@
 use crate::core::Target;
 use crate::util::errors::CargoResult;
 use crate::util::interning::InternedString;
-use crate::util::{Config, StableHasher};
+use crate::util::Config;
 use anyhow::Context as _;
+use cargo_util::StableHasher;
 use serde::Serialize;
 use std::collections::BTreeSet;
 use std::fs;
diff --git a/src/cargo/core/compiler/context/compilation_files.rs b/src/cargo/core/compiler/context/compilation_files.rs
index dcfaabf327a1..b9dd7f66b794 100644
--- a/src/cargo/core/compiler/context/compilation_files.rs
+++ b/src/cargo/core/compiler/context/compilation_files.rs
@@ -5,13 +5,14 @@ use std::hash::{Hash, Hasher};
 use std::path::{Path, PathBuf};
 use std::sync::Arc;
 
+use cargo_util::{short_hash, StableHasher};
 use lazycell::LazyCell;
 use log::debug;
 
 use super::{BuildContext, CompileKind, Context, FileFlavor, Layout};
 use crate::core::compiler::{CompileMode, CompileTarget, CrateType, FileType, Unit};
 use crate::core::{Target, TargetKind, Workspace};
-use crate::util::{self, CargoResult, StableHasher};
+use crate::util::CargoResult;
 
 /// This is a generic version number that can be changed to make
 /// backwards-incompatible changes to any file structures in the output
@@ -185,7 +186,7 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
     /// Used for the metadata when `metadata` returns `None`.
     pub fn target_short_hash(&self, unit: &Unit) -> String {
         let hashable = unit.pkg.package_id().stable_hash(self.ws.root());
-        util::short_hash(&(METADATA_VERSION, hashable))
+        short_hash(&(METADATA_VERSION, hashable))
     }
 
     /// Returns the directory where the artifacts for the given unit are
diff --git a/src/cargo/core/compiler/fingerprint.rs b/src/cargo/core/compiler/fingerprint.rs
index eb78fd8cae7f..4a1b970a64f5 100644
--- a/src/cargo/core/compiler/fingerprint.rs
+++ b/src/cargo/core/compiler/fingerprint.rs
@@ -322,7 +322,7 @@ use std::sync::{Arc, Mutex};
 use std::time::SystemTime;
 
 use anyhow::{bail, format_err, Context as _};
-use cargo_util::{paths, ProcessBuilder};
+use cargo_util::{hash_u64, paths, to_hex, ProcessBuilder, StableHasher};
 use filetime::FileTime;
 use log::{debug, info};
 use serde::de;
@@ -331,10 +331,9 @@ use serde::{Deserialize, Serialize};
 
 use crate::core::compiler::unit_graph::UnitDep;
 use crate::core::Package;
-use crate::util;
 use crate::util::errors::CargoResult;
 use crate::util::interning::InternedString;
-use crate::util::{internal, path_args, profile, StableHasher};
+use crate::util::{internal, path_args, profile};
 use crate::CARGO_ENV;
 
 use super::custom_build::BuildDeps;
@@ -812,7 +811,7 @@ impl Fingerprint {
         if let Some(s) = *self.memoized_hash.lock().unwrap() {
             return s;
         }
-        let ret = util::hash_u64(self);
+        let ret = hash_u64(self);
         *self.memoized_hash.lock().unwrap() = Some(ret);
         ret
     }
@@ -1160,9 +1159,9 @@ impl DepFingerprint {
         // `path` then we just hash the name, but otherwise we hash the full
         // id as it won't change when the directory is renamed.
         let pkg_id = if dep.unit.pkg.package_id().source_id().is_path() {
-            util::hash_u64(dep.unit.pkg.package_id().name())
+            hash_u64(dep.unit.pkg.package_id().name())
         } else {
-            util::hash_u64(dep.unit.pkg.package_id())
+            hash_u64(dep.unit.pkg.package_id())
         };
 
         Ok(DepFingerprint {
@@ -1309,7 +1308,7 @@ fn calculate_normal(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Finger
     }
     .to_vec();
 
-    let profile_hash = util::hash_u64((
+    let profile_hash = hash_u64((
         &unit.profile,
         unit.mode,
         cx.bcx.extra_args_for(unit),
@@ -1317,7 +1316,7 @@ fn calculate_normal(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Finger
     ));
     // Include metadata since it is exposed as environment variables.
     let m = unit.pkg.manifest().metadata();
-    let metadata = util::hash_u64((&m.authors, &m.description, &m.homepage, &m.repository));
+    let metadata = hash_u64((&m.authors, &m.description, &m.homepage, &m.repository));
     let mut config = StableHasher::new();
     if let Some(linker) = cx.bcx.linker(unit.kind) {
         linker.hash(&mut config);
@@ -1332,12 +1331,12 @@ fn calculate_normal(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Finger
     }
     let compile_kind = unit.kind.fingerprint_hash();
     Ok(Fingerprint {
-        rustc: util::hash_u64(&cx.bcx.rustc().verbose_version),
-        target: util::hash_u64(&unit.target),
+        rustc: hash_u64(&cx.bcx.rustc().verbose_version),
+        target: hash_u64(&unit.target),
         profile: profile_hash,
         // Note that .0 is hashed here, not .1 which is the cwd. That doesn't
         // actually affect the output artifact so there's no need to hash it.
-        path: util::hash_u64(path_args(cx.bcx.ws, unit).0),
+        path: hash_u64(path_args(cx.bcx.ws, unit).0),
         features: format!("{:?}", unit.features),
         deps,
         local: Mutex::new(local),
@@ -1402,7 +1401,7 @@ See https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-change
 
     Ok(Fingerprint {
         local: Mutex::new(local),
-        rustc: util::hash_u64(&cx.bcx.rustc().verbose_version),
+        rustc: hash_u64(&cx.bcx.rustc().verbose_version),
         deps,
         outputs: if overridden { Vec::new() } else { vec![output] },
 
@@ -1532,10 +1531,7 @@ fn build_script_override_fingerprint(
     let metadata = cx.get_run_build_script_metadata(unit);
     // Returns None if it is not overridden.
     let output = build_script_outputs.get(metadata)?;
-    let s = format!(
-        "overridden build state with hash: {}",
-        util::hash_u64(output)
-    );
+    let s = format!("overridden build state with hash: {}", hash_u64(output));
     Some(LocalFingerprint::Precalculated(s))
 }
 
@@ -1586,7 +1582,7 @@ fn write_fingerprint(loc: &Path, fingerprint: &Fingerprint) -> CargoResult<()> {
     // as we can use the full hash.
     let hash = fingerprint.hash_u64();
     debug!("write fingerprint ({:x}) : {}", hash, loc.display());
-    paths::write(loc, util::to_hex(hash).as_bytes())?;
+    paths::write(loc, to_hex(hash).as_bytes())?;
 
     let json = serde_json::to_string(fingerprint).unwrap();
     if cfg!(debug_assertions) {
@@ -1637,7 +1633,7 @@ fn compare_old_fingerprint(
 
     let new_hash = new_fingerprint.hash_u64();
 
-    if util::to_hex(new_hash) == old_fingerprint_short && new_fingerprint.fs_status.up_to_date() {
+    if to_hex(new_hash) == old_fingerprint_short && new_fingerprint.fs_status.up_to_date() {
         return Ok(());
     }
 
@@ -1646,10 +1642,7 @@ fn compare_old_fingerprint(
         .with_context(|| internal("failed to deserialize json"))?;
     // Fingerprint can be empty after a failed rebuild (see comment in prepare_target).
     if !old_fingerprint_short.is_empty() {
-        debug_assert_eq!(
-            util::to_hex(old_fingerprint.hash_u64()),
-            old_fingerprint_short
-        );
+        debug_assert_eq!(to_hex(old_fingerprint.hash_u64()), old_fingerprint_short);
     }
     let result = new_fingerprint.compare(&old_fingerprint);
     assert!(result.is_err());
diff --git a/src/cargo/core/compiler/job_queue.rs b/src/cargo/core/compiler/job_queue.rs
index b9ca2249a92b..497a9b020497 100644
--- a/src/cargo/core/compiler/job_queue.rs
+++ b/src/cargo/core/compiler/job_queue.rs
@@ -59,7 +59,7 @@ use std::thread::{self, Scope};
 use std::time::Duration;
 
 use anyhow::{format_err, Context as _};
-use cargo_util::ProcessBuilder;
+use cargo_util::{hash_u64, ProcessBuilder};
 use jobserver::{Acquired, Client, HelperThread};
 use log::{debug, trace};
 use semver::Version;
@@ -312,7 +312,7 @@ impl<'cfg> DiagDedupe<'cfg> {
     /// Returns `true` if the message was emitted, or `false` if it was
     /// suppressed for being a duplicate.
     fn emit_diag(&self, diag: &str) -> CargoResult<bool> {
-        let h = util::hash_u64(diag);
+        let h = hash_u64(diag);
         if !self.seen.borrow_mut().insert(h) {
             return Ok(false);
         }
diff --git a/src/cargo/core/compiler/unit.rs b/src/cargo/core/compiler/unit.rs
index 32a98cb8cd70..ade8e62f8d61 100644
--- a/src/cargo/core/compiler/unit.rs
+++ b/src/cargo/core/compiler/unit.rs
@@ -1,9 +1,9 @@
 use crate::core::compiler::{unit_dependencies::IsArtifact, CompileKind, CompileMode, CrateType};
 use crate::core::manifest::{Target, TargetKind};
 use crate::core::{profiles::Profile, Package};
-use crate::util::hex::short_hash;
 use crate::util::interning::InternedString;
 use crate::util::Config;
+use cargo_util::hex::short_hash;
 use std::cell::RefCell;
 use std::collections::HashSet;
 use std::fmt;
diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs
index c37dd1cb4558..a80b8999b9cc 100644
--- a/src/cargo/core/manifest.rs
+++ b/src/cargo/core/manifest.rs
@@ -20,7 +20,8 @@ use crate::core::{Edition, Feature, Features, WorkspaceConfig};
 use crate::util::errors::*;
 use crate::util::interning::InternedString;
 use crate::util::toml::{TomlManifest, TomlProfiles};
-use crate::util::{short_hash, Config, Filesystem};
+use crate::util::{Config, Filesystem};
+use cargo_util::hex::short_hash;
 
 pub enum EitherManifest {
     Real(Manifest),
diff --git a/src/cargo/core/source/source_id.rs b/src/cargo/core/source/source_id.rs
index bb43843b5c03..c8c666b07c21 100644
--- a/src/cargo/core/source/source_id.rs
+++ b/src/cargo/core/source/source_id.rs
@@ -712,7 +712,7 @@ impl Ord for SourceKind {
 fn test_cratesio_hash() {
     let config = Config::default().unwrap();
     let crates_io = SourceId::crates_io(&config).unwrap();
-    assert_eq!(crate::util::hex::short_hash(&crates_io), "1ecc6299db9ec823");
+    assert_eq!(cargo_util::hex::short_hash(&crates_io), "1ecc6299db9ec823");
 }
 
 /// A `Display`able view into a `SourceId` that will write it as a url
diff --git a/src/cargo/ops/cargo_compile/mod.rs b/src/cargo/ops/cargo_compile/mod.rs
index 622255f893c2..30ca48216d2e 100644
--- a/src/cargo/ops/cargo_compile/mod.rs
+++ b/src/cargo/ops/cargo_compile/mod.rs
@@ -52,7 +52,8 @@ use crate::ops::resolve::WorkspaceResolve;
 use crate::util::config::Config;
 use crate::util::interning::InternedString;
 use crate::util::restricted_names::is_glob_pattern;
-use crate::util::{closest_msg, profile, CargoResult, StableHasher};
+use crate::util::{closest_msg, profile, CargoResult};
+use cargo_util::StableHasher;
 
 mod compile_filter;
 pub use compile_filter::{CompileFilter, FilterRule, LibRule};
diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs
index ae5c7558fc4c..061320987d7f 100644
--- a/src/cargo/ops/cargo_package.rs
+++ b/src/cargo/ops/cargo_package.rs
@@ -14,10 +14,10 @@ use crate::core::{Package, PackageId, PackageSet, Resolve, SourceId};
 use crate::sources::PathSource;
 use crate::util::errors::CargoResult;
 use crate::util::toml::TomlManifest;
-use crate::util::{self, human_readable_bytes, restricted_names, Config, FileLock};
+use crate::util::{human_readable_bytes, restricted_names, Config, FileLock};
 use crate::{drop_println, ops};
 use anyhow::Context as _;
-use cargo_util::paths;
+use cargo_util::{hex, paths};
 use flate2::read::GzDecoder;
 use flate2::{Compression, GzBuilder};
 use log::debug;
@@ -885,13 +885,13 @@ fn hash_all(path: &Path) -> CargoResult<HashMap<PathBuf, u64>> {
             let file_type = entry.file_type();
             if file_type.is_file() {
                 let file = File::open(entry.path())?;
-                let hash = util::hex::hash_u64_file(&file)?;
+                let hash = hex::hash_u64_file(&file)?;
                 result.insert(entry.path().to_path_buf(), hash);
             } else if file_type.is_symlink() {
-                let hash = util::hex::hash_u64(&fs::read_link(entry.path())?);
+                let hash = hex::hash_u64(&fs::read_link(entry.path())?);
                 result.insert(entry.path().to_path_buf(), hash);
             } else if file_type.is_dir() {
-                let hash = util::hex::hash_u64(&());
+                let hash = hex::hash_u64(&());
                 result.insert(entry.path().to_path_buf(), hash);
             }
         }
diff --git a/src/cargo/sources/git/source.rs b/src/cargo/sources/git/source.rs
index d09d52716270..d544cf2791a3 100644
--- a/src/cargo/sources/git/source.rs
+++ b/src/cargo/sources/git/source.rs
@@ -4,9 +4,9 @@ use crate::core::{Dependency, Package, PackageId, Summary};
 use crate::sources::git::utils::GitRemote;
 use crate::sources::PathSource;
 use crate::util::errors::CargoResult;
-use crate::util::hex::short_hash;
 use crate::util::Config;
 use anyhow::Context;
+use cargo_util::hex::short_hash;
 use cargo_util::paths::exclude_from_backups_and_indexing;
 use log::trace;
 use std::fmt::{self, Debug, Formatter};
diff --git a/src/cargo/sources/registry/mod.rs b/src/cargo/sources/registry/mod.rs
index 8fd1a3b86c69..9951a6ff8a10 100644
--- a/src/cargo/sources/registry/mod.rs
+++ b/src/cargo/sources/registry/mod.rs
@@ -167,6 +167,7 @@ use std::path::{Path, PathBuf};
 use std::task::Poll;
 
 use anyhow::Context as _;
+use cargo_util::hex;
 use cargo_util::paths::exclude_from_backups_and_indexing;
 use flate2::read::GzDecoder;
 use log::debug;
@@ -178,7 +179,6 @@ use crate::core::dependency::{DepKind, Dependency};
 use crate::core::source::MaybePackage;
 use crate::core::{Package, PackageId, QueryKind, Source, SourceId, Summary};
 use crate::sources::PathSource;
-use crate::util::hex;
 use crate::util::interning::InternedString;
 use crate::util::into_url::IntoUrl;
 use crate::util::network::PollExt;
diff --git a/src/cargo/util/mod.rs b/src/cargo/util/mod.rs
index ccd6d59a4d12..de9f3abe98c3 100644
--- a/src/cargo/util/mod.rs
+++ b/src/cargo/util/mod.rs
@@ -10,8 +10,6 @@ pub use self::errors::CliError;
 pub use self::errors::{internal, CargoResult, CliResult};
 pub use self::flock::{FileLock, Filesystem};
 pub use self::graph::Graph;
-pub use self::hasher::StableHasher;
-pub use self::hex::{hash_u64, short_hash, to_hex};
 pub use self::into_url::IntoUrl;
 pub use self::into_url_with_base::IntoUrlWithBase;
 pub(crate) use self::io::LimitErrorReader;
@@ -40,8 +38,6 @@ pub mod diagnostic_server;
 pub mod errors;
 mod flock;
 pub mod graph;
-mod hasher;
-pub mod hex;
 pub mod important_paths;
 pub mod interning;
 pub mod into_url;
diff --git a/src/cargo/util/rustc.rs b/src/cargo/util/rustc.rs
index 6073ffe0b60c..99fd0de91ba5 100644
--- a/src/cargo/util/rustc.rs
+++ b/src/cargo/util/rustc.rs
@@ -5,12 +5,12 @@ use std::path::{Path, PathBuf};
 use std::sync::Mutex;
 
 use anyhow::Context as _;
-use cargo_util::{paths, ProcessBuilder, ProcessError};
+use cargo_util::{paths, ProcessBuilder, ProcessError, StableHasher};
 use log::{debug, info, warn};
 use serde::{Deserialize, Serialize};
 
 use crate::util::interning::InternedString;
-use crate::util::{profile, CargoResult, StableHasher};
+use crate::util::{profile, CargoResult};
 
 /// Information on the `rustc` executable
 #[derive(Debug)]