derive Hash impl

This commit is contained in:
alpharush 2022-12-26 10:01:20 -06:00
parent a27f6aa256
commit 982214b3d9
4 changed files with 16 additions and 59 deletions

View File

@ -9,7 +9,6 @@ use serde::{de::Visitor, Deserialize, Deserializer, Serialize, Serializer};
use std::{ use std::{
collections::{BTreeMap, HashSet}, collections::{BTreeMap, HashSet},
fmt, fs, fmt, fs,
hash::{Hash, Hasher},
path::{Path, PathBuf}, path::{Path, PathBuf},
str::FromStr, str::FromStr,
}; };
@ -284,7 +283,7 @@ impl From<CompilerInput> for StandardJsonCompilerInput {
} }
} }
#[derive(Clone, Debug, Eq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Settings { pub struct Settings {
/// Stop compilation after the given stage. /// Stop compilation after the given stage.
@ -498,37 +497,8 @@ impl Default for Settings {
} }
} }
impl Hash for Settings {
fn hash<H: Hasher>(&self, state: &mut H) {
self.optimizer.enabled.hash(state);
self.optimizer.runs.hash(state);
if let Some(SettingsMetadata { bytecode_hash, cbor_metadata, .. }) = self.metadata {
(bytecode_hash.unwrap_or_default() as u8).hash(state);
cbor_metadata.hash(state);
}
self.output_selection.0.keys().cloned().collect::<String>().hash(state);
(self.evm_version.unwrap_or_default() as u8).hash(state);
self.via_ir.hash(state);
if let Some(DebuggingSettings { revert_strings, debug_info, .. }) = &self.debug {
(revert_strings.unwrap_or_default() as u8).hash(state);
debug_info.hash(state);
}
}
}
impl PartialEq for Settings {
fn eq(&self, other: &Self) -> bool {
self.optimizer == other.optimizer &&
self.metadata == other.metadata &&
self.output_selection == other.output_selection &&
self.evm_version == other.evm_version &&
self.via_ir == other.via_ir &&
self.debug == other.debug
}
}
/// A wrapper type for all libraries in the form of `<file>:<lib>:<addr>` /// A wrapper type for all libraries in the form of `<file>:<lib>:<addr>`
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default, Hash)]
#[serde(transparent)] #[serde(transparent)]
pub struct Libraries { pub struct Libraries {
/// All libraries, `(file path -> (Lib name -> Address)) /// All libraries, `(file path -> (Lib name -> Address))
@ -621,7 +591,7 @@ impl AsMut<BTreeMap<PathBuf, BTreeMap<String, String>>> for Libraries {
} }
} }
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct Optimizer { pub struct Optimizer {
#[serde(default, skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Option::is_none")]
pub enabled: Option<bool>, pub enabled: Option<bool>,
@ -654,7 +624,7 @@ impl Default for Optimizer {
} }
} }
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default, Hash)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct OptimizerDetails { pub struct OptimizerDetails {
/// The peephole optimizer is always on if no details are given, /// The peephole optimizer is always on if no details are given,
@ -694,7 +664,7 @@ pub struct OptimizerDetails {
pub yul_details: Option<YulDetails>, pub yul_details: Option<YulDetails>,
} }
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default, Hash)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct YulDetails { pub struct YulDetails {
/// Improve allocation of stack slots for variables, can free up stack slots early. /// Improve allocation of stack slots for variables, can free up stack slots early.
@ -707,7 +677,7 @@ pub struct YulDetails {
pub optimizer_steps: Option<String>, pub optimizer_steps: Option<String>,
} }
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Hash)]
pub enum EvmVersion { pub enum EvmVersion {
Homestead, Homestead,
TangerineWhistle, TangerineWhistle,
@ -791,7 +761,7 @@ impl FromStr for EvmVersion {
} }
/// Debugging settings for solc /// Debugging settings for solc
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct DebuggingSettings { pub struct DebuggingSettings {
#[serde( #[serde(
@ -816,7 +786,7 @@ pub struct DebuggingSettings {
} }
/// How to treat revert (and require) reason strings. /// How to treat revert (and require) reason strings.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Hash)]
pub enum RevertStrings { pub enum RevertStrings {
/// "default" does not inject compiler-generated revert strings and keeps user-supplied ones. /// "default" does not inject compiler-generated revert strings and keeps user-supplied ones.
Default, Default,
@ -863,7 +833,7 @@ impl Default for RevertStrings {
} }
} }
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct SettingsMetadata { pub struct SettingsMetadata {
/// Use only literal content and not URLs (false by default) /// Use only literal content and not URLs (false by default)
#[serde(default, rename = "useLiteralContent", skip_serializing_if = "Option::is_none")] #[serde(default, rename = "useLiteralContent", skip_serializing_if = "Option::is_none")]
@ -898,7 +868,7 @@ impl From<BytecodeHash> for SettingsMetadata {
/// Determines the hash method for the metadata hash that is appended to the bytecode. /// Determines the hash method for the metadata hash that is appended to the bytecode.
/// ///
/// Solc's default is `Ipfs`, see <https://docs.soliditylang.org/en/latest/using-the-compiler.html#compiler-api>. /// Solc's default is `Ipfs`, see <https://docs.soliditylang.org/en/latest/using-the-compiler.html#compiler-api>.
#[derive(Clone, Debug, Copy, PartialEq, Eq, Serialize, Deserialize)] #[derive(Clone, Debug, Copy, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub enum BytecodeHash { pub enum BytecodeHash {
Ipfs, Ipfs,
None, None,
@ -1049,7 +1019,7 @@ pub struct MetadataSource {
} }
/// Model checker settings for solc /// Model checker settings for solc
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct ModelCheckerSettings { pub struct ModelCheckerSettings {
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")] #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub contracts: BTreeMap<String, Vec<String>>, pub contracts: BTreeMap<String, Vec<String>>,
@ -1066,7 +1036,7 @@ pub struct ModelCheckerSettings {
} }
/// Which model checker engine to run. /// Which model checker engine to run.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub enum ModelCheckerEngine { pub enum ModelCheckerEngine {
Default, Default,
All, All,
@ -1107,7 +1077,7 @@ impl Default for ModelCheckerEngine {
} }
/// Which model checker targets to check. /// Which model checker targets to check.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub enum ModelCheckerTarget { pub enum ModelCheckerTarget {
Assert, Assert,

View File

@ -68,7 +68,7 @@ pub type FileOutputSelection = BTreeMap<String, Vec<String>>;
/// } /// }
/// } /// }
/// ``` /// ```
#[derive(Debug, Clone, Eq, PartialEq, Default, Deserialize)] #[derive(Debug, Clone, Eq, PartialEq, Default, Deserialize, Hash)]
#[serde(transparent)] #[serde(transparent)]
pub struct OutputSelection(pub BTreeMap<String, FileOutputSelection>); pub struct OutputSelection(pub BTreeMap<String, FileOutputSelection>);

View File

@ -11,7 +11,6 @@ use std::{
collections::{BTreeSet, HashSet}, collections::{BTreeSet, HashSet},
fmt::{self, Formatter}, fmt::{self, Formatter},
fs, fs,
hash::{Hash, Hasher},
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
path::{Component, Path, PathBuf}, path::{Component, Path, PathBuf},
}; };
@ -720,7 +719,7 @@ impl ProjectPathsConfigBuilder {
} }
/// The config to use when compiling the contracts /// The config to use when compiling the contracts
#[derive(Clone, Debug, Eq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct SolcConfig { pub struct SolcConfig {
/// How the file was compiled /// How the file was compiled
pub settings: Settings, pub settings: Settings,
@ -746,18 +745,6 @@ impl From<SolcConfig> for Settings {
} }
} }
impl Hash for SolcConfig {
fn hash<H: Hasher>(&self, state: &mut H) {
self.settings.hash(state);
}
}
impl PartialEq for SolcConfig {
fn eq(&self, other: &Self) -> bool {
self.settings == other.settings
}
}
#[derive(Default)] #[derive(Default)]
pub struct SolcConfigBuilder { pub struct SolcConfigBuilder {
settings: Option<Settings>, settings: Option<Settings>,

View File

@ -44,7 +44,7 @@ const JS_LIB_DIR: &str = "node_modules";
/// contracts-ethereum-package ./MyContract.sol /// contracts-ethereum-package ./MyContract.sol
/// ///
/// [Source](https://ethereum.stackexchange.com/questions/74448/what-are-remappings-and-how-do-they-work-in-solidity) /// [Source](https://ethereum.stackexchange.com/questions/74448/what-are-remappings-and-how-do-they-work-in-solidity)
#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord)] #[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct Remapping { pub struct Remapping {
pub name: String, pub name: String,
pub path: String, pub path: String,