From d2d210257fd6df5ec2201a3be6f9053bda3ef686 Mon Sep 17 00:00:00 2001 From: Jacob Finkelman Date: Mon, 29 Jul 2024 17:15:41 +0000 Subject: [PATCH 1/2] dont call wrap in a no-op source_id::with* --- src/cargo/core/source_id.rs | 49 +++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/src/cargo/core/source_id.rs b/src/cargo/core/source_id.rs index d03a0a5769c..113f46170df 100644 --- a/src/cargo/core/source_id.rs +++ b/src/cargo/core/source_id.rs @@ -468,34 +468,51 @@ impl SourceId { /// Creates a new `SourceId` from this source with the given `precise`. pub fn with_git_precise(self, fragment: Option) -> SourceId { - SourceId::wrap(SourceIdInner { - precise: fragment.map(|f| Precise::GitUrlFragment(f)), - ..(*self.inner).clone() - }) + let precise = fragment.map(|f| Precise::GitUrlFragment(f)); + if self.inner.precise == precise { + self + } else { + SourceId::wrap(SourceIdInner { + precise, + ..(*self.inner).clone() + }) + } } /// Creates a new `SourceId` from this source without a `precise`. pub fn without_precise(self) -> SourceId { - SourceId::wrap(SourceIdInner { - precise: None, - ..(*self.inner).clone() - }) + if self.inner.precise.is_none() { + self + } else { + SourceId::wrap(SourceIdInner { + precise: None, + ..(*self.inner).clone() + }) + } } /// Creates a new `SourceId` from this source without a `precise`. pub fn with_locked_precise(self) -> SourceId { - SourceId::wrap(SourceIdInner { - precise: Some(Precise::Locked), - ..(*self.inner).clone() - }) + if self.inner.precise == Some(Precise::Locked) { + self + } else { + SourceId::wrap(SourceIdInner { + precise: Some(Precise::Locked), + ..(*self.inner).clone() + }) + } } /// Creates a new `SourceId` from this source with the `precise` from some other `SourceId`. pub fn with_precise_from(self, v: Self) -> SourceId { - SourceId::wrap(SourceIdInner { - precise: v.inner.precise.clone(), - ..(*self.inner).clone() - }) + if self.inner.precise == v.inner.precise { + self + } else { + SourceId::wrap(SourceIdInner { + precise: v.inner.precise.clone(), + ..(*self.inner).clone() + }) + } } /// When updating a lock file on a version using `cargo update --precise` From 22f4352c29d54331ae484a6a17545014c2e6c759 Mon Sep 17 00:00:00 2001 From: Jacob Finkelman Date: Mon, 29 Jul 2024 19:33:21 +0000 Subject: [PATCH 2/2] with_precise as a helper --- src/cargo/core/source_id.rs | 36 +++++++++--------------------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/src/cargo/core/source_id.rs b/src/cargo/core/source_id.rs index 113f46170df..19ec9ca051c 100644 --- a/src/cargo/core/source_id.rs +++ b/src/cargo/core/source_id.rs @@ -468,48 +468,30 @@ impl SourceId { /// Creates a new `SourceId` from this source with the given `precise`. pub fn with_git_precise(self, fragment: Option) -> SourceId { - let precise = fragment.map(|f| Precise::GitUrlFragment(f)); - if self.inner.precise == precise { - self - } else { - SourceId::wrap(SourceIdInner { - precise, - ..(*self.inner).clone() - }) - } + self.with_precise(&fragment.map(|f| Precise::GitUrlFragment(f))) } /// Creates a new `SourceId` from this source without a `precise`. pub fn without_precise(self) -> SourceId { - if self.inner.precise.is_none() { - self - } else { - SourceId::wrap(SourceIdInner { - precise: None, - ..(*self.inner).clone() - }) - } + self.with_precise(&None) } /// Creates a new `SourceId` from this source without a `precise`. pub fn with_locked_precise(self) -> SourceId { - if self.inner.precise == Some(Precise::Locked) { - self - } else { - SourceId::wrap(SourceIdInner { - precise: Some(Precise::Locked), - ..(*self.inner).clone() - }) - } + self.with_precise(&Some(Precise::Locked)) } /// Creates a new `SourceId` from this source with the `precise` from some other `SourceId`. pub fn with_precise_from(self, v: Self) -> SourceId { - if self.inner.precise == v.inner.precise { + self.with_precise(&v.inner.precise) + } + + fn with_precise(self, precise: &Option) -> SourceId { + if &self.inner.precise == precise { self } else { SourceId::wrap(SourceIdInner { - precise: v.inner.precise.clone(), + precise: precise.clone(), ..(*self.inner).clone() }) }