about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorYuki Okushi <huyuumi.dev@gmail.com>2019-12-12 10:09:28 +0900
committerGitHub <noreply@github.com>2019-12-12 10:09:28 +0900
commit60ebeda94f0ca7caf8cecf29cc3196b3dbd01b31 (patch)
treeae26ba1b0b957af7e48d8ae3d61871f488fd99bd /src/liballoc
parent59eed49115c98da4dca8387a89035e16a9d395c4 (diff)
parent24227977852b22eb0ab3a228b7135611ae49bb8d (diff)
downloadrust-60ebeda94f0ca7caf8cecf29cc3196b3dbd01b31.tar.gz
rust-60ebeda94f0ca7caf8cecf29cc3196b3dbd01b31.zip
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`.
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/borrow.rs18
-rw-r--r--src/liballoc/tests/cow_str.rs3
2 files changed, 8 insertions, 13 deletions
diff --git a/src/liballoc/borrow.rs b/src/liballoc/borrow.rs
index d2bdda83fa9..fc960451968 100644
--- a/src/liballoc/borrow.rs
+++ b/src/liballoc/borrow.rs
@@ -195,14 +195,10 @@ impl<B: ?Sized + ToOwned> Clone for Cow<'_, B> {
     }
 
     fn clone_from(&mut self, source: &Self) {
-        if let Owned(ref mut dest) = *self {
-            if let Owned(ref o) = *source {
-                o.borrow().clone_into(dest);
-                return;
-            }
+        match (self, source) {
+            (&mut Owned(ref mut dest), &Owned(ref o)) => o.borrow().clone_into(dest),
+            (t, s) => *t = s.clone(),
         }
-
-        *self = source.clone();
     }
 }
 
@@ -449,9 +445,7 @@ impl<'a> AddAssign<&'a str> for Cow<'a, str> {
     fn add_assign(&mut self, rhs: &'a str) {
         if self.is_empty() {
             *self = Cow::Borrowed(rhs)
-        } else if rhs.is_empty() {
-            return;
-        } else {
+        } else if !rhs.is_empty() {
             if let Cow::Borrowed(lhs) = *self {
                 let mut s = String::with_capacity(lhs.len() + rhs.len());
                 s.push_str(lhs);
@@ -467,9 +461,7 @@ impl<'a> AddAssign<Cow<'a, str>> for Cow<'a, str> {
     fn add_assign(&mut self, rhs: Cow<'a, str>) {
         if self.is_empty() {
             *self = rhs
-        } else if rhs.is_empty() {
-            return;
-        } else {
+        } else if !rhs.is_empty() {
             if let Cow::Borrowed(lhs) = *self {
                 let mut s = String::with_capacity(lhs.len() + rhs.len());
                 s.push_str(lhs);
diff --git a/src/liballoc/tests/cow_str.rs b/src/liballoc/tests/cow_str.rs
index 6f357eda9b8..62a5c245a54 100644
--- a/src/liballoc/tests/cow_str.rs
+++ b/src/liballoc/tests/cow_str.rs
@@ -138,4 +138,7 @@ fn check_cow_clone_from() {
     let c2: Cow<'_, str> = Cow::Owned(s);
     c1.clone_from(&c2);
     assert!(c1.into_owned().capacity() >= 25);
+    let mut c3: Cow<'_, str> = Cow::Borrowed("bye");
+    c3.clone_from(&c2);
+    assert_eq!(c2, c3);
 }