Skip to content

Commit

Permalink
refactor: use Cow to avoid unnecessary allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed Sep 15, 2023
1 parent 6c3933f commit a56315c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/cargo/core/source_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::util::{config, CanonicalUrl, CargoResult, Config, IntoUrl, ToSemver};
use anyhow::Context;
use serde::de;
use serde::ser;
use std::borrow::Cow;
use std::cmp::{self, Ordering};
use std::collections::HashSet;
use std::fmt::{self, Formatter};
Expand Down Expand Up @@ -308,17 +309,17 @@ impl SourceId {
}

/// Displays the name of a registry if it has one. Otherwise just the URL.
pub fn display_registry_name(self) -> String {
pub fn display_registry_name<'a>(self) -> Cow<'a, str> {
if self.is_crates_io() {
CRATES_IO_REGISTRY.to_string()
CRATES_IO_REGISTRY.into()
} else if let Some(key) = &self.inner.registry_key {
key.clone()
key.into()
} else if self.precise().is_some() {
// We remove `precise` here to retrieve an permissive version of
// `SourceIdInner`, which may contain the registry name.
self.with_precise(None).display_registry_name()
} else {
url_display(self.url())
url_display(self.url()).into()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn check_dep_has_version(dep: &crate::core::Dependency, publish: bool) -> crate:

if !dep.specified_req() && dep.is_transitive() {
let dep_version_source = dep.registry_id().map_or_else(
|| CRATES_IO_DOMAIN.to_string(),
|| CRATES_IO_DOMAIN.into(),
|registry_id| registry_id.display_registry_name(),
);
anyhow::bail!(
Expand Down

0 comments on commit a56315c

Please sign in to comment.