Skip to content

Commit 8366780

Browse files
committed
Rollup merge of rust-lang#50170 - burtonageo:more_cow_from, r=alexcrichton
Implement From for more types on Cow This is basically rust-lang#48191, except that it should be implemented in a way that doesn't break third party crates.
2 parents 02aedec + 7c0f664 commit 8366780

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed

src/liballoc/string.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2240,6 +2240,14 @@ impl<'a> From<String> for Cow<'a, str> {
22402240
}
22412241
}
22422242

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

src/liballoc/vec.rs

+7
Original file line numberDiff line numberDiff line change
@@ -2286,6 +2286,13 @@ impl<'a, T: Clone> From<Vec<T>> for Cow<'a, [T]> {
22862286
}
22872287
}
22882288

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

src/libstd/ffi/c_str.rs

+32
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,14 @@ impl Borrow<CStr> for CString {
682682
fn borrow(&self) -> &CStr { self }
683683
}
684684

685+
#[stable(feature = "cstring_from_cow_cstr", since = "1.28.0")]
686+
impl<'a> From<Cow<'a, CStr>> for CString {
687+
#[inline]
688+
fn from(s: Cow<'a, CStr>) -> Self {
689+
s.into_owned()
690+
}
691+
}
692+
685693
#[stable(feature = "box_from_c_str", since = "1.17.0")]
686694
impl<'a> From<&'a CStr> for Box<CStr> {
687695
fn from(s: &'a CStr) -> Box<CStr> {
@@ -706,6 +714,30 @@ impl From<CString> for Box<CStr> {
706714
}
707715
}
708716

717+
#[stable(feature = "cow_from_cstr", since = "1.28.0")]
718+
impl<'a> From<CString> for Cow<'a, CStr> {
719+
#[inline]
720+
fn from(s: CString) -> Cow<'a, CStr> {
721+
Cow::Owned(s)
722+
}
723+
}
724+
725+
#[stable(feature = "cow_from_cstr", since = "1.28.0")]
726+
impl<'a> From<&'a CStr> for Cow<'a, CStr> {
727+
#[inline]
728+
fn from(s: &'a CStr) -> Cow<'a, CStr> {
729+
Cow::Borrowed(s)
730+
}
731+
}
732+
733+
#[stable(feature = "cow_from_cstr", since = "1.28.0")]
734+
impl<'a> From<&'a CString> for Cow<'a, CStr> {
735+
#[inline]
736+
fn from(s: &'a CString) -> Cow<'a, CStr> {
737+
Cow::Borrowed(s.as_c_str())
738+
}
739+
}
740+
709741
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
710742
impl From<CString> for Arc<CStr> {
711743
#[inline]

src/libstd/ffi/os_str.rs

+32
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,38 @@ 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+
691+
#[stable(feature = "osstring_from_cow_osstr", since = "1.28.0")]
692+
impl<'a> From<Cow<'a, OsStr>> for OsString {
693+
#[inline]
694+
fn from(s: Cow<'a, OsStr>) -> Self {
695+
s.into_owned()
696+
}
697+
}
698+
667699
#[stable(feature = "box_default_extra", since = "1.17.0")]
668700
impl Default for Box<OsStr> {
669701
fn default() -> Box<OsStr> {

src/libstd/path.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,22 @@ impl<'a> From<PathBuf> for Cow<'a, Path> {
15041504
}
15051505
}
15061506

1507+
#[stable(feature = "cow_from_pathbuf_ref", since = "1.28.0")]
1508+
impl<'a> From<&'a PathBuf> for Cow<'a, Path> {
1509+
#[inline]
1510+
fn from(p: &'a PathBuf) -> Cow<'a, Path> {
1511+
Cow::Borrowed(p.as_path())
1512+
}
1513+
}
1514+
1515+
#[stable(feature = "pathbuf_from_cow_path", since = "1.28.0")]
1516+
impl<'a> From<Cow<'a, Path>> for PathBuf {
1517+
#[inline]
1518+
fn from(p: Cow<'a, Path>) -> Self {
1519+
p.into_owned()
1520+
}
1521+
}
1522+
15071523
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
15081524
impl From<PathBuf> for Arc<Path> {
15091525
#[inline]

0 commit comments

Comments
 (0)