Skip to content

Commit 25c5ced

Browse files
committed
Auto merge of #11197 - weihanglo:enum-default, r=epage
Use `#[default]` when possible Just realize that we can derive `Default` for some enums with `#[default]` attribute. This makes default value closer to enum definition. Ref: https://doc.rust-lang.org/std/default/trait.Default.html#enums
2 parents d9b05a1 + 8fbc845 commit 25c5ced

File tree

3 files changed

+20
-42
lines changed

3 files changed

+20
-42
lines changed

src/cargo/core/compiler/fingerprint.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,12 @@ pub struct Fingerprint {
559559
}
560560

561561
/// Indication of the status on the filesystem for a particular unit.
562+
#[derive(Default)]
562563
enum FsStatus {
563564
/// This unit is to be considered stale, even if hash information all
564565
/// matches. The filesystem inputs have changed (or are missing) and the
565566
/// unit needs to subsequently be recompiled.
567+
#[default]
566568
Stale,
567569

568570
/// This unit is up-to-date. All outputs and their corresponding mtime are
@@ -579,12 +581,6 @@ impl FsStatus {
579581
}
580582
}
581583

582-
impl Default for FsStatus {
583-
fn default() -> FsStatus {
584-
FsStatus::Stale
585-
}
586-
}
587-
588584
impl Serialize for DepFingerprint {
589585
fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error>
590586
where

src/cargo/core/resolver/resolve.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,23 @@ pub struct Resolve {
4848

4949
/// A version to indicate how a `Cargo.lock` should be serialized.
5050
///
51-
/// When creating a new lockfile, the default version defined in
52-
/// [`ResolveVersion::default`] is used.
51+
/// When creating a new lockfile, the version with `#[default]` is used.
5352
/// If an old version of lockfile already exists, it will stay as-is.
5453
///
54+
/// It's important that if a new version is added that this is not updated
55+
/// until *at least* the support for the version is in the stable release of Rust.
56+
///
57+
/// This resolve version will be used for all new lock files, for example
58+
/// those generated by `cargo update` (update everything) or building after
59+
/// a `cargo new` (where no lock file previously existed). This is also used
60+
/// for *updated* lock files such as when a dependency is added or when a
61+
/// version requirement changes. In this situation Cargo's updating the lock
62+
/// file anyway so it takes the opportunity to bump the lock file version
63+
/// forward.
64+
///
5565
/// It's theorized that we can add more here over time to track larger changes
5666
/// to the `Cargo.lock` format, but we've yet to see how that strategy pans out.
57-
#[derive(PartialEq, Eq, Clone, Copy, Debug, PartialOrd, Ord)]
67+
#[derive(Default, PartialEq, Eq, Clone, Copy, Debug, PartialOrd, Ord)]
5868
pub enum ResolveVersion {
5969
/// Historical baseline for when this abstraction was added.
6070
V1,
@@ -68,6 +78,7 @@ pub enum ResolveVersion {
6878
/// `branch = "master"` are no longer encoded the same way as those without
6979
/// branch specifiers. Introduced in 2020 in version 1.47. New lockfiles use
7080
/// V3 by default staring in 1.53.
81+
#[default]
7182
V3,
7283
}
7384

@@ -394,22 +405,3 @@ impl fmt::Debug for Resolve {
394405
write!(fmt, "}}")
395406
}
396407
}
397-
398-
impl Default for ResolveVersion {
399-
/// The default way to encode new or updated `Cargo.lock` files.
400-
///
401-
/// It's important that if a new version of [`ResolveVersion`] is added that
402-
/// this is not updated until *at least* the support for the version is in
403-
/// the stable release of Rust.
404-
///
405-
/// This resolve version will be used for all new lock files, for example
406-
/// those generated by `cargo update` (update everything) or building after
407-
/// a `cargo new` (where no lock file previously existed). This is also used
408-
/// for *updated* lock files such as when a dependency is added or when a
409-
/// version requirement changes. In this situation Cargo's updating the lock
410-
/// file anyway so it takes the opportunity to bump the lock file version
411-
/// forward.
412-
fn default() -> ResolveVersion {
413-
ResolveVersion::V3
414-
}
415-
}

src/cargo/util/config/mod.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,9 +2127,10 @@ pub struct CargoFutureIncompatConfig {
21272127
frequency: Option<CargoFutureIncompatFrequencyConfig>,
21282128
}
21292129

2130-
#[derive(Debug, Deserialize, PartialEq)]
2130+
#[derive(Debug, Default, Deserialize, PartialEq)]
21312131
#[serde(rename_all = "kebab-case")]
21322132
pub enum CargoFutureIncompatFrequencyConfig {
2133+
#[default]
21332134
Always,
21342135
Never,
21352136
}
@@ -2146,12 +2147,6 @@ impl CargoFutureIncompatConfig {
21462147
}
21472148
}
21482149

2149-
impl Default for CargoFutureIncompatFrequencyConfig {
2150-
fn default() -> Self {
2151-
Self::Always
2152-
}
2153-
}
2154-
21552150
/// Configuration for `ssl-version` in `http` section
21562151
/// There are two ways to configure:
21572152
///
@@ -2270,20 +2265,15 @@ pub struct ProgressConfig {
22702265
pub width: Option<usize>,
22712266
}
22722267

2273-
#[derive(Debug, Deserialize)]
2268+
#[derive(Debug, Default, Deserialize)]
22742269
#[serde(rename_all = "lowercase")]
22752270
pub enum ProgressWhen {
2271+
#[default]
22762272
Auto,
22772273
Never,
22782274
Always,
22792275
}
22802276

2281-
impl Default for ProgressWhen {
2282-
fn default() -> ProgressWhen {
2283-
ProgressWhen::Auto
2284-
}
2285-
}
2286-
22872277
fn progress_or_string<'de, D>(deserializer: D) -> Result<Option<ProgressConfig>, D::Error>
22882278
where
22892279
D: serde::de::Deserializer<'de>,

0 commit comments

Comments
 (0)