about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@tvl.su>2024-08-25T09·55+0300
committerclbot <clbot@tvl.fyi>2024-08-25T10·07+0000
commit6167e63e074979b3123202040fab421dfb269a22 (patch)
treeae898a178476f80dd585b53b8f87fb4183a3711c
parentd504f668e0f9938d053cef51df34d6ffe55ac87a (diff)
feat(tazjin/german-string): impl Clone for GermanString r/8581
Small strings are always copied fully, without allocations.

Large transient strings copy the data and allocate.

Large persistent strings are also a trivial copy.

Change-Id: I319c0b800fa7a4a62e634176b959bb2fa766a4eb
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12342
Autosubmit: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
-rw-r--r--users/tazjin/german-string/src/lib.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/users/tazjin/german-string/src/lib.rs b/users/tazjin/german-string/src/lib.rs
index 5cc8aebaab96..328eca309f38 100644
--- a/users/tazjin/german-string/src/lib.rs
+++ b/users/tazjin/german-string/src/lib.rs
@@ -295,6 +295,26 @@ impl Debug for GermanString {
     }
 }
 
+impl Clone for GermanString {
+    fn clone(&self) -> Self {
+        unsafe {
+            if self.len() <= 12 {
+                return GermanString(GSRepr {
+                    small: self.0.small.clone(),
+                });
+            }
+
+            if self.0.large.data.is_transient() {
+                return GermanString::transient(self.as_bytes());
+            }
+
+            return GermanString(GSRepr {
+                large: self.0.large.clone(),
+            });
+        }
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;