From 8fbc8459d59f3acecdb6132fe6f4f2c0b3edbc47 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Sun, 9 Oct 2022 00:30:12 +0100 Subject: [PATCH] Use `#[default]` when possible Make the default value closer to enum definition. --- src/cargo/core/compiler/fingerprint.rs | 8 ++---- src/cargo/core/resolver/resolve.rs | 36 ++++++++++---------------- src/cargo/util/config/mod.rs | 18 +++---------- 3 files changed, 20 insertions(+), 42 deletions(-) diff --git a/src/cargo/core/compiler/fingerprint.rs b/src/cargo/core/compiler/fingerprint.rs index 35a61be13a1..419c780ea1f 100644 --- a/src/cargo/core/compiler/fingerprint.rs +++ b/src/cargo/core/compiler/fingerprint.rs @@ -559,10 +559,12 @@ pub struct Fingerprint { } /// Indication of the status on the filesystem for a particular unit. +#[derive(Default)] enum FsStatus { /// This unit is to be considered stale, even if hash information all /// matches. The filesystem inputs have changed (or are missing) and the /// unit needs to subsequently be recompiled. + #[default] Stale, /// This unit is up-to-date. All outputs and their corresponding mtime are @@ -579,12 +581,6 @@ impl FsStatus { } } -impl Default for FsStatus { - fn default() -> FsStatus { - FsStatus::Stale - } -} - impl Serialize for DepFingerprint { fn serialize(&self, ser: S) -> Result where diff --git a/src/cargo/core/resolver/resolve.rs b/src/cargo/core/resolver/resolve.rs index 221fbeec6fc..6ab957e924f 100644 --- a/src/cargo/core/resolver/resolve.rs +++ b/src/cargo/core/resolver/resolve.rs @@ -48,13 +48,23 @@ pub struct Resolve { /// A version to indicate how a `Cargo.lock` should be serialized. /// -/// When creating a new lockfile, the default version defined in -/// [`ResolveVersion::default`] is used. +/// When creating a new lockfile, the version with `#[default]` is used. /// If an old version of lockfile already exists, it will stay as-is. /// +/// It's important that if a new version is added that this is not updated +/// until *at least* the support for the version is in the stable release of Rust. +/// +/// This resolve version will be used for all new lock files, for example +/// those generated by `cargo update` (update everything) or building after +/// a `cargo new` (where no lock file previously existed). This is also used +/// for *updated* lock files such as when a dependency is added or when a +/// version requirement changes. In this situation Cargo's updating the lock +/// file anyway so it takes the opportunity to bump the lock file version +/// forward. +/// /// It's theorized that we can add more here over time to track larger changes /// to the `Cargo.lock` format, but we've yet to see how that strategy pans out. -#[derive(PartialEq, Eq, Clone, Copy, Debug, PartialOrd, Ord)] +#[derive(Default, PartialEq, Eq, Clone, Copy, Debug, PartialOrd, Ord)] pub enum ResolveVersion { /// Historical baseline for when this abstraction was added. V1, @@ -68,6 +78,7 @@ pub enum ResolveVersion { /// `branch = "master"` are no longer encoded the same way as those without /// branch specifiers. Introduced in 2020 in version 1.47. New lockfiles use /// V3 by default staring in 1.53. + #[default] V3, } @@ -394,22 +405,3 @@ impl fmt::Debug for Resolve { write!(fmt, "}}") } } - -impl Default for ResolveVersion { - /// The default way to encode new or updated `Cargo.lock` files. - /// - /// It's important that if a new version of [`ResolveVersion`] is added that - /// this is not updated until *at least* the support for the version is in - /// the stable release of Rust. - /// - /// This resolve version will be used for all new lock files, for example - /// those generated by `cargo update` (update everything) or building after - /// a `cargo new` (where no lock file previously existed). This is also used - /// for *updated* lock files such as when a dependency is added or when a - /// version requirement changes. In this situation Cargo's updating the lock - /// file anyway so it takes the opportunity to bump the lock file version - /// forward. - fn default() -> ResolveVersion { - ResolveVersion::V3 - } -} diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index fddd47acb90..df5e0373af5 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -2127,9 +2127,10 @@ pub struct CargoFutureIncompatConfig { frequency: Option, } -#[derive(Debug, Deserialize, PartialEq)] +#[derive(Debug, Default, Deserialize, PartialEq)] #[serde(rename_all = "kebab-case")] pub enum CargoFutureIncompatFrequencyConfig { + #[default] Always, Never, } @@ -2146,12 +2147,6 @@ impl CargoFutureIncompatConfig { } } -impl Default for CargoFutureIncompatFrequencyConfig { - fn default() -> Self { - Self::Always - } -} - /// Configuration for `ssl-version` in `http` section /// There are two ways to configure: /// @@ -2270,20 +2265,15 @@ pub struct ProgressConfig { pub width: Option, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Default, Deserialize)] #[serde(rename_all = "lowercase")] pub enum ProgressWhen { + #[default] Auto, Never, Always, } -impl Default for ProgressWhen { - fn default() -> ProgressWhen { - ProgressWhen::Auto - } -} - fn progress_or_string<'de, D>(deserializer: D) -> Result, D::Error> where D: serde::de::Deserializer<'de>,