Skip to content

Commit 1133a14

Browse files
committed
Implement From for more types on Cow
1 parent ff48277 commit 1133a14

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

src/liballoc/string.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,6 +2239,14 @@ impl<'a> From<String> for Cow<'a, str> {
22392239
}
22402240
}
22412241

2242+
#[stable(feature = "cow_from_string_ref", since = "1.28.0")]
2243+
impl<'a> From<&'a String> for Cow<'a, str> {
2244+
#[inline]
2245+
fn from(s: &'a String) -> Cow<'a, str> {
2246+
Cow::Borrowed(s.as_str())
2247+
}
2248+
}
2249+
22422250
#[stable(feature = "cow_str_from_iter", since = "1.12.0")]
22432251
impl<'a> FromIterator<char> for Cow<'a, str> {
22442252
fn from_iter<I: IntoIterator<Item = char>>(it: I) -> Cow<'a, str> {

src/liballoc/vec.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,6 +2285,13 @@ impl<'a, T: Clone> From<Vec<T>> for Cow<'a, [T]> {
22852285
}
22862286
}
22872287

2288+
#[stable(feature = "cow_from_vec_ref", since = "1.28.0")]
2289+
impl<'a, T: Clone> From<&'a Vec<T>> for Cow<'a, [T]> {
2290+
fn from(v: &'a Vec<T>) -> Cow<'a, [T]> {
2291+
Cow::Borrowed(v.as_slice())
2292+
}
2293+
}
2294+
22882295
#[stable(feature = "rust1", since = "1.0.0")]
22892296
impl<'a, T> FromIterator<T> for Cow<'a, [T]> where T: Clone {
22902297
fn from_iter<I: IntoIterator<Item = T>>(it: I) -> Cow<'a, [T]> {

src/libstd/ffi/c_str.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,30 @@ impl From<CString> for Box<CStr> {
706706
}
707707
}
708708

709+
#[stable(feature = "cow_from_cstr", since = "1.28.0")]
710+
impl<'a> From<CString> for Cow<'a, CStr> {
711+
#[inline]
712+
fn from(s: CString) -> Cow<'a, CStr> {
713+
Cow::Owned(s)
714+
}
715+
}
716+
717+
#[stable(feature = "cow_from_cstr", since = "1.28.0")]
718+
impl<'a> From<&'a CStr> for Cow<'a, CStr> {
719+
#[inline]
720+
fn from(s: &'a CStr) -> Cow<'a, CStr> {
721+
Cow::Borrowed(s)
722+
}
723+
}
724+
725+
#[stable(feature = "cow_from_cstr", since = "1.28.0")]
726+
impl<'a> From<&'a CString> for Cow<'a, CStr> {
727+
#[inline]
728+
fn from(s: &'a CString) -> Cow<'a, CStr> {
729+
Cow::Borrowed(s.as_c_str())
730+
}
731+
}
732+
709733
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
710734
impl From<CString> for Arc<CStr> {
711735
#[inline]

src/libstd/ffi/os_str.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,30 @@ impl<'a> From<&'a OsStr> for Rc<OsStr> {
664664
}
665665
}
666666

667+
#[stable(feature = "cow_from_osstr", since = "1.28.0")]
668+
impl<'a> From<OsString> for Cow<'a, OsStr> {
669+
#[inline]
670+
fn from(s: OsString) -> Cow<'a, OsStr> {
671+
Cow::Owned(s)
672+
}
673+
}
674+
675+
#[stable(feature = "cow_from_osstr", since = "1.28.0")]
676+
impl<'a> From<&'a OsStr> for Cow<'a, OsStr> {
677+
#[inline]
678+
fn from(s: &'a OsStr) -> Cow<'a, OsStr> {
679+
Cow::Borrowed(s)
680+
}
681+
}
682+
683+
#[stable(feature = "cow_from_osstr", since = "1.28.0")]
684+
impl<'a> From<&'a OsString> for Cow<'a, OsStr> {
685+
#[inline]
686+
fn from(s: &'a OsString) -> Cow<'a, OsStr> {
687+
Cow::Borrowed(s.as_os_str())
688+
}
689+
}
690+
667691
#[stable(feature = "box_default_extra", since = "1.17.0")]
668692
impl Default for Box<OsStr> {
669693
fn default() -> Box<OsStr> {

src/libstd/path.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,14 @@ impl<'a> From<PathBuf> for Cow<'a, Path> {
15321532
}
15331533
}
15341534

1535+
#[stable(feature = "cow_from_pathbuf_ref", since = "1.28.0")]
1536+
impl<'a> From<&'a PathBuf> for Cow<'a, Path> {
1537+
#[inline]
1538+
fn from(p: &'a PathBuf) -> Cow<'a, Path> {
1539+
Cow::Borrowed(p.as_path())
1540+
}
1541+
}
1542+
15351543
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
15361544
impl From<PathBuf> for Arc<Path> {
15371545
#[inline]

0 commit comments

Comments
 (0)