From 6c9d20f50a8353caf9a5c2086235a70e8cfef5da Mon Sep 17 00:00:00 2001 From: Nick Cameron 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 --- Cargo.toml | 2 +- crates/cargo-util/Cargo.toml | 2 +- .../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 +- tests/testsuite/registry.rs | 4 +- 19 files changed, 45 insertions(+), 48 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/Cargo.toml b/Cargo.toml index d3352269b62..b1dc5c99246 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ path = "src/cargo/lib.rs" [dependencies] bytesize = "1.0" cargo-platform = { path = "crates/cargo-platform", version = "0.1.2" } -cargo-util = { path = "crates/cargo-util", version = "0.2.3" } +cargo-util = { path = "crates/cargo-util", version = "0.2.4" } crates-io = { path = "crates/crates-io", version = "0.35.0" } curl = { version = "0.4.44", features = ["http2"] } curl-sys = "0.4.59" diff --git a/crates/cargo-util/Cargo.toml b/crates/cargo-util/Cargo.toml index ff47587b3c7..a69c7d11fde 100644 --- a/crates/cargo-util/Cargo.toml +++ b/crates/cargo-util/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cargo-util" -version = "0.2.3" +version = "0.2.4" edition = "2021" license = "MIT OR Apache-2.0" homepage = "https://github.com/rust-lang/cargo" 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 0cbc920ecf3..4d4f588c8aa 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 7ab19ae1ccf..99cbbc4fac1 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 dcfaabf327a..b9dd7f66b79 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 eb78fd8cae7..4a1b970a64f 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, unit: &Unit) -> CargoResult, unit: &Unit) -> CargoResult 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 b9ca2249a92..497a9b02049 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 { - 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 32a98cb8cd7..ade8e62f8d6 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 c37dd1cb455..a80b8999b9c 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 bb43843b5c0..c8c666b07c2 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 622255f893c..30ca48216d2 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 ae5c7558fc4..061320987d7 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> { 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 d09d5271627..d544cf2791a 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 8fd1a3b86c6..9951a6ff8a1 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 ccd6d59a4d1..de9f3abe98c 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 6073ffe0b60..99fd0de91ba 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)] diff --git a/tests/testsuite/registry.rs b/tests/testsuite/registry.rs index fe59c4dd69f..eea3fa189d5 100644 --- a/tests/testsuite/registry.rs +++ b/tests/testsuite/registry.rs @@ -2593,7 +2593,7 @@ fn package_lock_inside_package_is_overwritten() { p.cargo("build").run(); let id = SourceId::for_registry(registry.index_url()).unwrap(); - let hash = cargo::util::hex::short_hash(&id); + let hash = cargo_util::hex::short_hash(&id); let ok = cargo_home() .join("registry") .join("src") @@ -2631,7 +2631,7 @@ fn package_lock_as_a_symlink_inside_package_is_overwritten() { p.cargo("build").run(); let id = SourceId::for_registry(registry.index_url()).unwrap(); - let hash = cargo::util::hex::short_hash(&id); + let hash = cargo_util::hex::short_hash(&id); let pkg_root = cargo_home() .join("registry") .join("src")