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.
This commit is contained in:
bors 2020-05-11 20:54:49 +00:00
commit 22c091c7b4
6 changed files with 38 additions and 16 deletions

View File

@ -1,7 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
use std::fmt; use std::fmt;
use std::hash::{Hash, Hasher, SipHasher}; use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::Arc; use std::sync::Arc;
@ -11,7 +11,7 @@ use log::info;
use super::{BuildContext, CompileKind, Context, FileFlavor, Layout}; use super::{BuildContext, CompileKind, Context, FileFlavor, Layout};
use crate::core::compiler::{CompileMode, CompileTarget, CrateType, FileType, Unit}; use crate::core::compiler::{CompileMode, CompileTarget, CrateType, FileType, Unit};
use crate::core::{Target, TargetKind, Workspace}; use crate::core::{Target, TargetKind, Workspace};
use crate::util::{self, CargoResult}; use crate::util::{self, CargoResult, StableHasher};
/// The `Metadata` is a hash used to make unique file names for each unit in a /// The `Metadata` is a hash used to make unique file names for each unit in a
/// build. It is also use for symbol mangling. /// build. It is also use for symbol mangling.
@ -481,7 +481,7 @@ fn compute_metadata(
if !should_use_metadata(bcx, unit) { if !should_use_metadata(bcx, unit) {
return None; return None;
} }
let mut hasher = SipHasher::new(); let mut hasher = StableHasher::new();
// This is a generic version number that can be changed to make // This is a generic version number that can be changed to make
// backwards-incompatible changes to any file structures in the output // backwards-incompatible changes to any file structures in the output
@ -556,7 +556,7 @@ fn compute_metadata(
Some(Metadata(hasher.finish())) Some(Metadata(hasher.finish()))
} }
fn hash_rustc_version(bcx: &BuildContext<'_, '_>, hasher: &mut SipHasher) { fn hash_rustc_version(bcx: &BuildContext<'_, '_>, hasher: &mut StableHasher) {
let vers = &bcx.rustc().version; let vers = &bcx.rustc().version;
if vers.pre.is_empty() || bcx.config.cli_unstable().separate_nightlies { if vers.pre.is_empty() || bcx.config.cli_unstable().separate_nightlies {
// For stable, keep the artifacts separate. This helps if someone is // For stable, keep the artifacts separate. This helps if someone is

View File

@ -1,4 +1,3 @@
#![allow(deprecated)]
use std::collections::{BTreeSet, HashMap, HashSet}; use std::collections::{BTreeSet, HashMap, HashSet};
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};

24
src/cargo/util/hasher.rs Normal file
View File

@ -0,0 +1,24 @@
//! Implementation of a hasher that produces the same values across releases.
//!
//! The hasher should be fast and have a low chance of collisions (but is not
//! sufficient for cryptographic purposes).
#![allow(deprecated)]
use std::hash::{Hasher, SipHasher};
pub struct StableHasher(SipHasher);
impl StableHasher {
pub fn new() -> StableHasher {
StableHasher(SipHasher::new())
}
}
impl Hasher for StableHasher {
fn finish(&self) -> u64 {
self.0.finish()
}
fn write(&mut self, bytes: &[u8]) {
self.0.write(bytes)
}
}

View File

@ -1,7 +1,6 @@
#![allow(deprecated)] use super::StableHasher;
use std::fs::File; use std::fs::File;
use std::hash::{Hash, Hasher, SipHasher}; use std::hash::{Hash, Hasher};
use std::io::Read; use std::io::Read;
pub fn to_hex(num: u64) -> String { pub fn to_hex(num: u64) -> String {
@ -18,13 +17,13 @@ pub fn to_hex(num: u64) -> String {
} }
pub fn hash_u64<H: Hash>(hashable: H) -> u64 { pub fn hash_u64<H: Hash>(hashable: H) -> u64 {
let mut hasher = SipHasher::new(); let mut hasher = StableHasher::new();
hashable.hash(&mut hasher); hashable.hash(&mut hasher);
hasher.finish() hasher.finish()
} }
pub fn hash_u64_file(mut file: &File) -> std::io::Result<u64> { pub fn hash_u64_file(mut file: &File) -> std::io::Result<u64> {
let mut hasher = SipHasher::new_with_keys(0, 0); let mut hasher = StableHasher::new();
let mut buf = [0; 64 * 1024]; let mut buf = [0; 64 * 1024];
loop { loop {
let n = file.read(&mut buf)?; let n = file.read(&mut buf)?;

View File

@ -9,6 +9,7 @@ pub use self::errors::{CargoResult, CargoResultExt, CliResult, Test};
pub use self::errors::{CargoTestError, CliError, ProcessError}; pub use self::errors::{CargoTestError, CliError, ProcessError};
pub use self::flock::{FileLock, Filesystem}; pub use self::flock::{FileLock, Filesystem};
pub use self::graph::Graph; pub use self::graph::Graph;
pub use self::hasher::StableHasher;
pub use self::hex::{hash_u64, short_hash, to_hex}; pub use self::hex::{hash_u64, short_hash, to_hex};
pub use self::into_url::IntoUrl; pub use self::into_url::IntoUrl;
pub use self::into_url_with_base::IntoUrlWithBase; pub use self::into_url_with_base::IntoUrlWithBase;
@ -39,6 +40,7 @@ pub mod diagnostic_server;
pub mod errors; pub mod errors;
mod flock; mod flock;
pub mod graph; pub mod graph;
mod hasher;
pub mod hex; pub mod hex;
pub mod important_paths; pub mod important_paths;
pub mod into_url; pub mod into_url;

View File

@ -1,8 +1,6 @@
#![allow(deprecated)] // for SipHasher
use std::collections::hash_map::{Entry, HashMap}; use std::collections::hash_map::{Entry, HashMap};
use std::env; use std::env;
use std::hash::{Hash, Hasher, SipHasher}; use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::Mutex; use std::sync::Mutex;
@ -11,7 +9,7 @@ use serde::{Deserialize, Serialize};
use crate::core::InternedString; use crate::core::InternedString;
use crate::util::paths; use crate::util::paths;
use crate::util::{self, profile, CargoResult, CargoResultExt, ProcessBuilder}; use crate::util::{self, profile, CargoResult, CargoResultExt, ProcessBuilder, StableHasher};
/// Information on the `rustc` executable /// Information on the `rustc` executable
#[derive(Debug)] #[derive(Debug)]
@ -222,7 +220,7 @@ impl Drop for Cache {
} }
fn rustc_fingerprint(path: &Path, rustup_rustc: &Path) -> CargoResult<u64> { fn rustc_fingerprint(path: &Path, rustup_rustc: &Path) -> CargoResult<u64> {
let mut hasher = SipHasher::new(); let mut hasher = StableHasher::new();
let path = paths::resolve_executable(path)?; let path = paths::resolve_executable(path)?;
path.hash(&mut hasher); path.hash(&mut hasher);
@ -266,7 +264,7 @@ fn rustc_fingerprint(path: &Path, rustup_rustc: &Path) -> CargoResult<u64> {
} }
fn process_fingerprint(cmd: &ProcessBuilder) -> u64 { fn process_fingerprint(cmd: &ProcessBuilder) -> u64 {
let mut hasher = SipHasher::new(); let mut hasher = StableHasher::new();
cmd.get_args().hash(&mut hasher); cmd.get_args().hash(&mut hasher);
let mut env = cmd.get_envs().iter().collect::<Vec<_>>(); let mut env = cmd.get_envs().iter().collect::<Vec<_>>();
env.sort_unstable(); env.sort_unstable();