Skip to content

Commit

Permalink
Small Cow improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
llogiq committed Dec 11, 2019
1 parent 90b957a commit 2422797
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
18 changes: 5 additions & 13 deletions src/liballoc/borrow.rs
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/liballoc/tests/cow_str.rs
Expand Up @@ -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);
}

0 comments on commit 2422797

Please sign in to comment.