Skip to content

Commit

Permalink
remove hazard by using derivative(procmacro crate) instead of handwri…
Browse files Browse the repository at this point in the history
…tten impls
  • Loading branch information
Hezuikn committed Sep 21, 2022
1 parent 869951c commit 3b3f447
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 38 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ unicode-width = "0.1.5"
openssl = { version = '0.10.11', optional = true }
im-rc = "15.0.0"
itertools = "0.10.0"
derivative = "2.2.0"

# A noop dependency that changes in the Rust repository, it's a bit of a hack.
# See the `src/tools/rustc-workspace-hack/README.md` file in `rust-lang/rust`
Expand Down
46 changes: 8 additions & 38 deletions src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::util::{closest_msg, config, CargoResult, Config};
use anyhow::{bail, Context as _};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::hash::Hash;
use std::{cmp, env, fmt, hash};
use std::{env, fmt};

/// Collection of all profiles.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -539,10 +539,16 @@ pub enum ProfileRoot {

/// Profile settings used to determine which compiler flags to use for a
/// target.
#[derive(Clone, Eq, PartialOrd, Ord, serde::Serialize)]
#[derive(Clone, Eq, PartialOrd, Ord, serde::Serialize, derivative::Derivative)]
/// Don't compare/hash fields which wont affect compilation.
/// This is necessary for `Unit` deduplication for things like "test" and
/// "dev" which are essentially the same.
#[derivative(Hash, PartialEq)]
pub struct Profile {
#[derivative(Hash = "ignore", PartialEq = "ignore")]
pub name: InternedString,
pub opt_level: InternedString,
#[derivative(Hash = "ignore", PartialEq = "ignore")]
#[serde(skip)] // named profiles are unstable
pub root: ProfileRoot,
pub lto: Lto,
Expand Down Expand Up @@ -620,21 +626,6 @@ impl fmt::Display for Profile {
}
}

impl hash::Hash for Profile {
fn hash<H>(&self, state: &mut H)
where
H: hash::Hasher,
{
self.comparable().hash(state);
}
}

impl cmp::PartialEq for Profile {
fn eq(&self, other: &Self) -> bool {
self.comparable() == other.comparable()
}
}

impl Profile {
fn default_dev() -> Profile {
Profile {
Expand All @@ -656,27 +647,6 @@ impl Profile {
..Profile::default()
}
}

/// Don't compare/hash fields which wont affect compilation.
/// This is necessary for `Unit` deduplication for things like "test" and
/// "dev" which are essentially the same.
fn comparable(&self) -> impl Hash + Eq + '_ {
(
self.opt_level,
self.lto,
self.codegen_backend,
self.codegen_units,
self.debuginfo,
self.split_debuginfo,
self.debug_assertions,
self.overflow_checks,
self.rpath,
self.incremental,
self.panic,
//"This trait is implemented for tuples up to twelve items long." - https://doc.rust-lang.org/std/cmp/trait.Eq.html#impl-Eq-203
(self.strip, &self.rustflags),
)
}
}

/// The link-time-optimization setting.
Expand Down

0 comments on commit 3b3f447

Please sign in to comment.