Skip to content

Commit 93734ea

Browse files
committed
Refactoring: move hex and hasher modules from util module to util crate
Signed-off-by: Nick Cameron <[email protected]>
1 parent 1382b44 commit 93734ea

File tree

16 files changed

+41
-44
lines changed

16 files changed

+41
-44
lines changed
File renamed without changes.
File renamed without changes.

crates/cargo-util/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
//! Miscellaneous support code used by Cargo.
22
3+
pub use self::hasher::StableHasher;
4+
pub use self::hex::{hash_u64, short_hash, to_hex};
35
pub use self::read2::read2;
46
pub use process_builder::ProcessBuilder;
57
pub use process_error::{exit_status_to_string, is_simple_exit_code, ProcessError};
68
pub use sha256::Sha256;
79

10+
mod hasher;
11+
pub mod hex;
812
pub mod paths;
913
mod process_builder;
1014
mod process_error;

src/cargo/core/compiler/compile_kind.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::core::Target;
22
use crate::util::errors::CargoResult;
33
use crate::util::interning::InternedString;
4-
use crate::util::{Config, StableHasher};
4+
use crate::util::Config;
55
use anyhow::Context as _;
6+
use cargo_util::StableHasher;
67
use serde::Serialize;
78
use std::collections::BTreeSet;
89
use std::fs;

src/cargo/core/compiler/context/compilation_files.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ use std::hash::{Hash, Hasher};
55
use std::path::{Path, PathBuf};
66
use std::sync::Arc;
77

8+
use cargo_util::{short_hash, StableHasher};
89
use lazycell::LazyCell;
910
use log::debug;
1011

1112
use super::{BuildContext, CompileKind, Context, FileFlavor, Layout};
1213
use crate::core::compiler::{CompileMode, CompileTarget, CrateType, FileType, Unit};
1314
use crate::core::{Target, TargetKind, Workspace};
14-
use crate::util::{self, CargoResult, StableHasher};
15+
use crate::util::CargoResult;
1516

1617
/// This is a generic version number that can be changed to make
1718
/// backwards-incompatible changes to any file structures in the output
@@ -185,7 +186,7 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
185186
/// Used for the metadata when `metadata` returns `None`.
186187
pub fn target_short_hash(&self, unit: &Unit) -> String {
187188
let hashable = unit.pkg.package_id().stable_hash(self.ws.root());
188-
util::short_hash(&(METADATA_VERSION, hashable))
189+
short_hash(&(METADATA_VERSION, hashable))
189190
}
190191

191192
/// Returns the directory where the artifacts for the given unit are

src/cargo/core/compiler/fingerprint.rs

+15-22
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ use std::sync::{Arc, Mutex};
322322
use std::time::SystemTime;
323323

324324
use anyhow::{bail, format_err, Context as _};
325-
use cargo_util::{paths, ProcessBuilder};
325+
use cargo_util::{hash_u64, paths, to_hex, ProcessBuilder, StableHasher};
326326
use filetime::FileTime;
327327
use log::{debug, info};
328328
use serde::de;
@@ -331,10 +331,9 @@ use serde::{Deserialize, Serialize};
331331

332332
use crate::core::compiler::unit_graph::UnitDep;
333333
use crate::core::Package;
334-
use crate::util;
335334
use crate::util::errors::CargoResult;
336335
use crate::util::interning::InternedString;
337-
use crate::util::{internal, path_args, profile, StableHasher};
336+
use crate::util::{internal, path_args, profile};
338337
use crate::CARGO_ENV;
339338

