]> git.lizzy.rs Git - rust.git/commitdiff
Override ToOwned::clone_into for Path and OsStr
authorScott McMurray <scottmcm@users.noreply.github.com>
Thu, 13 Apr 2017 09:48:46 +0000 (02:48 -0700)
committerScott McMurray <scottmcm@users.noreply.github.com>
Wed, 19 Apr 2017 04:02:18 +0000 (21:02 -0700)
The only non-overridden one remaining is the CStr impl, which cannot
be optimized as doing so would break CString's second invariant.

src/libstd/ffi/os_str.rs
src/libstd/lib.rs
src/libstd/path.rs

index bf3f41b13c18493b87c6bfe1280787d03fc7b255..b90192dd8af24ba2cd78569e1778091c90b2587b 100644 (file)
@@ -677,7 +677,13 @@ fn borrow(&self) -> &OsStr { &self[..] }
 #[stable(feature = "rust1", since = "1.0.0")]
 impl ToOwned for OsStr {
     type Owned = OsString;
-    fn to_owned(&self) -> OsString { self.to_os_string() }
+    fn to_owned(&self) -> OsString {
+        self.to_os_string()
+    }
+    fn clone_into(&self, target: &mut OsString) {
+        target.clear();
+        target.push(self);
+    }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
@@ -863,4 +869,14 @@ fn boxed_default() {
         let boxed = <Box<OsStr>>::default();
         assert!(boxed.is_empty());
     }
+
+    #[test]
+    fn test_os_str_clone_into() {
+        let mut os_string = OsString::with_capacity(123);
+        os_string.push("hello");
+        let os_str = OsStr::new("bonjour");
+        os_str.clone_into(&mut os_string);
+        assert_eq!(os_str, os_string);
+        assert!(os_string.capacity() >= 123);
+    }
 }
index 8de6e1a24f1f2d7f705bafb092255fea2389a7fe..367779bb701c858f95f29fc28d39c510962f33e6 100644 (file)
 #![feature(str_utf16)]
 #![feature(test, rustc_private)]
 #![feature(thread_local)]
+#![feature(toowned_clone_into)]
 #![feature(try_from)]
 #![feature(unboxed_closures)]
 #![feature(unicode)]
index db446d88900c18a43122b64a59ff68dc73aa9f1b..fcbd3705e881f56267990f0fc48913b79de8e50e 100644 (file)
@@ -1322,6 +1322,9 @@ impl ToOwned for Path {
     fn to_owned(&self) -> PathBuf {
         self.to_path_buf()
     }
+    fn clone_into(&self, target: &mut PathBuf) {
+        self.inner.clone_into(&mut target.inner);
+    }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
@@ -3722,4 +3725,13 @@ fn into_boxed() {
         assert_eq!(&*boxed, &*path_buf);
         assert_eq!(&*path_buf, path);
     }
+
+    #[test]
+    fn test_clone_into() {
+        let mut path_buf = PathBuf::from("supercalifragilisticexpialidocious");
+        let path = Path::new("short");
+        path.clone_into(&mut path_buf);
+        assert_eq!(path, path_buf);
+        assert!(path_buf.into_os_string().capacity() >= 15);
+    }
 }