]> git.lizzy.rs Git - rust.git/commitdiff
Implement From for more types on Cow
authorGeorge Burton <burtonageo@gmail.com>
Sun, 22 Apr 2018 21:57:52 +0000 (22:57 +0100)
committerGeorge Burton <burtonageo@gmail.com>
Sun, 22 Apr 2018 21:57:52 +0000 (22:57 +0100)
src/liballoc/string.rs
src/liballoc/vec.rs
src/libstd/ffi/c_str.rs
src/libstd/ffi/os_str.rs
src/libstd/path.rs

index 2f84d5f7f8676877328dc57aa79155374886f6e6..5f684361fdc23f6dc2a7f6c4e6e8a9770bdbe4ca 100644 (file)
@@ -2239,6 +2239,14 @@ fn from(s: String) -> Cow<'a, str> {
     }
 }
 
+#[stable(feature = "cow_from_string_ref", since = "1.28.0")]
+impl<'a> From<&'a String> for Cow<'a, str> {
+    #[inline]
+    fn from(s: &'a String) -> Cow<'a, str> {
+        Cow::Borrowed(s.as_str())
+    }
+}
+
 #[stable(feature = "cow_str_from_iter", since = "1.12.0")]
 impl<'a> FromIterator<char> for Cow<'a, str> {
     fn from_iter<I: IntoIterator<Item = char>>(it: I) -> Cow<'a, str> {
index b184404c15bfd6c050b99252db3c15e41b2d00a2..be4c80c85d997b0340122db793c291ed9ca47f33 100644 (file)
@@ -2285,6 +2285,13 @@ fn from(v: Vec<T>) -> Cow<'a, [T]> {
     }
 }
 
+#[stable(feature = "cow_from_vec_ref", since = "1.28.0")]
+impl<'a, T: Clone> From<&'a Vec<T>> for Cow<'a, [T]> {
+    fn from(v: &'a Vec<T>) -> Cow<'a, [T]> {
+        Cow::Borrowed(v.as_slice())
+    }
+}
+
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<'a, T> FromIterator<T> for Cow<'a, [T]> where T: Clone {
     fn from_iter<I: IntoIterator<Item = T>>(it: I) -> Cow<'a, [T]> {
index c88c2bc913713634aa465e8fa82830226b3802e2..08a1596ef25f68e005701f45ec665b0ad33a3e7c 100644 (file)
@@ -706,6 +706,30 @@ fn from(s: CString) -> Box<CStr> {
     }
 }
 
+#[stable(feature = "cow_from_cstr", since = "1.28.0")]
+impl<'a> From<CString> for Cow<'a, CStr> {
+    #[inline]
+    fn from(s: CString) -> Cow<'a, CStr> {
+        Cow::Owned(s)
+    }
+}
+
+#[stable(feature = "cow_from_cstr", since = "1.28.0")]
+impl<'a> From<&'a CStr> for Cow<'a, CStr> {
+    #[inline]
+    fn from(s: &'a CStr) -> Cow<'a, CStr> {
+        Cow::Borrowed(s)
+    }
+}
+
+#[stable(feature = "cow_from_cstr", since = "1.28.0")]
+impl<'a> From<&'a CString> for Cow<'a, CStr> {
+    #[inline]
+    fn from(s: &'a CString) -> Cow<'a, CStr> {
+        Cow::Borrowed(s.as_c_str())
+    }
+}
+
 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
 impl From<CString> for Arc<CStr> {
     #[inline]
index 4850ed0c5be054fac6cd5bfe4771a7520fab1430..e42a28ed88f8243f0764ef8cf63ae67c8a4b67c9 100644 (file)
@@ -664,6 +664,30 @@ fn from(s: &OsStr) -> Rc<OsStr> {
     }
 }
 
+#[stable(feature = "cow_from_osstr", since = "1.28.0")]
+impl<'a> From<OsString> for Cow<'a, OsStr> {
+    #[inline]
+    fn from(s: OsString) -> Cow<'a, OsStr> {
+        Cow::Owned(s)
+    }
+}
+
+#[stable(feature = "cow_from_osstr", since = "1.28.0")]
+impl<'a> From<&'a OsStr> for Cow<'a, OsStr> {
+    #[inline]
+    fn from(s: &'a OsStr) -> Cow<'a, OsStr> {
+        Cow::Borrowed(s)
+    }
+}
+
+#[stable(feature = "cow_from_osstr", since = "1.28.0")]
+impl<'a> From<&'a OsString> for Cow<'a, OsStr> {
+    #[inline]
+    fn from(s: &'a OsString) -> Cow<'a, OsStr> {
+        Cow::Borrowed(s.as_os_str())
+    }
+}
+
 #[stable(feature = "box_default_extra", since = "1.17.0")]
 impl Default for Box<OsStr> {
     fn default() -> Box<OsStr> {
index ec96157547383daa4cbad54658beb78a602fd7bd..19f38e4d6d9b3e1cad75e86582aac3ffcf8fbbb9 100644 (file)
@@ -1532,6 +1532,14 @@ fn from(s: PathBuf) -> Cow<'a, Path> {
     }
 }
 
+#[stable(feature = "cow_from_pathbuf_ref", since = "1.28.0")]
+impl<'a> From<&'a PathBuf> for Cow<'a, Path> {
+    #[inline]
+    fn from(p: &'a PathBuf) -> Cow<'a, Path> {
+        Cow::Borrowed(p.as_path())
+    }
+}
+
 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
 impl From<PathBuf> for Arc<Path> {
     #[inline]