340339
use super::custom_build::BuildDeps;
@@ -812,7 +811,7 @@ impl Fingerprint {
812811
if let Some(s) = *self.memoized_hash.lock().unwrap() {
813812
return s;
814813
}
815-
let ret = util::hash_u64(self);
814+
let ret = hash_u64(self);
816815
*self.memoized_hash.lock().unwrap() = Some(ret);
817816
ret
818817
}
@@ -1160,9 +1159,9 @@ impl DepFingerprint {
11601159
// `path` then we just hash the name, but otherwise we hash the full
11611160
// id as it won't change when the directory is renamed.
11621161
let pkg_id = if dep.unit.pkg.package_id().source_id().is_path() {
1163-
util::hash_u64(dep.unit.pkg.package_id().name())
1162+
hash_u64(dep.unit.pkg.package_id().name())
11641163
} else {
1165-
util::hash_u64(dep.unit.pkg.package_id())
1164+
hash_u64(dep.unit.pkg.package_id())
11661165
};
11671166

11681167
Ok(DepFingerprint {
@@ -1309,15 +1308,15 @@ fn calculate_normal(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Finger
13091308
}
13101309
.to_vec();
13111310

1312-
let profile_hash = util::hash_u64((
1311+
let profile_hash = hash_u64((
13131312
&unit.profile,
13141313
unit.mode,
13151314
cx.bcx.extra_args_for(unit),
13161315
cx.lto[unit],
13171316
));
13181317
// Include metadata since it is exposed as environment variables.
13191318
let m = unit.pkg.manifest().metadata();
1320-
let metadata = util::hash_u64((&m.authors, &m.description, &m.homepage, &m.repository));
1319+
let metadata = hash_u64((&m.authors, &m.description, &m.homepage, &m.repository));
13211320
let mut config = StableHasher::new();
13221321
if let Some(linker) = cx.bcx.linker(unit.kind) {
13231322
linker.hash(&mut config);
@@ -1332,12 +1331,12 @@ fn calculate_normal(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Finger
13321331
}
13331332
let compile_kind = unit.kind.fingerprint_hash();
13341333
Ok(Fingerprint {
1335-
rustc: util::hash_u64(&cx.bcx.rustc().verbose_version),
1336-
target: util::hash_u64(&unit.target),
1334+
rustc: hash_u64(&cx.bcx.rustc().verbose_version),
1335+
target: hash_u64(&unit.target),
13371336
profile: profile_hash,
13381337
// Note that .0 is hashed here, not .1 which is the cwd. That doesn't
13391338
// actually affect the output artifact so there's no need to hash it.
1340-
path: util::hash_u64(path_args(cx.bcx.ws, unit).0),
1339+
path: hash_u64(path_args(cx.bcx.ws, unit).0),
13411340
features: format!("{:?}", unit.features),
13421341
deps,
13431342
local: Mutex::new(local),
@@ -1402,7 +1401,7 @@ See https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-change
14021401

14031402
Ok(Fingerprint {
14041403
local: Mutex::new(local),
1405-
rustc: util::hash_u64(&cx.bcx.rustc().verbose_version),
1404+
rustc: hash_u64(&cx.bcx.rustc().verbose_version),
14061405
deps,
14071406
outputs: if overridden { Vec::new() } else { vec![output] },
14081407

@@ -1532,10 +1531,7 @@ fn build_script_override_fingerprint(
15321531
let metadata = cx.get_run_build_script_metadata(unit);
15331532
// Returns None if it is not overridden.
15341533
let output = build_script_outputs.get(metadata)?;
1535-
let s = format!(
1536-
"overridden build state with hash: {}",
1537-
util::hash_u64(output)
1538-
);
1534+
let s = format!("overridden build state with hash: {}", hash_u64(output));
15391535
Some(LocalFingerprint::Precalculated(s))
15401536
}
15411537

@@ -1586,7 +1582,7 @@ fn write_fingerprint(loc: &Path, fingerprint: &Fingerprint) -> CargoResult<()> {
15861582
// as we can use the full hash.
15871583
let hash = fingerprint.hash_u64();
15881584
debug!("write fingerprint ({:x}) : {}", hash, loc.display());
1589-
paths::write(loc, util::to_hex(hash).as_bytes())?;
1585+
paths::write(loc, to_hex(hash).as_bytes())?;
15901586

15911587
let json = serde_json::to_string(fingerprint).unwrap();
15921588
if cfg!(debug_assertions) {
@@ -1637,7 +1633,7 @@ fn compare_old_fingerprint(
16371633

16381634
let new_hash = new_fingerprint.hash_u64();
16391635

1640-
if util::to_hex(new_hash) == old_fingerprint_short && new_fingerprint.fs_status.up_to_date() {
1636+
if to_hex(new_hash) == old_fingerprint_short && new_fingerprint.fs_status.up_to_date() {
16411637
return Ok(());
16421638
}
16431639

@@ -1646,10 +1642,7 @@ fn compare_old_fingerprint(
16461642
.with_context(|| internal("failed to deserialize json"))?;
16471643
// Fingerprint can be empty after a failed rebuild (see comment in prepare_target).
16481644
if !old_fingerprint_short.is_empty() {
1649-
debug_assert_eq!(
1650-
util::to_hex(old_fingerprint.hash_u64()),
1651-
old_fingerprint_short
1652-
);
1645+
debug_assert_eq!(to_hex(old_fingerprint.hash_u64()), old_fingerprint_short);
16531646
}
16541647
let result = new_fingerprint.compare(&old_fingerprint);
16551648
assert!(result.is_err());

src/cargo/core/compiler/job_queue.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use std::thread::{self, Scope};
5959
use std::time::Duration;
6060

6161
use anyhow::{format_err, Context as _};
62-
use cargo_util::ProcessBuilder;
62+
use cargo_util::{hash_u64, ProcessBuilder};
6363
use jobserver::{Acquired, Client, HelperThread};
6464
use log::{debug, trace};
6565
use semver::Version;
@@ -312,7 +312,7 @@ impl<'cfg> DiagDedupe<'cfg> {
312312
/// Returns `true` if the message was emitted, or `false` if it was
313313
/// suppressed for being a duplicate.
314314
fn emit_diag(&self, diag: &str) -> CargoResult<bool> {
315-
let h = util::hash_u64(diag);
315+
let h = hash_u64(diag);
316316
if !self.seen.borrow_mut().insert(h) {
317317
return Ok(false);
318318
}

src/cargo/core/compiler/unit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::core::compiler::{unit_dependencies::IsArtifact, CompileKind, CompileMode, CrateType};
22
use crate::core::manifest::{Target, TargetKind};
33
use crate::core::{profiles::Profile, Package};
4-
use crate::util::hex::short_hash;
54
use crate::util::interning::InternedString;
65
use crate::util::Config;
6+
use cargo_util::hex::short_hash;
77
use std::cell::RefCell;
88
use std::collections::HashSet;
99
use std::fmt;

src/cargo/core/manifest.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ use crate::core::{Edition, Feature, Features, WorkspaceConfig};
2020
use crate::util::errors::*;
2121
use crate::util::interning::InternedString;
2222
use crate::util::toml::{TomlManifest, TomlProfiles};
23-
use crate::util::{short_hash, Config, Filesystem};
23+
use crate::util::{Config, Filesystem};
24+
use cargo_util::hex::short_hash;
2425

2526
pub enum EitherManifest {
2627
Real(Manifest),

src/cargo/core/source/source_id.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ impl Ord for SourceKind {
712712
fn test_cratesio_hash() {
713713
let config = Config::default().unwrap();
714714
let crates_io = SourceId::crates_io(&config).unwrap();
715-
assert_eq!(crate::util::hex::short_hash(&crates_io), "1ecc6299db9ec823");
715+
assert_eq!(cargo_util::hex::short_hash(&crates_io), "1ecc6299db9ec823");
716716
}
717717

718718
/// A `Display`able view into a `SourceId` that will write it as a url

src/cargo/ops/cargo_compile/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ use crate::ops::resolve::WorkspaceResolve;
5252
use crate::util::config::Config;
5353
use crate::util::interning::InternedString;
5454
use crate::util::restricted_names::is_glob_pattern;
55-
use crate::util::{closest_msg, profile, CargoResult, StableHasher};
55+
use crate::util::{closest_msg, profile, CargoResult};
56+
use cargo_util::StableHasher;
5657

5758
mod compile_filter;
5859
pub use compile_filter::{CompileFilter, FilterRule, LibRule};

src/cargo/ops/cargo_package.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ use crate::core::{Package, PackageId, PackageSet, Resolve, SourceId};
1414
use crate::sources::PathSource;
1515
use crate::util::errors::CargoResult;
1616
use crate::util::toml::TomlManifest;
17-
use crate::util::{self, human_readable_bytes, restricted_names, Config, FileLock};
17+
use crate::util::{human_readable_bytes, restricted_names, Config, FileLock};
1818
use crate::{drop_println, ops};
1919
use anyhow::Context as _;
20-
use cargo_util::paths;
20+
use cargo_util::{hex, paths};
2121
use flate2::read::GzDecoder;
2222
use flate2::{Compression, GzBuilder};
2323
use log::debug;
@@ -885,13 +885,13 @@ fn hash_all(path: &Path) -> CargoResult<HashMap<PathBuf, u64>> {
885885
let file_type = entry.file_type();
886886
if file_type.is_file() {
887887
let file = File::open(entry.path())?;
888-
let hash = util::hex::hash_u64_file(&file)?;
888+
let hash = hex::hash_u64_file(&file)?;
889889
result.insert(entry.path().to_path_buf(), hash);
890890
} else if file_type.is_symlink() {
891-
let hash = util::hex::hash_u64(&fs::read_link(entry.path())?);
891+
let hash = hex::hash_u64(&fs::read_link(entry.path())?);
892892
result.insert(entry.path().to_path_buf(), hash);
893893
} else if file_type.is_dir() {
894-
let hash = util::hex::hash_u64(&());
894+
let hash = hex::hash_u64(&());
895895
result.insert(entry.path().to_path_buf(), hash);
896896
}
897897
}

src/cargo/sources/git/source.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use crate::core::{Dependency, Package, PackageId, Summary};
44
use crate::sources::git::utils::GitRemote;
55
use crate::sources::PathSource;
66
use crate::util::errors::CargoResult;
7-
use crate::util::hex::short_hash;
87
use crate::util::Config;
98
use anyhow::Context;
9+
use cargo_util::hex::short_hash;
1010
use cargo_util::paths::exclude_from_backups_and_indexing;
1111
use log::trace;
1212
use std::fmt::{self, Debug, Formatter};

src/cargo/sources/registry/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ use std::path::{Path, PathBuf};
167167
use std::task::Poll;
168168

169169
use anyhow::Context as _;
170+
use cargo_util::hex;
170171
use cargo_util::paths::exclude_from_backups_and_indexing;
171172
use flate2::read::GzDecoder;
172173
use log::debug;
@@ -178,7 +179,6 @@ use crate::core::dependency::{DepKind, Dependency};
178179
use crate::core::source::MaybePackage;
179180
use crate::core::{Package, PackageId, QueryKind, Source, SourceId, Summary};
180181
use crate::sources::PathSource;
181-
use crate::util::hex;
182182
use crate::util::interning::InternedString;
183183
use crate::util::into_url::IntoUrl;
184184
use crate::util::network::PollExt;

src/cargo/util/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ pub use self::errors::CliError;
1010
pub use self::errors::{internal, CargoResult, CliResult};
1111
pub use self::flock::{FileLock, Filesystem};
1212
pub use self::graph::Graph;
13-
pub use self::hasher::StableHasher;
14-
pub use self::hex::{hash_u64, short_hash, to_hex};
1513
pub use self::into_url::IntoUrl;
1614
pub use self::into_url_with_base::IntoUrlWithBase;
1715
pub(crate) use self::io::LimitErrorReader;
@@ -40,8 +38,6 @@ pub mod diagnostic_server;
4038
pub mod errors;
4139
mod flock;
4240
pub mod graph;
43-
mod hasher;
44-
pub mod hex;
4541
pub mod important_paths;
4642
pub mod interning;
4743
pub mod into_url;

src/cargo/util/rustc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use std::path::{Path, PathBuf};
55
use std::sync::Mutex;
66

77
use anyhow::Context as _;
8-
use cargo_util::{paths, ProcessBuilder, ProcessError};
8+
use cargo_util::{paths, ProcessBuilder, ProcessError, StableHasher};
99
use log::{debug, info, warn};
1010
use serde::{Deserialize, Serialize};
1111

1212
use crate::util::interning::InternedString;
13-
use crate::util::{profile, CargoResult, StableHasher};
13+
use crate::util::{profile, CargoResult};
1414

1515
/// Information on the `rustc` executable
1616
#[derive(Debug)]

0 commit comments

Comments
 (0)