Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tweak the CompactStringExt trait so join_compact and concat_compact work better #418

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions compact_str/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub trait CompactStringExt {
///
/// assert_eq!(compact, "hello world!");
/// ```
fn concat_compact(&self) -> CompactString;
fn concat_compact(self) -> CompactString;

/// Joins all the items of a collection, placing a separator between them, forming a
/// [`CompactString`]
Expand All @@ -165,23 +165,23 @@ pub trait CompactStringExt {
///
/// assert_eq!(compact, "apples, oranges, bananas");
/// ```
fn join_compact<S: AsRef<str>>(&self, separator: S) -> CompactString;
fn join_compact<S: AsRef<str>>(self, separator: S) -> CompactString;
}

impl<I, C> CompactStringExt for C
where
I: AsRef<str>,
for<'a> &'a C: IntoIterator<Item = &'a I>,
C: IntoIterator<Item = I>,
{
fn concat_compact(&self) -> CompactString {
fn concat_compact(self) -> CompactString {
self.into_iter()
.fold(CompactString::const_new(""), |mut s, item| {
s.push_str(item.as_ref());
s
})
}

fn join_compact<S: AsRef<str>>(&self, separator: S) -> CompactString {
fn join_compact<S: AsRef<str>>(self, separator: S) -> CompactString {
let mut compact_string = CompactString::const_new("");

let mut iter = self.into_iter().peekable();
Expand Down Expand Up @@ -213,18 +213,44 @@ mod tests {
#[test]
fn test_join() {
let slice = ["hello", "world"];
let c = slice.join_compact(" ");
assert_eq!(c, "hello world");
let c1 = (&slice).join_compact(" ");
let c2 = slice.join_compact(" ");
assert_eq!(c1, "hello world");
assert_eq!(c2, "hello world");

let vector = vec!["🍎", "🍊", "🍌"];
let c = vector.join_compact(",");
assert_eq!(c, "🍎,🍊,🍌");

let owned_strings = ["foo".to_string(), "bar".to_string()];
let c = owned_strings.join_compact("/");
assert_eq!(c, "foo/bar");
}

#[test]
fn test_join_iter() {
let values = ["foo", "bar", "baz"];
let compact = values.iter().map(|x| x.to_string()).join_compact("/");
assert_eq!(compact, "foo/bar/baz");

let compact = values
.clone()
.into_iter()
.map(|x| x.trim())
.join_compact("-");
assert_eq!(compact, "foo-bar-baz");

let compact = values
.into_iter()
.map(CompactString::from)
.join_compact("...");
assert_eq!(compact, "foo...bar...baz");
}

#[proptest]
#[cfg_attr(miri, ignore)]
fn proptest_join(items: Vec<String>, separator: String) {
let c: CompactString = items.join_compact(&separator);
let c: CompactString = items.iter().join_compact(&separator);
let s: String = items.join(&separator);
assert_eq!(c, s);
}
Expand All @@ -243,7 +269,7 @@ mod tests {
#[proptest]
#[cfg_attr(miri, ignore)]
fn proptest_concat(items: Vec<String>) {
let c: CompactString = items.concat_compact();
let c: CompactString = items.iter().concat_compact();
let s: String = items.concat();
assert_eq!(c, s);
}
Expand Down
4 changes: 2 additions & 2 deletions fuzz/src/creation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ impl Creation<'_> {
Some((compact, word))
}
Join(collection, seperator) => {
let compact: CompactString = collection.join_compact(seperator);
let compact: CompactString = (&collection).join_compact(seperator);
let std_str: String = collection.join(seperator);

assert_eq!(compact, std_str);
Expand All @@ -697,7 +697,7 @@ impl Creation<'_> {
Some((compact, std_str))
}
Concat(collection) => {
let compact: CompactString = collection.concat_compact();
let compact: CompactString = (&collection).concat_compact();
let std_str: String = collection.concat();

assert_eq!(compact, std_str);
Expand Down
Loading