about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2018-05-17 05:22:07 +0800
committerkennytm <kennytm@gmail.com>2018-05-17 05:22:07 +0800
commit8366780164d062c6cb69d0243e9fcf85a37d548f (patch)
tree4aac4c7cc81b3a3fc94aa9cc69a47c3b52202bcd /src/liballoc
parent02aedec72264b76dce679570ea64a799a82ad3ce (diff)
parent7c0f664f153f4f41f820723c6b2c758ad5286531 (diff)
downloadrust-8366780164d062c6cb69d0243e9fcf85a37d548f.tar.gz
rust-8366780164d062c6cb69d0243e9fcf85a37d548f.zip
Rollup merge of #50170 - burtonageo:more_cow_from, r=alexcrichton
Implement From for more types on Cow

This is basically https://github.com/rust-lang/rust/pull/48191, except that it should be implemented in a way that doesn't break third party crates.
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/string.rs8
-rw-r--r--src/liballoc/vec.rs7
2 files changed, 15 insertions, 0 deletions
diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs
index da9afdd2ca3..449e3152d8f 100644
--- a/src/liballoc/string.rs
+++ b/src/liballoc/string.rs
@@ -2240,6 +2240,14 @@ impl<'a> From<String> for 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> {
diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs
index 690cbcb559b..d30f8cd0fca 100644
--- a/src/liballoc/vec.rs
+++ b/src/liballoc/vec.rs
@@ -2286,6 +2286,13 @@ impl<'a, T: Clone> From<Vec<T>> for 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]> {