Skip to content

Commit 60ebeda

Browse files
authored
Rollup merge of #67238 - llogiq:moo-and-improved, r=Dylan-DPC
Small std::borrow::Cow improvements This is a small set of improvements (+ one more tested code path) for `Cow`.
2 parents 59eed49 + 2422797 commit 60ebeda

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

src/liballoc/borrow.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,10 @@ impl<B: ?Sized + ToOwned> Clone for Cow<'_, B> {
195195
}
196196

197197
fn clone_from(&mut self, source: &Self) {
198-
if let Owned(ref mut dest) = *self {
199-
if let Owned(ref o) = *source {
200-
o.borrow().clone_into(dest);
201-
return;
202-
}
198+
match (self, source) {
199+
(&mut Owned(ref mut dest), &Owned(ref o)) => o.borrow().clone_into(dest),
200+
(t, s) => *t = s.clone(),
203201
}
204-
205-
*self = source.clone();
206202
}
207203
}
208204

@@ -449,9 +445,7 @@ impl<'a> AddAssign<&'a str> for Cow<'a, str> {
449445
fn add_assign(&mut self, rhs: &'a str) {
450446
if self.is_empty() {
451447
*self = Cow::Borrowed(rhs)
452-
} else if rhs.is_empty() {
453-
return;
454-
} else {
448+
} else if !rhs.is_empty() {
455449
if let Cow::Borrowed(lhs) = *self {
456450
let mut s = String::with_capacity(lhs.len() + rhs.len());
457451
s.push_str(lhs);
@@ -467,9 +461,7 @@ impl<'a> AddAssign<Cow<'a, str>> for Cow<'a, str> {
467461
fn add_assign(&mut self, rhs: Cow<'a, str>) {
468462
if self.is_empty() {
469463
*self = rhs
470-
} else if rhs.is_empty() {
471-
return;
472-
} else {
464+
} else if !rhs.is_empty() {
473465
if let Cow::Borrowed(lhs) = *self {
474466
let mut s = String::with_capacity(lhs.len() + rhs.len());
475467
s.push_str(lhs);

src/liballoc/tests/cow_str.rs

+3
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,7 @@ fn check_cow_clone_from() {
138138
let c2: Cow<'_, str> = Cow::Owned(s);
139139
c1.clone_from(&c2);
140140
assert!(c1.into_owned().capacity() >= 25);
141+
let mut c3: Cow<'_, str> = Cow::Borrowed("bye");
142+
c3.clone_from(&c2);
143+
assert_eq!(c2, c3);
141144
}

0 commit comments

Comments
 (0)