about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorShotaro Yamada <sinkuu@sinkuu.xyz>2019-10-18 16:10:13 +0900
committerShotaro Yamada <sinkuu@sinkuu.xyz>2019-10-18 16:10:13 +0900
commit23cb1d520bbe943b9dfae54c6a8f2f1fe4748872 (patch)
treed5f116e655cde7c8344873c92138c2e75ab649b6 /src/libstd
parentfa0f7d0080d8e7e9eb20aa9cbf8013f96c81287f (diff)
downloadrust-23cb1d520bbe943b9dfae54c6a8f2f1fe4748872.tar.gz
rust-23cb1d520bbe943b9dfae54c6a8f2f1fe4748872.zip
Avoid realloc in `CString::new`
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/ffi/c_str.rs26
-rw-r--r--src/libstd/lib.rs1
2 files changed, 26 insertions, 1 deletions
diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs
index 483f2ba52ec..6dcda986310 100644
--- a/src/libstd/ffi/c_str.rs
+++ b/src/libstd/ffi/c_str.rs
@@ -327,7 +327,31 @@ impl CString {
     /// [`NulError`]: struct.NulError.html
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn new<T: Into<Vec<u8>>>(t: T) -> Result<CString, NulError> {
-        Self::_new(t.into())
+        trait SpecIntoVec {
+            fn into_vec(self) -> Vec<u8>;
+        }
+        impl<T: Into<Vec<u8>>> SpecIntoVec for T {
+            default fn into_vec(self) -> Vec<u8> {
+                self.into()
+            }
+        }
+        // Specialization for avoiding reallocation.
+        impl SpecIntoVec for &'_ [u8] {
+            fn into_vec(self) -> Vec<u8> {
+                let mut v = Vec::with_capacity(self.len() + 1);
+                v.extend(self);
+                v
+            }
+        }
+        impl SpecIntoVec for &'_ str {
+            fn into_vec(self) -> Vec<u8> {
+                let mut v = Vec::with_capacity(self.len() + 1);
+                v.extend(self.as_bytes());
+                v
+            }
+        }
+
+        Self::_new(SpecIntoVec::into_vec(t))
     }
 
     fn _new(bytes: Vec<u8>) -> Result<CString, NulError> {
diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs
index af6cb656444..50a1226cc1d 100644
--- a/src/libstd/lib.rs
+++ b/src/libstd/lib.rs
@@ -297,6 +297,7 @@
 #![feature(slice_concat_ext)]
 #![feature(slice_internals)]
 #![feature(slice_patterns)]
+#![feature(specialization)]
 #![feature(staged_api)]
 #![feature(std_internals)]
 #![feature(stdsimd)]