Skip to content

Commit ef5ee45

Browse files
authored
span: avoid cloning when rounding
PR #303
1 parent e279916 commit ef5ee45

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/span.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -6088,9 +6088,8 @@ impl<'a> Relative<'a> {
60886088
if smallest >= Unit::Day {
60896089
Nudge::relative_calendar(
60906090
relspan.span,
6091-
// FIXME: Find a way to drop these clones.
6092-
&Relative::Zoned(start.clone()),
6093-
&Relative::Zoned(end.clone()),
6091+
&Relative::Zoned(start.borrowed()),
6092+
&Relative::Zoned(end.borrowed()),
60946093
smallest,
60956094
increment,
60966095
mode,
@@ -6364,6 +6363,12 @@ impl<'a> RelativeZoned<'a> {
63646363
)
63656364
})
63666365
}
6366+
6367+
/// Returns the borrowed version of self; useful when you need to convert
6368+
/// `&RelativeZoned` into `RelativeZoned` without cloning anything.
6369+
fn borrowed(&self) -> RelativeZoned {
6370+
RelativeZoned { zoned: self.zoned.borrowed() }
6371+
}
63676372
}
63686373

63696374
// The code below is the "core" rounding logic for spans. It was greatly

src/util/borrow.rs

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ pub(crate) enum DumbCow<'a, T> {
1414
Borrowed(&'a T),
1515
}
1616

17+
impl<'a, T> DumbCow<'a, T> {
18+
pub(crate) fn borrowed(&self) -> DumbCow<'_, T> {
19+
match *self {
20+
DumbCow::Owned(ref this) => DumbCow::Borrowed(this),
21+
DumbCow::Borrowed(ref this) => DumbCow::Borrowed(this),
22+
}
23+
}
24+
}
25+
1726
impl<'a, T> core::ops::Deref for DumbCow<'a, T> {
1827
type Target = T;
1928
fn deref(&self) -> &T {

0 commit comments

Comments
 (0)