nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: ParkMyCar/compact_str
Pull request #73 (2022-03-31 16:40:51)

Implement fmt::Write for CompactStr
This makes it possible to `write!()` into a CompactStr. This is useful if you know that the formatted string will be (most often) shorter than 24 bytes.

ParkMyCar: Thanks for the PR! It's probably fine to bump the MSRV, let me just look into this a bit more
ParkMyCar: Ahh I see, the optimization provided by `Arguments::as_str()` is just when the arguments contain 0 or 1 static strings and no arguments to format into the resulting string.

Given it's an optimization in only the most trivial cases, I'd opt to keep the MSRV at 1.49, and if you could move the commented code into a draft PR like you suggested, that would be great :)

Patch:
diff --git a/compact_str/src/lib.rs b/compact_str/src/lib.rs
index 96606918..2d6e1165 100644
--- a/compact_str/src/lib.rs
+++ b/compact_str/src/lib.rs
@@ -678,4 +678,11 @@ impl Extend<String> for CompactStr {
     }
 }
 
+impl fmt::Write for CompactStr {
+    fn write_str(&mut self, s: &str) -> fmt::Result {
+        self.push_str(s);
+        Ok(())
+    }
+}
+
 crate::asserts::assert_size_eq!(CompactStr, String);


Test patch:
diff --git a/compact_str/src/tests.rs b/compact_str/src/tests.rs
index 67779f6d..890cfaad 100644
--- a/compact_str/src/tests.rs
+++ b/compact_str/src/tests.rs
@@ -339,3 +339,19 @@ fn test_compact_str_is_send_and_sync() {
     fn is_send_and_sync<T: Send + Sync>() {}
     is_send_and_sync::<CompactStr>();
 }
+
+#[test]
+fn test_fmt_write() {
+    use core::fmt::Write;
+
+    let mut compact = CompactStr::default();
+
+    write!(compact, "test").unwrap();
+    assert_eq!(compact, "test");
+
+    writeln!(compact, "{}", 1234).unwrap();
+    assert_eq!(compact, "test1234\n");
+
+    write!(compact, "{:>8} {} {:<8}", "some", "more", "words").unwrap();
+    assert_eq!(compact, "test1234\n    some more words   ");
+}
Repository: ParkMyCar/compact_str
Pull request #81 (2022-04-05 03:52:54)

impl a bunch of Add<T>s for CompactStr, and Add<CompactStr> for String


Patch:
diff --git a/compact_str/src/lib.rs b/compact_str/src/lib.rs
index 2d6e1165..c7e24ea5 100644
--- a/compact_str/src/lib.rs
+++ b/compact_str/src/lib.rs
@@ -21,7 +21,10 @@ use core::hash::{
     Hasher,
 };
 use core::iter::FromIterator;
-use core::ops::Deref;
+use core::ops::{
+    Add,
+    Deref,
+};
 use core::str::{
     FromStr,
     Utf8Error,
@@ -685,4 +688,52 @@ impl fmt::Write for CompactStr {
     }
 }
 
+impl Add<Self> for CompactStr {
+    type Output = Self;
+    fn add(mut self, rhs: Self) -> Self::Output {
+        self.push_str(&rhs);
+        self
+    }
+}
+
+impl Add<&Self> for CompactStr {
+    type Output = Self;
+    fn add(mut self, rhs: &Self) -> Self::Output {
+        self.push_str(rhs);
+        self
+    }
+}
+
+impl Add<&str> for CompactStr {
+    type Output = Self;
+    fn add(mut self, rhs: &str) -> Self::Output {
+        self.push_str(rhs);
+        self
+    }
+}
+
+impl Add<&String> for CompactStr {
+    type Output = Self;
+    fn add(mut self, rhs: &String) -> Self::Output {
+        self.push_str(rhs);
+        self
+    }
+}
+
+impl Add<String> for CompactStr {
+    type Output = Self;
+    fn add(mut self, rhs: String) -> Self::Output {
+        self.push_str(&rhs);
+        self
+    }
+}
+
+impl Add<CompactStr> for String {
+    type Output = Self;
+    fn add(mut self, rhs: CompactStr) -> Self::Output {
+        self.push_str(&rhs);
+        self
+    }
+}
+
 crate::asserts::assert_size_eq!(CompactStr, String);


