Skip to content

Commit

Permalink
Use #[default] when possible
Browse files Browse the repository at this point in the history
Make the default value closer to enum definition.
  • Loading branch information
weihanglo committed Oct 8, 2022
1 parent d9b05a1 commit 8fbc845
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 42 deletions.
8 changes: 2 additions & 6 deletions src/cargo/core/compiler/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -579,12 +581,6 @@ impl FsStatus {
}
}

impl Default for FsStatus {
fn default() -> FsStatus {
FsStatus::Stale
}
}

impl Serialize for DepFingerprint {
fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error>
where
Expand Down
36 changes: 14 additions & 22 deletions src/cargo/core/resolver/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
}

Expand Down Expand Up @@ -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
}
}
18 changes: 4 additions & 14 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2127,9 +2127,10 @@ pub struct CargoFutureIncompatConfig {
frequency: Option<CargoFutureIncompatFrequencyConfig>,
}

#[derive(Debug, Deserialize, PartialEq)]
#[derive(Debug, Default, Deserialize, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub enum CargoFutureIncompatFrequencyConfig {
#[default]
Always,
Never,
}
Expand All @@ -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:
///
Expand Down Expand Up @@ -2270,20 +2265,15 @@ pub struct ProgressConfig {
pub width: Option<usize>,
}

#[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<Option<ProgressConfig>, D::Error>
where
D: serde::de::Deserializer<'de>,
Expand Down

0 comments on commit 8fbc845

Please sign in to comment.