Skip to content

Commit 2662da3

Browse files
Merge pull request #297 from stepancheg/reference-to-string
Avoid extra allocation in Reference::to_string
2 parents b39fda3 + dd808d6 commit 2662da3

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

src/distribution/reference.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -183,37 +183,40 @@ impl Reference {
183183
}
184184
}
185185

186-
/// Returns the full repository name and path.
187-
fn full_name(&self) -> String {
188-
if self.registry() == "" {
189-
self.repository().to_string()
190-
} else {
191-
format!("{}/{}", self.registry(), self.repository())
192-
}
193-
}
194-
195186
/// Returns the whole reference.
196187
pub fn whole(&self) -> String {
197-
let mut s = self.full_name();
188+
self.to_string()
189+
}
190+
}
191+
192+
impl fmt::Display for Reference {
193+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
194+
let mut not_empty = false;
195+
if !self.registry().is_empty() {
196+
write!(f, "{}", self.registry())?;
197+
not_empty = true;
198+
}
199+
if !self.repository().is_empty() {
200+
if not_empty {
201+
write!(f, "/")?;
202+
}
203+
write!(f, "{}", self.repository())?;
204+
not_empty = true;
205+
}
198206
if let Some(t) = self.tag() {
199-
if !s.is_empty() {
200-
s.push(':');
207+
if not_empty {
208+
write!(f, ":")?;
201209
}
202-
s.push_str(t);
210+
write!(f, "{t}")?;
211+
not_empty = true;
203212
}
204213
if let Some(d) = self.digest() {
205-
if !s.is_empty() {
206-
s.push('@');
214+
if not_empty {
215+
write!(f, "@")?;
207216
}
208-
s.push_str(d);
217+
write!(f, "{d}")?;
209218
}
210-
s
211-
}
212-
}
213-
214-
impl fmt::Display for Reference {
215-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
216-
write!(f, "{}", self.whole())
219+
Ok(())
217220
}
218221
}
219222

0 commit comments

Comments
 (0)