Test patch:
diff --git a/compact_str/src/tests.rs b/compact_str/src/tests.rs
index 890cfaad..3689478a 100644
--- a/compact_str/src/tests.rs
+++ b/compact_str/src/tests.rs
@@ -355,3 +355,13 @@ fn test_fmt_write() {
     write!(compact, "{:>8} {} {:<8}", "some", "more", "words").unwrap();
     assert_eq!(compact, "test1234\n    some more words   ");
 }
+
+#[test]
+fn test_plus_operator() {
+    assert_eq!(CompactStr::from("a") + CompactStr::from("b"), "ab");
+    assert_eq!(CompactStr::from("a") + &CompactStr::from("b"), "ab");
+    assert_eq!(CompactStr::from("a") + "b", "ab");
+    assert_eq!(CompactStr::from("a") + &String::from("b"), "ab");
+    assert_eq!(CompactStr::from("a") + String::from("b"), "ab");
+    assert_eq!(String::from("a") + CompactStr::from("b"), "ab");
+}
Repository: ParkMyCar/compact_str
Pull request #197 (2022-08-07 22:32:15)

Don't format char to determine it's UTF-8 length


Patch:
diff --git a/compact_str/src/lib.rs b/compact_str/src/lib.rs
index c25d365e..53f6ebe1 100644
--- a/compact_str/src/lib.rs
+++ b/compact_str/src/lib.rs
@@ -1,6 +1,8 @@
 #![doc = include_str!("../README.md")]
 #![cfg_attr(docsrs, feature(doc_cfg))]
 
+#[doc(hidden)]
+pub use core;
 use core::borrow::{
     Borrow,
     BorrowMut,
diff --git a/compact_str/src/macros.rs b/compact_str/src/macros.rs
index 0a28cbea..9c157d2b 100644
--- a/compact_str/src/macros.rs
+++ b/compact_str/src/macros.rs
@@ -26,9 +26,9 @@
 /// `ToCompactString::to_compact_string` never returns an error itself.
 #[macro_export]
 macro_rules! format_compact {
-    ($($arg:tt)*) => {{
-        $crate::ToCompactString::to_compact_string(&format_args!($($arg)*))
-    }}
+    ($($arg:tt)*) => {
+        $crate::ToCompactString::to_compact_string(&$crate::core::format_args!($($arg)*))
+    }
 }
 
 #[cfg(test)]
diff --git a/compact_str/src/utility.rs b/compact_str/src/utility.rs
index 98cd0eb5..d4d338c9 100644
--- a/compact_str/src/utility.rs
+++ b/compact_str/src/utility.rs
@@ -19,6 +19,12 @@ impl Write for Sink {
         self.0 += s.len();
         Ok(())
     }
+
+    #[inline(always)]
+    fn write_char(&mut self, c: char) -> fmt::Result {
+        self.0 += c.len_utf8();
+        Ok(())
+    }
 }
 
 #[cfg(test)]


Test patch:
diff --git a/compact_str/src/tests.rs b/compact_str/src/tests.rs
index 0b1be1b2..2e423db6 100644
--- a/compact_str/src/tests.rs
+++ b/compact_str/src/tests.rs
@@ -8,6 +8,7 @@ use proptest::strategy::Strategy;
 use test_strategy::proptest;
 
 use crate::{
+    format_compact,
     CompactString,
     ToCompactString,
 };
@@ -774,19 +775,6 @@ fn test_bool_to_compact_string() {
     assert!(!c.is_heap_allocated());
 }
 
-macro_rules! format_compact {
-    ( $fmt:expr $(, $args:tt)* ) => {
-        ToCompactString::to_compact_string(
-            &core::format_args!(
-                $fmt,
-                $(
-                    $args,
-                )*
-            )
-        )
-    };
-}
-
 macro_rules! assert_int_MAX_to_compact_string {
     ($int: ty) => {
         assert_eq!(&*<$int>::MAX.to_string(), &*<$int>::MAX.to_compact_string());