Skip to content

Commit dd13a80

Browse files
committedSep 29, 2016
impl {Add, AddAssign}<{str, Cow<str>}> for Cow<str>
This does not actually add anything that wasn't there, but is merely an optimization for the given cases, which would have incurred additional heap allocation for adding empty strings, and improving the ergonomics of `Cow` with strings.
1 parent fb62f4d commit dd13a80

File tree

2 files changed

+112
-1
lines changed

2 files changed

+112
-1
lines changed
 

‎src/libcollections/borrow.rs

+47-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use core::cmp::Ordering;
1616
use core::hash::{Hash, Hasher};
17-
use core::ops::Deref;
17+
use core::ops::{Add, AddAssign, Deref};
1818

1919
use fmt;
2020

@@ -270,3 +270,49 @@ impl<'a, T: ?Sized + ToOwned> AsRef<T> for Cow<'a, T> {
270270
self
271271
}
272272
}
273+
274+
#[stable(feature = "cow_add", since = "1.13.0")]
275+
impl<'a> Add<&'a str> for Cow<'a, str> {
276+
type Output = Cow<'a, str>;
277+
278+
fn add(self, rhs: &'a str) -> Self {
279+
if self == "" {
280+
Cow::Borrowed(rhs)
281+
} else if rhs == "" {
282+
self
283+
} else {
284+
Cow::Owned(self.into_owned() + rhs)
285+
}
286+
}
287+
}
288+
289+
#[stable(feature = "cow_add", since = "1.13.0")]
290+
impl<'a> Add<Cow<'a, str>> for Cow<'a, str> {
291+
type Output = Cow<'a, str>;
292+
293+
fn add(self, rhs: Cow<'a, str>) -> Self {
294+
if self == "" {
295+
rhs
296+
} else if rhs == "" {
297+
self
298+
} else {
299+
Cow::Owned(self.into_owned() + rhs.borrow())
300+
}
301+
}
302+
}
303+
304+
#[stable(feature = "cow_add", since = "1.13.0")]
305+
impl<'a> AddAssign<&'a str> for Cow<'a, str> {
306+
fn add_assign(&mut self, rhs: &'a str) {
307+
if rhs == "" { return; }
308+
self.to_mut().push_str(rhs);
309+
}
310+
}
311+
312+
#[stable(feature = "cow_add", since = "1.13.0")]
313+
impl<'a> AddAssign<Cow<'a, str>> for Cow<'a, str> {
314+
fn add_assign(&mut self, rhs: Cow<'a, str>) {
315+
if rhs == "" { return; }
316+
self.to_mut().push_str(rhs.borrow());
317+
}
318+
}

‎src/libcollectionstest/cow_str.rs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2012-2013-2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::borrow::Cow;
12+
13+
// check that Cow<'a, str> implements addition
14+
#[test]
15+
fn check_cow_add() {
16+
borrowed1 = Cow::Borrowed("Hello, ");
17+
borrowed2 = Cow::Borrowed("World!");
18+
borrow_empty = Cow::Borrowed("");
19+
20+
owned1 = Cow::Owned("Hi, ".into());
21+
owned2 = Cow::Owned("Rustaceans!".into());
22+
owned_empty = Cow::Owned("".into());
23+
24+
assert_eq!("Hello, World!", borrowed1 + borrowed2);
25+
assert_eq!("Hello, Rustaceans!", borrowed1 + owned2);
26+
27+
assert_eq!("Hello, World!", owned1 + borrowed2);
28+
assert_eq!("Hello, Rustaceans!", owned1 + owned2);
29+
30+
if let Cow::Owned(_) = borrowed1 + borrow_empty {
31+
panic!("Adding empty strings to a borrow should note allocate");
32+
}
33+
if let Cow::Owned(_) = borrow_empty + borrowed1 {
34+
panic!("Adding empty strings to a borrow should note allocate");
35+
}
36+
if let Cow::Owned(_) = borrowed1 + owned_empty {
37+
panic!("Adding empty strings to a borrow should note allocate");
38+
}
39+
if let Cow::Owned(_) = owned_empty + borrowed1 {
40+
panic!("Adding empty strings to a borrow should note allocate");
41+
}
42+
}
43+
44+
fn check_cow_add_assign() {
45+
borrowed1 = Cow::Borrowed("Hello, ");
46+
borrowed2 = Cow::Borrowed("World!");
47+
borrow_empty = Cow::Borrowed("");
48+
49+
owned1 = Cow::Owned("Hi, ".into());
50+
owned2 = Cow::Owned("Rustaceans!".into());
51+
owned_empty = Cow::Owned("".into());
52+
53+
let borrowed1clone = borrowed1.clone();
54+
borrowed1clone += borrow_empty;
55+
assert_eq!((&borrowed1clone).as_ptr(), (&borrowed1).as_ptr());
56+
57+
borrowed1clone += owned_empty;
58+
assert_eq!((&borrowed1clone).as_ptr(), (&borrowed1).as_ptr());
59+
60+
owned1 += borrowed2;
61+
borrowed1 += owned2;
62+
63+
assert_eq!("Hello, World!", owned1);
64+
assert_eq!("Hello, Rustaceans!", borrowed1);
65+
}

0 commit comments

Comments
 (0)