Skip to content

Commit 22c091c

Browse files
committed
Auto merge of #8233 - ehuss:siphasher, r=alexcrichton
Move SipHasher to an isolated module. This allows removing the blanket `#![allow(deprecated)]` sprinkled whenever it is used. We could alternatively use the [siphasher](https://crates.io/crates/siphasher) crate, but I don't think it is necessary at this time.
2 parents cb06cb2 + 67b10f7 commit 22c091c

File tree

6 files changed

+38
-16
lines changed

6 files changed

+38
-16
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22
use std::env;
33
use std::fmt;
4-
use std::hash::{Hash, Hasher, SipHasher};
4+
use std::hash::{Hash, Hasher};
55
use std::path::{Path, PathBuf};
66
use std::sync::Arc;
77

@@ -11,7 +11,7 @@ use log::info;
1111
use super::{BuildContext, CompileKind, Context, FileFlavor, Layout};
1212
use crate::core::compiler::{CompileMode, CompileTarget, CrateType, FileType, Unit};
1313
use crate::core::{Target, TargetKind, Workspace};
14-
use crate::util::{self, CargoResult};
14+
use crate::util::{self, CargoResult, StableHasher};
1515

1616
/// The `Metadata` is a hash used to make unique file names for each unit in a
1717
/// build. It is also use for symbol mangling.
@@ -481,7 +481,7 @@ fn compute_metadata(
481481
if !should_use_metadata(bcx, unit) {
482482
return None;
483483
}
484-
let mut hasher = SipHasher::new();
484+
let mut hasher = StableHasher::new();
485485

486486
// This is a generic version number that can be changed to make
487487
// backwards-incompatible changes to any file structures in the output
@@ -556,7 +556,7 @@ fn compute_metadata(
556556
Some(Metadata(hasher.finish()))
557557
}
558558

559-
fn hash_rustc_version(bcx: &BuildContext<'_, '_>, hasher: &mut SipHasher) {
559+
fn hash_rustc_version(bcx: &BuildContext<'_, '_>, hasher: &mut StableHasher) {
560560
let vers = &bcx.rustc().version;
561561
if vers.pre.is_empty() || bcx.config.cli_unstable().separate_nightlies {
562562
// For stable, keep the artifacts separate. This helps if someone is

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![allow(deprecated)]
21
use std::collections::{BTreeSet, HashMap, HashSet};
32
use std::path::PathBuf;
43
use std::sync::{Arc, Mutex};

src/cargo/util/hasher.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//! Implementation of a hasher that produces the same values across releases.
2+
//!
3+
//! The hasher should be fast and have a low chance of collisions (but is not
4+
//! sufficient for cryptographic purposes).
5+
#![allow(deprecated)]
6+
7+
use std::hash::{Hasher, SipHasher};
8+
9+
pub struct StableHasher(SipHasher);
10+
11+
impl StableHasher {
12+
pub fn new() -> StableHasher {
13+
StableHasher(SipHasher::new())
14+
}
15+
}
16+
17+
impl Hasher for StableHasher {
18+
fn finish(&self) -> u64 {
19+
self.0.finish()
20+
}
21+
fn write(&mut self, bytes: &[u8]) {
22+
self.0.write(bytes)
23+
}
24+
}

src/cargo/util/hex.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#![allow(deprecated)]
2-
1+
use super::StableHasher;
32
use std::fs::File;
4-
use std::hash::{Hash, Hasher, SipHasher};
3+
use std::hash::{Hash, Hasher};
54
use std::io::Read;
65

76
pub fn to_hex(num: u64) -> String {
@@ -18,13 +17,13 @@ pub fn to_hex(num: u64) -> String {
1817
}
1918

2019
pub fn hash_u64<H: Hash>(hashable: H) -> u64 {
21-
let mut hasher = SipHasher::new();
20+
let mut hasher = StableHasher::new();
2221
hashable.hash(&mut hasher);
2322
hasher.finish()
2423
}
2524

2625
pub fn hash_u64_file(mut file: &File) -> std::io::Result<u64> {
27-
let mut hasher = SipHasher::new_with_keys(0, 0);
26+
let mut hasher = StableHasher::new();
2827
let mut buf = [0; 64 * 1024];
2928
loop {
3029
let n = file.read(&mut buf)?;

src/cargo/util/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub use self::errors::{CargoResult, CargoResultExt, CliResult, Test};
99
pub use self::errors::{CargoTestError, CliError, ProcessError};
1010
pub use self::flock::{FileLock, Filesystem};
1111
pub use self::graph::Graph;
12+
pub use self::hasher::StableHasher;
1213
pub use self::hex::{hash_u64, short_hash, to_hex};
1314
pub use self::into_url::IntoUrl;
1415
pub use self::into_url_with_base::IntoUrlWithBase;
@@ -39,6 +40,7 @@ pub mod diagnostic_server;
3940
pub mod errors;
4041
mod flock;
4142
pub mod graph;
43+
mod hasher;
4244
pub mod hex;
4345
pub mod important_paths;
4446
pub mod into_url;

src/cargo/util/rustc.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
#![allow(deprecated)] // for SipHasher
2-
31
use std::collections::hash_map::{Entry, HashMap};
42
use std::env;
5-
use std::hash::{Hash, Hasher, SipHasher};
3+
use std::hash::{Hash, Hasher};
64
use std::path::{Path, PathBuf};
75
use std::sync::Mutex;
86

@@ -11,7 +9,7 @@ use serde::{Deserialize, Serialize};
119

1210
use crate::core::InternedString;
1311
use crate::util::paths;
14-
use crate::util::{self, profile, CargoResult, CargoResultExt, ProcessBuilder};
12+
use crate::util::{self, profile, CargoResult, CargoResultExt, ProcessBuilder, StableHasher};
1513

1614
/// Information on the `rustc` executable
1715
#[derive(Debug)]
@@ -222,7 +220,7 @@ impl Drop for Cache {
222220
}
223221

224222
fn rustc_fingerprint(path: &Path, rustup_rustc: &Path) -> CargoResult<u64> {
225-
let mut hasher = SipHasher::new();
223+
let mut hasher = StableHasher::new();
226224

227225
let path = paths::resolve_executable(path)?;
228226
path.hash(&mut hasher);
@@ -266,7 +264,7 @@ fn rustc_fingerprint(path: &Path, rustup_rustc: &Path) -> CargoResult<u64> {
266264
}
267265

268266
fn process_fingerprint(cmd: &ProcessBuilder) -> u64 {
269-
let mut hasher = SipHasher::new();
267+
let mut hasher = StableHasher::new();
270268
cmd.get_args().hash(&mut hasher);
271269
let mut env = cmd.get_envs().iter().collect::<Vec<_>>();
272270
env.sort_unstable();

0 commit comments

Comments
 (0)