]> git.lizzy.rs Git - rust.git/commitdiff
Small Cow improvements
authorAndre Bogus <bogusandre@gmail.com>
Wed, 11 Dec 2019 20:01:33 +0000 (21:01 +0100)
committerAndre Bogus <bogusandre@gmail.com>
Wed, 11 Dec 2019 20:01:33 +0000 (21:01 +0100)
src/liballoc/borrow.rs
src/liballoc/tests/cow_str.rs

index d2bdda83fa998ae4e8e195d955627c7c88887fba..fc9604519684682afd284ecb3d33fa595362c536 100644 (file)
@@ -195,14 +195,10 @@ fn clone(&self) -> Self {
     }
 
     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);
index 6f357eda9b83b88d70e93c58cddb4edef75b452b..62a5c245a54297d8bf784951ac7209d7f4253fb7 100644 (file)
@@ -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);
 }