Skip to content

Commit

Permalink
Modified our Add<T> impls:
Browse files Browse the repository at this point in the history
    * removed Add<CompactString> for String, because it broke adding some types to String
    * remove all Add<T> for CompactString types, besides Add<&str> for CompactString, which actually allows us to add _more_ string types
    * Update .gitignore

  Backport of #130
  • Loading branch information
ParkMyCar committed Jun 28, 2022
1 parent 81c43b0 commit e46f89c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 43 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@

# macOS
**/.DS_Store

# VSCode
.vscode/**
40 changes: 0 additions & 40 deletions compact_str/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,22 +706,6 @@ impl fmt::Write for CompactString {
}
}

impl Add<Self> for CompactString {
type Output = Self;
fn add(mut self, rhs: Self) -> Self::Output {
self.push_str(&rhs);
self
}
}

impl Add<&Self> for CompactString {
type Output = Self;
fn add(mut self, rhs: &Self) -> Self::Output {
self.push_str(rhs);
self
}
}

impl Add<&str> for CompactString {
type Output = Self;
fn add(mut self, rhs: &str) -> Self::Output {
Expand All @@ -730,28 +714,4 @@ impl Add<&str> for CompactString {
}
}

impl Add<&String> for CompactString {
type Output = Self;
fn add(mut self, rhs: &String) -> Self::Output {
self.push_str(rhs);
self
}
}

impl Add<String> for CompactString {
type Output = Self;
fn add(mut self, rhs: String) -> Self::Output {
self.push_str(&rhs);
self
}
}

impl Add<CompactString> for String {
type Output = Self;
fn add(mut self, rhs: CompactString) -> Self::Output {
self.push_str(&rhs);
self
}
}

crate::asserts::assert_size_eq!(CompactString, String);
24 changes: 21 additions & 3 deletions compact_str/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::num;
use std::str::FromStr;

Expand Down Expand Up @@ -354,12 +355,29 @@ fn test_fmt_write() {

#[test]
fn test_plus_operator() {
assert_eq!(CompactString::from("a") + CompactString::from("b"), "ab");
// + &CompactString
assert_eq!(CompactString::from("a") + &CompactString::from("b"), "ab");
// + &str
assert_eq!(CompactString::from("a") + "b", "ab");
// + &String
assert_eq!(CompactString::from("a") + &String::from("b"), "ab");
assert_eq!(CompactString::from("a") + String::from("b"), "ab");
assert_eq!(String::from("a") + CompactString::from("b"), "ab");
// + &Box<str>
let box_str = String::from("b").into_boxed_str();
assert_eq!(CompactString::from("a") + &box_str, "ab");
// + &Cow<'a, str>
let cow = Cow::from("b");
assert_eq!(CompactString::from("a") + &cow, "ab");

// Implementing `Add<T> for String` can break adding &String or other types to String, so we
// explicitly don't do this. See https://github.com/rust-lang/rust/issues/77143 for more details.
// Below we assert adding types to String still compiles

// String + &CompactString
assert_eq!(String::from("a") + &CompactString::from("b"), "ab");
// String + &String
assert_eq!(String::from("a") + &("b".to_string()), "ab");
// String + &str
assert_eq!(String::from("a") + &"b", "ab");
}

#[test]
Expand Down

0 comments on commit e46f89c

Please sign in to comment.